|
@@ -0,0 +1,95 @@
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+const assert = require('assert');
|
|
|
|
+const _ = require('lodash');
|
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
|
+const { isNullOrUndefined, trimData } = require('naf-core').Util;
|
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
|
+
|
|
|
|
+class ColumnService extends CrudService {
|
|
|
|
+ constructor(ctx) {
|
|
|
|
+ super(ctx);
|
|
|
|
+ this.model = this.ctx.model.column;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async create({ site }, { moudle, name, picurl, description, inMenu, showModes, isAudit, sort, issuer }) {
|
|
|
|
+ // 检查数据
|
|
|
|
+ assert(_.isString(site), 'site不能为空');
|
|
|
|
+ assert(_.isString(moudle), 'moudle不能为空');
|
|
|
|
+ assert(_.isString(name), 'name不能为空');
|
|
|
|
+ assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
|
|
|
|
+ assert(!description || _.isString(description), 'description必须为字符串');
|
|
|
|
+ assert(!inMenu || _.isString(inMenu), 'inMenu必须为字符串');
|
|
|
|
+ assert(!showModes || _.isString(showModes), 'showModes必须为字符串');
|
|
|
|
+ assert(!isAudit || _.isString(isAudit), 'isAudit必须为字符串');
|
|
|
|
+ assert(_.isUndefined(sort) || _.isNumber(sort), 'top必须为数字');
|
|
|
|
+ assert(!issuer || _.isString(issuer), 'issuer必须为字符串');
|
|
|
|
+
|
|
|
|
+ // TODO: 检查用户信息
|
|
|
|
+ const userid = this.ctx.userid;
|
|
|
|
+ if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
|
|
|
|
+
|
|
|
|
+ // TODO:保存数据
|
|
|
|
+ const data = {
|
|
|
|
+ moudle, name, picurl, description, inMenu, showModes, isAudit, sort, issuer,
|
|
|
|
+ meta: { createdBy: userid },
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const res = await this.model.create(data);
|
|
|
|
+ return res;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async update({ id }, payload) {
|
|
|
|
+ // 检查数据
|
|
|
|
+ const { moudle, name, picurl, description, inMenu, showModes, isAudit, sort, issuer } = payload;
|
|
|
|
+ assert(id, 'id不能为空');
|
|
|
|
+ // assert(_.isString(site), 'site不能为空');
|
|
|
|
+ assert(_.isString(moudle), 'moudle不能为空');
|
|
|
|
+ assert(_.isString(name), 'name不能为空');
|
|
|
|
+ assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
|
|
|
|
+ assert(!description || _.isString(description), 'description必须为字符串');
|
|
|
|
+ assert(!inMenu || _.isString(inMenu), 'inMenu必须为字符串');
|
|
|
|
+ assert(!showModes || _.isString(showModes), 'showModes必须为字符串');
|
|
|
|
+ assert(!isAudit || _.isString(isAudit), 'isAudit必须为字符串');
|
|
|
|
+ assert(_.isUndefined(sort) || _.isNumber(sort), 'top必须为数字');
|
|
|
|
+ assert(!issuer || _.isString(issuer), 'issuer必须为字符串');
|
|
|
|
+
|
|
|
|
+ // TODO: 检查用户信息
|
|
|
|
+ const userid = this.ctx.userid;
|
|
|
|
+ if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
|
|
|
|
+
|
|
|
|
+ // TODO:检查数据是否存在
|
|
|
|
+ const doc = await this.model.findById(id).exec();
|
|
|
|
+ if (isNullOrUndefined(doc)) {
|
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // TODO:保存数据
|
|
|
|
+ const data = trimData(payload);
|
|
|
|
+ await this.model.findByIdAndUpdate(doc.id, { ...data, 'meta.updatedBy': userid }, { new: true }).exec();
|
|
|
|
+ const res = this.model.findById(id, '+name').exec();
|
|
|
|
+ return res;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async status({ id, state }) {
|
|
|
|
+
|
|
|
|
+ // TODO: 检查数据状态
|
|
|
|
+ const doc = await this.model.findById(id).exec();
|
|
|
|
+ if (!doc) {
|
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ doc.meta.state = state;
|
|
|
|
+ return await doc.save();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async delete({ id }) {
|
|
|
|
+ return await this.status({ id, state: 1 });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async restore({ id }) {
|
|
|
|
+ return await this.status({ id, state: 0 });
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+module.exports = ColumnService;
|