yapi/server_dist/controllers/group.js

381 lines
14 KiB
JavaScript
Raw Normal View History

'use strict';
2017-07-04 17:19:20 +08:00
var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
2017-07-05 17:47:51 +08:00
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
2017-07-05 17:47:51 +08:00
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _group = require('../models/group.js');
var _group2 = _interopRequireDefault(_group);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
2017-07-05 17:47:51 +08:00
var _base = require('./base.js');
var _base2 = _interopRequireDefault(_base);
2017-07-10 20:02:44 +08:00
var _project = require('../models/project.js');
var _project2 = _interopRequireDefault(_project);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2017-07-06 20:53:26 +08:00
//
2017-07-05 17:47:51 +08:00
var groupController = function (_baseController) {
(0, _inherits3.default)(groupController, _baseController);
2017-07-06 19:21:54 +08:00
function groupController(ctx) {
(0, _classCallCheck3.default)(this, groupController);
2017-07-14 17:00:30 +08:00
return (0, _possibleConstructorReturn3.default)(this, (groupController.__proto__ || (0, _getPrototypeOf2.default)(groupController)).call(this, ctx));
}
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
*/
(0, _createClass3.default)(groupController, [{
key: 'add',
value: function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
var params, groupInst, checkRepeat, data, result;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
params = ctx.request.body;
2017-07-26 14:22:59 +08:00
params = _yapi2.default.commons.handleParams(params, {
group_name: 'string',
group_desc: 'string'
});
2017-07-19 19:38:48 +08:00
if (!(this.getRole() !== 'admin')) {
2017-07-26 14:22:59 +08:00
_context.next = 4;
break;
}
2017-07-19 19:38:48 +08:00
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '没有权限'));
2017-07-26 14:22:59 +08:00
case 4:
2017-07-19 19:38:48 +08:00
if (params.group_name) {
2017-07-26 14:22:59 +08:00
_context.next = 6;
2017-07-19 19:38:48 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目分组名不能为空'));
2017-07-26 14:22:59 +08:00
case 6:
groupInst = _yapi2.default.getInst(_group2.default);
2017-07-26 14:22:59 +08:00
_context.next = 9;
return groupInst.checkRepeat(params.group_name);
2017-07-26 14:22:59 +08:00
case 9:
checkRepeat = _context.sent;
if (!(checkRepeat > 0)) {
2017-07-26 14:22:59 +08:00
_context.next = 12;
break;
}
2017-07-06 19:21:54 +08:00
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '项目分组名已存在'));
2017-07-26 14:22:59 +08:00
case 12:
data = {
group_name: params.group_name,
group_desc: params.group_desc,
2017-07-26 14:22:59 +08:00
uid: this.getUid(),
add_time: _yapi2.default.commons.time(),
up_time: _yapi2.default.commons.time()
};
2017-07-26 14:22:59 +08:00
_context.prev = 13;
_context.next = 16;
return groupInst.save(data);
2017-07-26 14:22:59 +08:00
case 16:
result = _context.sent;
result = _yapi2.default.commons.fieldSelect(result, ['_id', 'group_name', 'group_desc', 'uid']);
ctx.body = _yapi2.default.commons.resReturn(result);
2017-07-26 14:22:59 +08:00
_context.next = 24;
break;
2017-07-26 14:22:59 +08:00
case 21:
_context.prev = 21;
_context.t0 = _context['catch'](13);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t0.message);
2017-07-26 14:22:59 +08:00
case 24:
case 'end':
return _context.stop();
}
}
2017-07-26 14:22:59 +08:00
}, _callee, this, [[13, 21]]);
}));
function add(_x) {
return _ref.apply(this, arguments);
}
return add;
}()
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
*/
}, {
key: 'list',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) {
var groupInst, result;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
groupInst = _yapi2.default.getInst(_group2.default);
_context2.next = 4;
return groupInst.list();
case 4:
result = _context2.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context2.next = 11;
break;
case 8:
_context2.prev = 8;
_context2.t0 = _context2['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context2.t0.message);
case 11:
case 'end':
return _context2.stop();
}
}
}, _callee2, this, [[0, 8]]);
}));
function list(_x2) {
return _ref2.apply(this, arguments);
}
return list;
}()
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
*/
}, {
key: 'del',
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) {
2017-07-10 20:02:44 +08:00
var groupInst, projectInst, id, count, result;
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
2017-07-19 19:38:48 +08:00
if (!(this.getRole() !== 'admin')) {
_context3.next = 2;
break;
}
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '没有权限'));
case 2:
_context3.prev = 2;
groupInst = _yapi2.default.getInst(_group2.default);
2017-07-10 20:02:44 +08:00
projectInst = _yapi2.default.getInst(_project2.default);
id = ctx.request.body.id;
2017-07-10 20:02:44 +08:00
if (id) {
2017-07-19 19:38:48 +08:00
_context3.next = 8;
2017-07-10 20:02:44 +08:00
break;
}
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 402, 'id不能为空'));
2017-07-19 19:38:48 +08:00
case 8:
2017-07-19 20:01:29 +08:00
_context3.next = 10;
return projectInst.countByGroupId(id);
case 10:
count = _context3.sent;
2017-07-10 20:02:44 +08:00
if (!(count > 0)) {
2017-07-19 20:01:29 +08:00
_context3.next = 13;
2017-07-10 20:02:44 +08:00
break;
}
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 403, '请先删除该分组下的项目'));
2017-07-19 20:01:29 +08:00
case 13:
_context3.next = 15;
return groupInst.del(id);
2017-07-19 20:01:29 +08:00
case 15:
result = _context3.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
2017-07-19 20:01:29 +08:00
_context3.next = 22;
break;
2017-07-19 20:01:29 +08:00
case 19:
_context3.prev = 19;
2017-07-19 19:38:48 +08:00
_context3.t0 = _context3['catch'](2);
ctx.body = _yapi2.default.commons.resReturn(null, 402, e.message);
2017-07-19 20:01:29 +08:00
case 22:
case 'end':
return _context3.stop();
}
}
2017-07-19 20:01:29 +08:00
}, _callee3, this, [[2, 19]]);
}));
function del(_x3) {
return _ref3.apply(this, arguments);
}
return del;
}()
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
*/
}, {
key: 'up',
value: function () {
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(ctx) {
var groupInst, id, data, result;
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
2017-07-19 19:38:48 +08:00
if (!(this.getRole() !== 'admin')) {
_context4.next = 2;
break;
}
return _context4.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '没有权限'));
case 2:
_context4.prev = 2;
2017-07-26 14:22:59 +08:00
ctx.request.body = _yapi2.default.commons.handleParams(ctx.request.body, {
id: 'number',
group_name: 'string',
group_desc: 'string'
});
groupInst = _yapi2.default.getInst(_group2.default);
id = ctx.request.body.id;
data = {};
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 ((0, _keys2.default)(data).length === 0) {
2017-07-06 19:21:54 +08:00
ctx.body = _yapi2.default.commons.resReturn(null, 404, '分组名和分组描述不能为空');
}
2017-07-26 14:22:59 +08:00
_context4.next = 12;
return groupInst.up(id, data);
2017-07-26 14:22:59 +08:00
case 12:
result = _context4.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
2017-07-26 14:22:59 +08:00
_context4.next = 19;
break;
2017-07-26 14:22:59 +08:00
case 16:
_context4.prev = 16;
2017-07-19 19:38:48 +08:00
_context4.t0 = _context4['catch'](2);
ctx.body = _yapi2.default.commons.resReturn(null, 402, e.message);
2017-07-26 14:22:59 +08:00
case 19:
case 'end':
return _context4.stop();
}
}
2017-07-26 14:22:59 +08:00
}, _callee4, this, [[2, 16]]);
}));
function up(_x4) {
return _ref4.apply(this, arguments);
}
return up;
}()
}]);
return groupController;
2017-07-05 17:47:51 +08:00
}(_base2.default);
module.exports = groupController;