news.js 3.1 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({ column, site }, { title, content, picurl, top, tags, attachment, issuer }) {
  13. // 检查数据
  14. assert(_.isString(column), 'column不能为空');
  15. assert(_.isString(site), 'site不能为空');
  16. assert(_.isString(title), 'title不能为空');
  17. assert(_.isString(content), 'content不能为空');
  18. assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
  19. assert(_.isUndefined(top) || _.isNumber(top), 'top必须为数字');
  20. assert(!tags || _.isArray(tags), 'tags必须为数组');
  21. assert(!attachment || _.isArray(attachment), 'attachment必须为数组');
  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,column, title, content, picurl, top, tags, attachment, 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 { title, content, picurl, top, tags, attachment, issuer } = payload;
  37. assert(id, 'id不能为空');
  38. assert(!title || _.isString(title), 'title必须为字符串');
  39. // assert(_.isString(site), 'site不能为空');
  40. assert(!content || _.isString(content), 'content必须为字符串');
  41. assert(!picurl || _.isString(picurl), 'picurl必须为字符串');
  42. assert(_.isUndefined(top) || _.isNumber(top), 'top必须为数字');
  43. assert(!tags || _.isArray(tags), 'tags必须为数组');
  44. assert(!attachment || _.isArray(attachment), 'attachment必须为数组');
  45. assert(!issuer || _.isString(issuer), 'issuer必须为字符串');
  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;