yapi/server/models/interface.js

139 lines
3.4 KiB
JavaScript
Raw Normal View History

import yapi from '../yapi.js';
import baseModel from './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 {
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-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-08-16 10:56:42 +08:00
type: {type: String, enum: ['static', 'var'], default:'static'},
req_query:[{
2017-08-15 12:08:59 +08:00
name: String, value: String, desc: String, required: {
type:String,
enum: ["1", "0"],
default: "1"
}
}],
2017-07-10 11:56:53 +08:00
req_headers: [{
2017-08-15 12:08:59 +08:00
name: String, value: String, desc: String, required: {
type:String,
enum: ["1", "0"],
default: "1"
}
2017-07-10 11:56:53 +08:00
}],
req_params:[{
name: String,
desc: String
}],
2017-08-15 12:08:59 +08:00
req_body_type: {
2017-07-10 11:56:53 +08:00
type: String,
2017-08-15 12:08:59 +08:00
enum: ['form', 'json', 'text', 'file']
2017-07-10 11:56:53 +08:00
},
2017-08-15 12:08:59 +08:00
req_body_form: [{
name: String, type: { type: String, enum: ['text', 'file'] }, desc: String, required: {
type:String,
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,
enum: ['json', 'text', 'xml']
2017-07-10 11:56:53 +08:00
},
res_body: String
2017-08-17 15:19:47 +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-08-16 10:56:42 +08:00
getVar(project_id, method){
return this.model.find({
type: 'var',
method: method
}).select('_id path').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
list(project_id) {
2017-07-10 11:56:53 +08:00
return this.model.find({
project_id: project_id
})
.sort({ _id: -1 })
.exec();
2017-07-10 11:56:53 +08:00
}
del(id) {
2017-07-10 11:56:53 +08:00
return this.model.deleteOne({
_id: id
});
2017-07-10 11:56:53 +08:00
}
2017-08-10 12:14:35 +08:00
2017-08-16 10:56:42 +08:00
delByProjectId(id){
return this.model.deleteMany({
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-08-17 15:19:47 +08:00
}, data, {runValidators: true });
}
upEditUid(id, uid){
return this.model.update({
_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;