column.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, columnname, picurl, description, inMenu, showModes, isAudit, issuer }) {
  13. // 检查数据
  14. assert(_.isString(site), 'site不能为空');
  15. assert(_.isString(moudle), 'moudle不能为空');
  16. assert(_.isString(columnname), 'columnname不能为空');
  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(!issuer || _.isString(issuer), 'issuer必须为字符串');
  23. // TODO: 检查用户信息
  24. const userid = this.ctx.userid;
  25. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  26. // TODO:保存数据
  27. const data = {
  28. site, moudle, columnname, picurl, description, inMenu, showModes, isAudit, issuer,
  29. meta: { createdBy: userid },
  30. };
  31. const res = await this.model.create(data);
  32. return res;
  33. }
  34. async update({ id }, payload) {
  35. // 检查数据
  36. const { moudle, columnname, picurl, description, inMenu, showModes, isAudit, issuer } = payload;
  37. assert(id, 'id不能为空');
  38. // assert(_.isString(site), 'site不能为空');
  39. assert(_.isString(moudle), 'moudle不能为空');
  40. assert(_.isString(columnname), 'columnname不能为空');
  41. assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
  42. assert(!description || _.isString(description), 'description必须为字符串');
  43. assert(!inMenu || _.isString(inMenu), 'inMenu必须为字符串');
  44. assert(!showModes || _.isString(showModes), 'showModes必须为字符串');
  45. assert(!isAudit || _.isString(isAudit), 'isAudit必须为字符串');
  46. assert(!issuer || _.isString(issuer), 'issuer必须为字符串');
  47. // TODO: 检查用户信息
  48. const userid = this.ctx.userid;
  49. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  50. // TODO:检查数据是否存在
  51. const doc = await this.model.findById(id).exec();
  52. if (isNullOrUndefined(doc)) {
  53. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  54. }
  55. // TODO:保存数据
  56. const data = trimData(payload);
  57. await this.model.findByIdAndUpdate(doc.id, { ...data, 'meta.updatedBy': userid }, { new: true }).exec();
  58. const res = this.model.findById(id, '+columnname').exec();
  59. return res;
  60. }
  61. async status({ id, state }) {
  62. // TODO: 检查数据状态
  63. const doc = await this.model.findById(id).exec();
  64. if (!doc) {
  65. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  66. }
  67. doc.meta.state = state;
  68. return await doc.save();
  69. }
  70. async delete({ id }) {
  71. return await this.status({ id, state: 1 });
  72. }
  73. async restore({ id }) {
  74. return await this.status({ id, state: 0 });
  75. }
  76. }
  77. module.exports = ColumnService;