yapi/server/models/interface.js

182 lines
4.6 KiB
JavaScript
Raw Normal View History

const yapi = require('../yapi.js');
const baseModel = require('./base.js');
2017-07-03 16:16:05 +08:00
class interfaceModel extends baseModel {
getName() {
return 'interface';
2017-07-10 11:56:53 +08:00
}
2017-07-03 16:16:05 +08:00
getSchema() {
2017-07-10 11:56:53 +08:00
return {
2017-10-25 19:33:34 +08:00
title: {type: String, required: true},
uid: {type: Number, required: true},
path: {type: String, required: true},
method: {type: String, required: true},
project_id: {type: Number, required: true},
2017-08-18 20:35:31 +08:00
catid: {type: Number, required: true},
2017-08-17 12:03:55 +08:00
edit_uid: {type: Number, default: 0},
2017-08-15 12:08:59 +08:00
status: {type: String, enum: ['undone', 'done'], default: 'undone'},
2017-07-10 11:56:53 +08:00
desc: String,
add_time: Number,
up_time: Number,
2017-10-25 19:33:34 +08:00
type: {type: String, enum: ['static', 'var'], default: 'static'},
2017-09-19 13:22:36 +08:00
query_path: {
path: String,
params: [{
name: String, value: String
}],
},
2017-10-25 19:33:34 +08:00
req_query: [{
name: String, value: String, example: String, desc: String, required: {
type: String,
2017-08-15 12:08:59 +08:00
enum: ["1", "0"],
default: "1"
}
}],
2017-07-10 11:56:53 +08:00
req_headers: [{
2017-10-25 19:33:34 +08:00
name: String, value: String, example: String, desc: String, required: {
type: String,
2017-08-15 12:08:59 +08:00
enum: ["1", "0"],
default: "1"
}
2017-07-10 11:56:53 +08:00
}],
2017-10-25 19:33:34 +08:00
req_params: [{
name: String,
2017-09-21 20:47:08 +08:00
desc: String,
2017-10-25 19:33:34 +08:00
example: String
}],
2017-08-15 12:08:59 +08:00
req_body_type: {
2017-07-10 11:56:53 +08:00
type: String,
2017-08-24 16:37:45 +08:00
enum: ['form', 'json', 'text', 'file', 'raw']
2017-07-10 11:56:53 +08:00
},
2017-08-15 12:08:59 +08:00
req_body_form: [{
2017-10-25 19:33:34 +08:00
name: String, type: {type: String, enum: ['text', 'file']}, example: String, desc: String, required: {
type: String,
2017-08-15 12:08:59 +08:00
enum: ["1", "0"],
default: "1"
}
2017-07-10 11:56:53 +08:00
}],
2017-08-15 12:08:59 +08:00
req_body_other: String,
2017-07-10 11:56:53 +08:00
res_body_type: {
type: String,
2017-08-24 16:37:45 +08:00
enum: ['json', 'text', 'xml', 'raw']
2017-07-10 11:56:53 +08:00
},
res_body: String
2017-09-08 11:18:30 +08:00
};
2017-07-10 11:56:53 +08:00
}
2017-07-03 16:16:05 +08:00
2017-07-10 11:56:53 +08:00
save(data) {
let m = new this.model(data);
return m.save();
}
2017-07-03 16:16:05 +08:00
get(id) {
2017-07-10 11:56:53 +08:00
return this.model.findOne({
_id: id
})
.exec();
2017-07-10 11:56:53 +08:00
}
2017-10-25 19:33:34 +08:00
getBaseinfo(id) {
return this.model.findOne({
2017-09-08 11:18:30 +08:00
_id: id
}).select('path method uid title project_id cat_id status req_body_other req_body_type').exec()
}
2017-10-25 19:33:34 +08:00
getVar(project_id, method) {
2017-08-16 10:56:42 +08:00
return this.model.find({
project_id: project_id,
2017-08-16 10:56:42 +08:00
type: 'var',
method: method
}).select('_id path').exec()
}
2017-09-19 13:22:36 +08:00
getByQueryPath(project_id, path, method) {
return this.model.find({
project_id: project_id,
"query_path.path": path,
method: method
})
.exec();
}
getByPath(project_id, path, method) {
2017-07-10 20:02:44 +08:00
return this.model.find({
project_id: project_id,
path: path,
method: method
})
.exec();
2017-07-10 20:02:44 +08:00
}
checkRepeat(id, path, method) {
2017-07-10 11:56:53 +08:00
return this.model.count({
2017-07-26 17:56:51 +08:00
project_id: id,
2017-07-10 11:56:53 +08:00
path: path,
method: method
});
2017-07-10 11:56:53 +08:00
}
countByProjectId(id) {
2017-07-10 20:02:44 +08:00
return this.model.count({
project_id: id
});
2017-07-10 20:02:44 +08:00
}
2017-07-10 11:56:53 +08:00
2017-08-18 20:35:31 +08:00
list(project_id, select) {
select = select || '_id title uid path method project_id catid edit_uid status desc add_time up_time'
2017-07-10 11:56:53 +08:00
return this.model.find({
project_id: project_id
})
2017-08-18 20:35:31 +08:00
.select(select)
2017-10-25 19:33:34 +08:00
.sort({_id: -1})
.exec();
2017-07-10 11:56:53 +08:00
}
2017-10-25 19:33:34 +08:00
//获取全部接口信息
getInterfaceListCount() {
return this.model.count({});
}
listByCatid(catid) {
2017-08-18 20:35:31 +08:00
return this.model.find({
catid: catid
2017-09-17 13:36:51 +08:00
}).exec();
2017-08-18 20:35:31 +08:00
}
del(id) {
2017-10-18 10:04:16 +08:00
return this.model.remove({
2017-07-10 11:56:53 +08:00
_id: id
});
2017-07-10 11:56:53 +08:00
}
2017-08-10 12:14:35 +08:00
2017-10-25 19:33:34 +08:00
delByCatid(id) {
2017-10-18 10:04:16 +08:00
return this.model.remove({
2017-08-18 20:35:31 +08:00
catid: id
})
}
2017-10-25 19:33:34 +08:00
delByProjectId(id) {
2017-10-18 10:04:16 +08:00
return this.model.remove({
2017-08-16 10:56:42 +08:00
project_id: id
})
}
up(id, data) {
2017-07-10 11:56:53 +08:00
data.up_time = yapi.commons.time();
return this.model.update({
2017-07-27 15:06:42 +08:00
_id: id
2017-10-25 19:33:34 +08:00
}, data, {runValidators: true});
2017-08-17 15:19:47 +08:00
}
2017-10-25 19:33:34 +08:00
upEditUid(id, uid) {
2017-08-17 15:19:47 +08:00
return this.model.update({
2017-10-25 19:33:34 +08:00
_id: id
},
{edit_uid: uid},
{runValidators: true});
2017-07-10 11:56:53 +08:00
}
2017-07-03 16:16:05 +08:00
}
2017-08-10 12:14:35 +08:00
module.exports = interfaceModel;