news.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 NewsService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.model = this.ctx.model.News;
  11. }
  12. async create({ site }, { title, pic, content, type, parent_id, parent, attachment, is_use }) {
  13. // 检查数据
  14. assert(_.isString(site), 'site不能为空');
  15. assert(_.isString(title), 'title不能为空');
  16. assert(!pic || _.isString(pic), 'pic必须为字符串');
  17. assert(!content || _.isString(content), 'content必须为字符串');
  18. assert(!type || _.isString(type), 'type必须为字符串');
  19. assert(!parent_id || _.isString(parent_id), 'parent_id必须为字符串');
  20. assert(!parent || _.isString(parent), 'parent必须为字符串');
  21. assert(!attachment || _.isArray(attachment), 'attachment必须为数组');
  22. assert(!is_use || _.isString(is_use), 'is_use必须为字符串');
  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, title, pic, content, type, parent_id, parent, attachment, is_use,
  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 { title, pic, content, type, parent_id, parent, attachment, is_use } = payload;
  37. assert(id, 'id不能为空');
  38. assert(!title || _.isString(title), 'title必须为字符串');
  39. assert(!pic || _.isString(pic), 'pic必须为字符串');
  40. assert(!content || _.isString(content), 'content必须为字符串');
  41. assert(!type || _.isString(type), 'type必须为字符串');
  42. assert(!parent_id || _.isString(parent_id), 'parent_id必须为字符串');
  43. assert(!parent || _.isString(parent), 'parent必须为字符串');
  44. assert(!attachment || _.isArray(attachment), 'attachment必须为数组');
  45. assert(!is_use || _.isString(is_use), 'is_use必须为字符串');
  46. // TODO: 检查用户信息
  47. const userid = this.ctx.userid;
  48. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  49. // TODO:检查数据是否存在
  50. const doc = await this.model.findById(id).exec();
  51. if (isNullOrUndefined(doc)) {
  52. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  53. }
  54. // TODO:保存数据
  55. const data = trimData(payload);
  56. await this.model.findByIdAndUpdate(doc.id, { ...data, 'meta.updatedBy': userid }, { new: true }).exec();
  57. const res = this.model.findById(id, '+content').exec();
  58. return res;
  59. }
  60. async status({ id, state }) {
  61. // TODO: 检查数据状态
  62. const doc = await this.model.findById(id).exec();
  63. if (!doc) {
  64. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  65. }
  66. doc.meta.state = state;
  67. return await doc.save();
  68. }
  69. async delete({ id }) {
  70. return await this.status({ id, state: 1 });
  71. }
  72. async restore({ id }) {
  73. return await this.status({ id, state: 0 });
  74. }
  75. }
  76. module.exports = NewsService;