yapi/server/models/group.js

112 lines
2.3 KiB
JavaScript
Raw Normal View History

import yapi from '../yapi.js';
import baseModel from './base.js';
class groupModel extends baseModel {
getName() {
return 'group';
}
2017-07-04 17:19:20 +08:00
getSchema() {
return {
2017-07-06 19:21:54 +08:00
uid: Number,
group_name: String,
group_desc: String,
add_time: Number,
2017-08-08 14:45:19 +08:00
up_time: Number,
members: [
{
uid: Number,
role: {type: String, enum:['owner', 'dev']},
username: String,
email: String
}
]
};
}
save(data) {
let m = new this.model(data);
2017-07-04 17:19:20 +08:00
return m.save();
}
2017-07-28 17:29:17 +08:00
get(id) {
return this.model.findOne({
_id: id
}).exec();
}
checkRepeat(name) {
return this.model.count({
2017-07-04 17:19:20 +08:00
group_name: name
});
}
2017-08-08 14:45:19 +08:00
addMember(id, data){
return this.model.update(
{
_id: id
}, {
$push: { members: data }
}
);
}
delMember(id, uid) {
return this.model.update(
{
_id: id
}, {
$pull: { members: {uid: uid} }
}
);
}
changeMemberRole(id, uid, role) {
return this.model.update(
{
_id: id,
"members.uid": uid
}, {
2017-08-15 17:55:52 +08:00
"$set": { "members.$.role": role}
}
);
}
2017-08-08 14:45:19 +08:00
checkMemberRepeat(id, uid){
return this.model.count({
_id: id,
"members.uid": uid
});
}
list() {
2017-07-27 19:49:26 +08:00
return this.model.find().select('group_name _id group_desc add_time up_time').exec();
}
del(id) {
return this.model.deleteOne({
2017-07-04 17:19:20 +08:00
_id: id
});
}
up(id, data) {
return this.model.update(
{
_id: id
}, {
group_name: data.group_name,
group_desc: data.group_desc,
up_time: yapi.commons.time()
}
);
2017-07-04 17:19:20 +08:00
}
search(keyword) {
return this.model.find({
2017-07-14 17:00:30 +08:00
group_name: new RegExp(keyword, 'i')
})
.limit(10);
}
}
module.exports = groupModel;