yapi/server/models/project.js

158 lines
3.5 KiB
JavaScript
Raw Normal View History

import yapi from '../yapi.js';
import baseModel from './base.js';
class projectModel extends baseModel {
getName() {
return 'project';
2017-07-06 19:21:54 +08:00
}
getSchema() {
2017-07-06 19:21:54 +08:00
return {
2017-08-08 14:45:19 +08:00
uid: {type: Number, required: true},
name: { type: String, required: true },
basepath: {type: String },
2017-07-06 19:21:54 +08:00
desc: String,
group_id: { type: Number, required: true },
project_type: {type:String, required: true, enum: ['public', 'private']},
2017-08-08 14:45:19 +08:00
members: [
2017-08-14 15:06:32 +08:00
{uid: Number, role: {type: String, enum:['owner', 'dev']},username: String, email: String}
2017-08-08 14:45:19 +08:00
],
2017-07-10 20:02:44 +08:00
env: [
{ name: String, domain: String }
2017-07-10 20:02:44 +08:00
],
2017-07-06 19:21:54 +08:00
add_time: Number,
up_time: Number
};
2017-07-06 19:21:54 +08:00
}
2017-07-06 19:21:54 +08:00
save(data) {
let m = new this.model(data);
return m.save();
2017-07-06 19:21:54 +08:00
}
get(id) {
2017-07-06 19:21:54 +08:00
return this.model.findOne({
_id: id
}).exec();
2017-07-06 19:21:54 +08:00
}
getByDomain(domain) {
2017-07-10 20:02:44 +08:00
return this.model.find({
prd_host: domain
}).exec();
2017-07-10 20:02:44 +08:00
}
checkNameRepeat(name) {
2017-07-06 19:21:54 +08:00
return this.model.count({
name: name
});
2017-07-06 19:21:54 +08:00
}
checkDomainRepeat(domain, basepath) {
2017-07-06 19:21:54 +08:00
return this.model.count({
prd_host: domain,
basepath: basepath
});
2017-07-06 19:21:54 +08:00
}
list(group_id, auth) {
let params = {group_id: group_id}
2017-08-09 16:16:35 +08:00
if(!auth) params.project_type = 'public';
return this.model.find(params).select("_id uid name basepath desc group_id project_type env add_time up_time").sort({ _id: -1 }).exec();
2017-07-06 19:21:54 +08:00
}
2017-07-18 16:37:28 +08:00
listWithPaging(group_id, page, limit) {
page = parseInt(page);
limit = parseInt(limit);
return this.model.find({
group_id: group_id
}).sort({ _id: -1 }).skip((page - 1) * limit).limit(limit).exec();
2017-07-18 16:37:28 +08:00
}
listCount(group_id) {
return this.model.count({
group_id: group_id
});
2017-07-18 16:37:28 +08:00
}
countByGroupId(group_id) {
2017-07-10 20:02:44 +08:00
return this.model.count({
group_id: group_id
});
2017-07-10 20:02:44 +08:00
}
del(id) {
2017-07-06 19:21:54 +08:00
return this.model.deleteOne({
_id: id
});
2017-07-06 19:21:54 +08:00
}
2017-08-16 10:56:42 +08:00
delByGroupid(groupId){
return this.model.deleteMany({
2017-08-16 10:56:42 +08:00
group_id: groupId
})
}
up(id, data) {
data.up_time = yapi.commons.time();
2017-07-06 19:21:54 +08:00
return this.model.update({
2017-07-27 19:49:26 +08:00
_id: id
}, data, { runValidators: true });
}
2017-07-06 19:21:54 +08:00
2017-08-08 14:45:19 +08:00
addMember(id, data) {
return this.model.update(
{
_id: id
}, {
2017-08-08 14:45:19 +08:00
$push: { members: data }
}
);
2017-07-06 19:21:54 +08:00
}
delMember(id, uid) {
return this.model.update(
{
_id: id
}, {
2017-08-14 15:06:32 +08:00
$pull: { members: {uid: uid} }
}
);
2017-07-06 19:21:54 +08:00
}
checkMemberRepeat(id, uid) {
2017-07-06 19:21:54 +08:00
return this.model.count({
_id: id,
2017-08-08 14:45:19 +08:00
"members.uid": uid
});
2017-07-06 19:21:54 +08:00
}
changeMemberRole(id, uid, role) {
return this.model.update(
{
_id: id,
"members.uid": uid
}, {
"$set": { "members.$.uid": role}
}
);
}
search(keyword) {
return this.model.find({
name: new RegExp(keyword, 'ig')
})
.limit(10);
}
2017-08-10 12:14:35 +08:00
download(id) {
console.log('models in download');
// return this.model.find({
// name: new RegExp(id, 'ig')
// })
// .limit(10);
}
2017-07-06 19:21:54 +08:00
}
2017-08-10 12:14:35 +08:00
module.exports = projectModel;