2017-07-27 14:12:43 +08:00
|
|
|
|
import groupModel from '../models/group.js';
|
|
|
|
|
import yapi from '../yapi.js';
|
|
|
|
|
import baseController from './base.js';
|
|
|
|
|
import projectModel from '../models/project.js';
|
|
|
|
|
|
|
|
|
|
class groupController extends baseController {
|
|
|
|
|
constructor(ctx) {
|
|
|
|
|
super(ctx);
|
2017-07-05 16:17:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 19:21:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* 添加项目分组
|
|
|
|
|
* @interface /group/add
|
|
|
|
|
* @method POST
|
|
|
|
|
* @category group
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @param {String} group_name 项目分组名称,不能为空
|
|
|
|
|
* @param {String} [group_desc] 项目分组描述
|
|
|
|
|
* @returns {Object}
|
2017-07-06 20:53:26 +08:00
|
|
|
|
* @example ./api/group/add.json
|
2017-07-06 19:21:54 +08:00
|
|
|
|
*/
|
2017-07-04 16:43:07 +08:00
|
|
|
|
async add(ctx) {
|
|
|
|
|
let params = ctx.request.body;
|
2017-07-27 14:12:43 +08:00
|
|
|
|
|
2017-07-26 14:22:59 +08:00
|
|
|
|
params = yapi.commons.handleParams(params, {
|
|
|
|
|
group_name: 'string',
|
|
|
|
|
group_desc: 'string'
|
2017-07-27 14:12:43 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (this.getRole() !== 'admin') {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 401, '没有权限');
|
2017-07-19 19:38:48 +08:00
|
|
|
|
}
|
2017-07-27 14:12:43 +08:00
|
|
|
|
|
|
|
|
|
if (!params.group_name) {
|
2017-07-06 19:21:54 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 400, '项目分组名不能为空');
|
2017-07-04 16:43:07 +08:00
|
|
|
|
}
|
2017-07-27 14:12:43 +08:00
|
|
|
|
|
|
|
|
|
let groupInst = yapi.getInst(groupModel);
|
|
|
|
|
|
|
|
|
|
let checkRepeat = await groupInst.checkRepeat(params.group_name);
|
|
|
|
|
|
|
|
|
|
if (checkRepeat > 0) {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 401, '项目分组名已存在');
|
2017-07-04 17:19:20 +08:00
|
|
|
|
}
|
2017-07-27 14:12:43 +08:00
|
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
|
let data = {
|
|
|
|
|
group_name: params.group_name,
|
|
|
|
|
group_desc: params.group_desc,
|
2017-07-26 14:22:59 +08:00
|
|
|
|
uid: this.getUid(),
|
2017-07-04 16:43:07 +08:00
|
|
|
|
add_time: yapi.commons.time(),
|
|
|
|
|
up_time: yapi.commons.time()
|
2017-07-27 14:12:43 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
2017-07-05 16:17:58 +08:00
|
|
|
|
let result = await groupInst.save(data);
|
2017-07-27 14:12:43 +08:00
|
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
|
result = yapi.commons.fieldSelect(result, ['_id', 'group_name', 'group_desc', 'uid']);
|
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
2017-07-27 14:12:43 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
2017-07-04 16:43:07 +08:00
|
|
|
|
}
|
2017-07-27 14:12:43 +08:00
|
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
|
}
|
2017-07-04 16:43:07 +08:00
|
|
|
|
|
2017-07-06 20:53:26 +08:00
|
|
|
|
/**
|
2017-07-10 11:56:53 +08:00
|
|
|
|
* 获取项目分组列表
|
2017-07-06 20:53:26 +08:00
|
|
|
|
* @interface /group/list
|
|
|
|
|
* @method get
|
|
|
|
|
* @category group
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @returns {Object}
|
2017-07-10 11:56:53 +08:00
|
|
|
|
* @example ./api/group/list.json
|
2017-07-06 20:53:26 +08:00
|
|
|
|
*/
|
2017-07-04 16:43:07 +08:00
|
|
|
|
async list(ctx) {
|
2017-07-27 14:12:43 +08:00
|
|
|
|
try {
|
2017-07-05 16:52:48 +08:00
|
|
|
|
var groupInst = yapi.getInst(groupModel);
|
2017-07-05 16:17:58 +08:00
|
|
|
|
let result = await groupInst.list();
|
2017-07-27 14:12:43 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
2017-07-04 17:19:20 +08:00
|
|
|
|
}
|
2017-07-05 16:17:58 +08:00
|
|
|
|
}
|
2017-07-04 16:43:07 +08:00
|
|
|
|
|
2017-07-10 11:56:53 +08:00
|
|
|
|
/**
|
|
|
|
|
* 删除项目分组
|
|
|
|
|
* @interface /group/del
|
|
|
|
|
* @method post
|
|
|
|
|
* @param {String} id 项目分组id
|
|
|
|
|
* @category group
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example ./api/group/del.json
|
|
|
|
|
*/
|
2017-07-27 14:12:43 +08:00
|
|
|
|
async del(ctx) {
|
|
|
|
|
if (this.getRole() !== 'admin') {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 401, '没有权限');
|
|
|
|
|
}
|
2017-07-10 11:56:53 +08:00
|
|
|
|
|
2017-07-27 14:12:43 +08:00
|
|
|
|
try {
|
|
|
|
|
let groupInst = yapi.getInst(groupModel);
|
|
|
|
|
let projectInst = yapi.getInst(projectModel);
|
|
|
|
|
let id = ctx.request.body.id;
|
|
|
|
|
|
|
|
|
|
if (!id) {
|
2017-07-10 20:02:44 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 402, 'id不能为空');
|
|
|
|
|
}
|
2017-07-27 14:12:43 +08:00
|
|
|
|
|
|
|
|
|
let count = await projectInst.countByGroupId(id);
|
|
|
|
|
|
|
|
|
|
if (count > 0) {
|
2017-07-10 20:02:44 +08:00
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 403, '请先删除该分组下的项目');
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
|
let result = await groupInst.del(id);
|
2017-07-27 14:12:43 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, err.message);
|
2017-07-04 17:19:20 +08:00
|
|
|
|
}
|
2017-07-05 16:17:58 +08:00
|
|
|
|
}
|
2017-07-04 16:43:07 +08:00
|
|
|
|
|
2017-07-10 11:56:53 +08:00
|
|
|
|
/**
|
|
|
|
|
* 更新项目分组
|
|
|
|
|
* @interface /group/up
|
|
|
|
|
* @method post
|
|
|
|
|
* @param {String} id 项目分组id
|
|
|
|
|
* @param {String} group_name 项目分组名称
|
|
|
|
|
* @param {String} group_desc 项目分组描述
|
|
|
|
|
* @category group
|
|
|
|
|
* @foldnumber 10
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
* @example ./api/group/up.json
|
|
|
|
|
*/
|
2017-07-27 14:12:43 +08:00
|
|
|
|
async up(ctx) {
|
|
|
|
|
if (this.getRole() !== 'admin') {
|
|
|
|
|
return ctx.body = yapi.commons.resReturn(null, 401, '没有权限');
|
2017-07-19 19:38:48 +08:00
|
|
|
|
}
|
2017-07-27 14:12:43 +08:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
2017-07-26 14:22:59 +08:00
|
|
|
|
ctx.request.body = yapi.commons.handleParams(ctx.request.body, {
|
|
|
|
|
id: 'number',
|
|
|
|
|
group_name: 'string',
|
|
|
|
|
group_desc: 'string'
|
2017-07-27 14:12:43 +08:00
|
|
|
|
});
|
|
|
|
|
let groupInst = yapi.getInst(groupModel);
|
2017-07-04 17:19:20 +08:00
|
|
|
|
let id = ctx.request.body.id;
|
|
|
|
|
let data = {};
|
2017-07-27 14:12:43 +08:00
|
|
|
|
ctx.request.body.group_name && (data.group_name = ctx.request.body.group_name);
|
|
|
|
|
ctx.request.body.group_desc && (data.group_desc = ctx.request.body.group_desc);
|
|
|
|
|
if (Object.keys(data).length === 0) {
|
2017-07-06 19:21:54 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 404, '分组名和分组描述不能为空');
|
2017-07-04 17:19:20 +08:00
|
|
|
|
}
|
2017-07-05 16:17:58 +08:00
|
|
|
|
let result = await groupInst.up(id, data);
|
2017-07-27 14:12:43 +08:00
|
|
|
|
ctx.body = yapi.commons.resReturn(result);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
ctx.body = yapi.commons.resReturn(null, 402, err.message);
|
2017-07-04 17:19:20 +08:00
|
|
|
|
}
|
2017-07-04 16:43:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-05 16:17:58 +08:00
|
|
|
|
|
2017-07-27 14:12:43 +08:00
|
|
|
|
module.exports = groupController;
|