2017-09-03 08:43:13 +08:00
|
|
|
const yapi = require('../yapi.js');
|
|
|
|
const baseModel = require('./base.js');
|
2017-07-04 16:43:07 +08:00
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
class groupModel extends baseModel {
|
|
|
|
getName() {
|
|
|
|
return 'group';
|
2017-07-05 16:17:58 +08:00
|
|
|
}
|
2017-07-04 17:19:20 +08:00
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
getSchema() {
|
2017-07-05 16:17:58 +08:00
|
|
|
return {
|
2017-07-06 19:21:54 +08:00
|
|
|
uid: Number,
|
2017-07-05 16:17:58 +08:00
|
|
|
group_name: String,
|
|
|
|
group_desc: String,
|
|
|
|
add_time: Number,
|
2017-08-08 14:45:19 +08:00
|
|
|
up_time: Number,
|
2017-09-29 17:02:19 +08:00
|
|
|
type: {type:String,default: 'public', enum: ['public', 'private']},
|
2017-08-08 14:45:19 +08:00
|
|
|
members: [
|
|
|
|
{
|
2017-08-18 15:25:27 +08:00
|
|
|
uid: Number,
|
2017-08-08 14:45:19 +08:00
|
|
|
role: {type: String, enum:['owner', 'dev']},
|
|
|
|
username: String,
|
|
|
|
email: String
|
|
|
|
}
|
|
|
|
]
|
2017-07-26 22:03:18 +08:00
|
|
|
};
|
2017-07-05 16:17:58 +08:00
|
|
|
}
|
2017-07-04 16:43:07 +08:00
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
save(data) {
|
|
|
|
let m = new this.model(data);
|
2017-07-04 17:19:20 +08:00
|
|
|
return m.save();
|
2017-07-05 16:17:58 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
2017-07-28 17:29:17 +08:00
|
|
|
get(id) {
|
|
|
|
return this.model.findOne({
|
|
|
|
_id: id
|
|
|
|
}).exec();
|
|
|
|
}
|
|
|
|
|
2017-09-27 13:59:51 +08:00
|
|
|
getByPrivateUid(uid){
|
|
|
|
return this.model.findOne({
|
|
|
|
uid: uid,
|
|
|
|
type: 'private'
|
|
|
|
}).select('group_name _id group_desc add_time up_time type').exec();
|
|
|
|
}
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
getGroupById(id) {
|
|
|
|
return this.model.findOne({
|
|
|
|
_id: id
|
2017-09-27 13:59:51 +08:00
|
|
|
}).select("uid group_name group_desc add_time up_time type").exec();
|
2017-08-16 17:13:14 +08:00
|
|
|
}
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
checkRepeat(name) {
|
2017-07-05 16:17:58 +08:00
|
|
|
return this.model.count({
|
2017-07-04 17:19:20 +08:00
|
|
|
group_name: name
|
2017-07-26 22:03:18 +08:00
|
|
|
});
|
2017-07-05 16:17:58 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
2017-08-08 14:45:19 +08:00
|
|
|
addMember(id, data){
|
|
|
|
return this.model.update(
|
|
|
|
{
|
|
|
|
_id: id
|
|
|
|
}, {
|
2017-09-27 18:21:01 +08:00
|
|
|
// $push: { members: data },
|
|
|
|
$push: { members: { $each: data } }
|
2017-08-08 14:45:19 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
delMember(id, uid) {
|
|
|
|
return this.model.update(
|
|
|
|
{
|
|
|
|
_id: id
|
|
|
|
}, {
|
|
|
|
$pull: { members: {uid: uid} }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-08 19:18:12 +08:00
|
|
|
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 19:18:12 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-08 14:45:19 +08:00
|
|
|
checkMemberRepeat(id, uid){
|
|
|
|
return this.model.count({
|
|
|
|
_id: id,
|
|
|
|
"members.uid": uid
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
list() {
|
2017-09-27 13:59:51 +08:00
|
|
|
return this.model.find({
|
|
|
|
type: 'public'
|
2017-09-27 14:43:13 +08:00
|
|
|
}).select('group_name _id group_desc add_time up_time type uid').exec();
|
2017-07-05 16:17:58 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
del(id) {
|
2017-07-05 16:17:58 +08:00
|
|
|
return this.model.deleteOne({
|
2017-07-04 17:19:20 +08:00
|
|
|
_id: id
|
2017-07-26 22:03:18 +08:00
|
|
|
});
|
2017-07-05 16:17:58 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-07-05 16:17:58 +08:00
|
|
|
|
2017-07-14 16:41:14 +08:00
|
|
|
search(keyword) {
|
|
|
|
return this.model.find({
|
2017-07-14 17:00:30 +08:00
|
|
|
group_name: new RegExp(keyword, 'i')
|
2017-07-14 16:41:14 +08:00
|
|
|
})
|
2017-07-26 22:03:18 +08:00
|
|
|
.limit(10);
|
2017-07-14 16:41:14 +08:00
|
|
|
}
|
2017-07-05 16:17:58 +08:00
|
|
|
}
|
|
|
|
|
2017-08-18 15:25:27 +08:00
|
|
|
module.exports = groupModel;
|