column.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { BusinessError, ErrorCode } = require('naf-core').Error;
  5. const { isNullOrUndefined, trimData } = require('naf-core').Util;
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. class ColumnService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.model = this.ctx.model.column;
  11. }
  12. async create({ site }, { moudle, name, picurl, description, inMenu, showModes, isAudit, sort, issuer }) {
  13. // 检查数据
  14. assert(_.isString(site), 'site不能为空');
  15. assert(_.isString(moudle), 'moudle不能为空');
  16. assert(_.isString(name), 'name不能为空');
  17. assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
  18. assert(!description || _.isString(description), 'description必须为字符串');
  19. assert(!inMenu || _.isString(inMenu), 'inMenu必须为字符串');
  20. assert(!showModes || _.isString(showModes), 'showModes必须为字符串');
  21. assert(!isAudit || _.isString(isAudit), 'isAudit必须为字符串');
  22. assert(_.isUndefined(sort) || _.isNumber(sort), 'top必须为数字');
  23. assert(!issuer || _.isString(issuer), 'issuer必须为字符串');
  24. // TODO: 检查用户信息
  25. const userid = this.ctx.userid;
  26. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  27. // TODO:保存数据
  28. const data = {
  29. moudle, name, picurl, description, inMenu, showModes, isAudit, sort, issuer,
  30. meta: { createdBy: userid },
  31. };
  32. const res = await this.model.create(data);
  33. return res;
  34. }
  35. async update({ id }, payload) {
  36. // 检查数据
  37. const { moudle, name, picurl, description, inMenu, showModes, isAudit, sort, issuer } = payload;
  38. assert(id, 'id不能为空');
  39. // assert(_.isString(site), 'site不能为空');
  40. assert(_.isString(moudle), 'moudle不能为空');
  41. assert(_.isString(name), 'name不能为空');
  42. assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
  43. assert(!description || _.isString(description), 'description必须为字符串');
  44. assert(!inMenu || _.isString(inMenu), 'inMenu必须为字符串');
  45. assert(!showModes || _.isString(showModes), 'showModes必须为字符串');
  46. assert(!isAudit || _.isString(isAudit), 'isAudit必须为字符串');
  47. assert(_.isUndefined(sort) || _.isNumber(sort), 'top必须为数字');
  48. assert(!issuer || _.isString(issuer), 'issuer必须为字符串');
  49. // TODO: 检查用户信息
  50. const userid = this.ctx.userid;
  51. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  52. // TODO:检查数据是否存在
  53. const doc = await this.model.findById(id).exec();
  54. if (isNullOrUndefined(doc)) {
  55. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  56. }
  57. // TODO:保存数据
  58. const data = trimData(payload);
  59. await this.model.findByIdAndUpdate(doc.id, { ...data, 'meta.updatedBy': userid }, { new: true }).exec();
  60. const res = this.model.findById(id, '+name').exec();
  61. return res;
  62. }
  63. async status({ id, state }) {
  64. // TODO: 检查数据状态
  65. const doc = await this.model.findById(id).exec();
  66. if (!doc) {
  67. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  68. }
  69. doc.meta.state = state;
  70. return await doc.save();
  71. }
  72. async delete({ id }) {
  73. return await this.status({ id, state: 1 });
  74. }
  75. async restore({ id }) {
  76. return await this.status({ id, state: 0 });
  77. }
  78. }
  79. module.exports = ColumnService;