MCPcopy
hub / github.com/YMFE/yapi / add

Method add

server/controllers/group.js:126–190  ·  view source on GitHub ↗

* 添加项目分组 * @interface /group/add * @method POST * @category group * @foldnumber 10 * @param {String} group_name 项目分组名称,不能为空 * @param {String} [group_desc] 项目分组描述 * @param {String} [owner_uids] 组长[uid] * @returns {Object} * @example ./api/group/add.json

(ctx)

Source from the content-addressed store, hash-verified

124 * @example ./api/group/add.json
125 */
126 async add(ctx) {
127 let params = ctx.params;
128
129 // 新版每个人都有权限添加分组
130
131 // if (this.getRole() !== 'admin') {
132 // return (ctx.body = yapi.commons.resReturn(null, 401, '没有权限'));
133 // }
134
135 let owners = [];
136
137 if(params.owner_uids.length === 0){
138 params.owner_uids.push(
139 this.getUid()
140 )
141 }
142
143 if (params.owner_uids) {
144 for (let i = 0, len = params.owner_uids.length; i < len; i++) {
145 let id = params.owner_uids[i];
146 let groupUserdata = await this.getUserdata(id, 'owner');
147 if (groupUserdata) {
148 owners.push(groupUserdata);
149 }
150 }
151 }
152
153 let groupInst = yapi.getInst(groupModel);
154
155 let checkRepeat = await groupInst.checkRepeat(params.group_name);
156
157 if (checkRepeat > 0) {
158 return (ctx.body = yapi.commons.resReturn(null, 401, '项目分组名已存在'));
159 }
160
161 let data = {
162 group_name: params.group_name,
163 group_desc: params.group_desc,
164 uid: this.getUid(),
165 add_time: yapi.commons.time(),
166 up_time: yapi.commons.time(),
167 members: owners
168 };
169
170 let result = await groupInst.save(data);
171 result = yapi.commons.fieldSelect(result, [
172 '_id',
173 'group_name',
174 'group_desc',
175 'uid',
176 'members',
177 'type'
178 ]);
179 let username = this.getUsername();
180 yapi.commons.saveLog({
181 content: `<a href="/user/profile/${this.getUid()}">${username}</a> 新增了分组 <a href="/group/${
182 result._id
183 }">${params.group_name}</a>`,

Callers 15

aFunction · 0.45
AtFunction · 0.45
OtFunction · 0.45
FtFunction · 0.45
ItFunction · 0.45
xnFunction · 0.45
RnFunction · 0.45
erFunction · 0.45
drFunction · 0.45
CrFunction · 0.45
XrFunction · 0.45
rFunction · 0.45

Calls 5

getUserdataMethod · 0.95
getUsernameMethod · 0.80
getUidMethod · 0.45
checkRepeatMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected