news.js 3.4 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 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, publish_time,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(!publish_time || _.isString(publish_time), 'publish_time必须为字符串');
  22. assert(!attachment || _.isArray(attachment), 'attachment必须为数组');
  23. assert(!is_use || _.isString(is_use), 'is_use必须为字符串');
  24. // TODO: 检查用户信息
  25. const userid = this.ctx.userid;
  26. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  27. // TODO:保存数据
  28. const data = {
  29. site, title, pic, content, type, parent_id, parent, publish_time, attachment, is_use,
  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 { title, pic, content, type, parent_id, parent, publish_time,attachment, is_use } = payload;
  38. assert(id, 'id不能为空');
  39. assert(!title || _.isString(title), 'title必须为字符串');
  40. assert(!pic || _.isString(pic), 'pic必须为字符串');
  41. assert(!content || _.isString(content), 'content必须为字符串');
  42. assert(!type || _.isString(type), 'type必须为字符串');
  43. assert(!parent_id || _.isString(parent_id), 'parent_id必须为字符串');
  44. assert(!parent || _.isString(parent), 'parent必须为字符串');
  45. assert(!publish_time || _.isString(publish_time), 'publish_time必须为字符串');
  46. assert(!attachment || _.isArray(attachment), 'attachment必须为数组');
  47. assert(!is_use || _.isString(is_use), 'is_use必须为字符串');
  48. // TODO: 检查用户信息
  49. const userid = this.ctx.userid;
  50. if (!_.isString(userid)) throw new BusinessError(ErrorCode.NOT_LOGIN);
  51. // TODO:检查数据是否存在
  52. const doc = await this.model.findById(id).exec();
  53. if (isNullOrUndefined(doc)) {
  54. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  55. }
  56. // TODO:保存数据
  57. const data = trimData(payload);
  58. await this.model.findByIdAndUpdate(doc.id, { ...data, 'meta.updatedBy': userid }, { new: true }).exec();
  59. const res = this.model.findById(id, '+content').exec();
  60. return res;
  61. }
  62. async status({ id, state }) {
  63. // TODO: 检查数据状态
  64. const doc = await this.model.findById(id).exec();
  65. if (!doc) {
  66. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  67. }
  68. doc.meta.state = state;
  69. return await doc.save();
  70. }
  71. async delete({ id }) {
  72. return await this.status({ id, state: 1 });
  73. }
  74. async restore({ id }) {
  75. return await this.status({ id, state: 0 });
  76. }
  77. }
  78. module.exports = NewsService;