bannner.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const assert = require('assert');
  4. const moment = require('moment');
  5. class ContentService extends Service {
  6. async create({ title, shortTitle, slug, path, annex, content, istop, columnList }) {
  7. assert(title, '标题不存在');
  8. assert(shortTitle, '短标题不存在');
  9. assert(slug, '摘要不存在');
  10. assert(path, '缩略图不存在');
  11. assert(istop, '置顶不存在');
  12. assert(content, '内容不存在');
  13. const { Content: model } = this.ctx.model;
  14. const createAt = moment().format('x');
  15. try {
  16. await model.create({ title, shortTitle, slug, path, annex, content, istop, columnList, createAt });
  17. return { errmsg: '', errcode: 0 };
  18. } catch (error) {
  19. throw new Error({ errcode: -2001, errmsg: '添加失败' });
  20. }
  21. }
  22. async update({ title, shortTitle, slug, path, annex, content, istop, columnList, id }) {
  23. assert(id, 'id不存在');
  24. const { Content: model } = this.ctx.model;
  25. try {
  26. await model.findByIdAndUpdate(id, { title, shortTitle, slug, path, annex, content, istop, columnList });
  27. return { errmsg: '', errcode: 0 };
  28. } catch (error) {
  29. throw new Error({ errcode: -2001, errmsg: '修改失败' });
  30. }
  31. }
  32. async del({ id }) {
  33. assert(id, 'id不存在');
  34. const { Content: model } = this.ctx.model;
  35. try {
  36. await model.findByIdAndDelete(id);
  37. return { errmsg: '', errcode: 0 };
  38. } catch (error) {
  39. throw new Error({ errcode: -2001, errmsg: '删除失败' });
  40. }
  41. }
  42. async details({ id }) {
  43. assert(id, 'id不存在');
  44. const { Content: model } = this.ctx.model;
  45. try {
  46. const res = await model.findById(id);
  47. return { errmsg: '', errcode: 0, data: res };
  48. } catch (error) {
  49. throw new Error({ errcode: -2001, errmsg: '删除失败' });
  50. }
  51. }
  52. async query({ skip, limit }) {
  53. const { Content: model } = this.ctx.model;
  54. try {
  55. let res;
  56. if (skip && limit) {
  57. res = await model.find({ content: false }).skip(skip * limit).limit(limit);
  58. } else {
  59. res = await model.find({ content: false });
  60. }
  61. return { errmsg: '', errcode: 0, data: res };
  62. } catch (error) {
  63. throw new Error({ errcode: -2001, errmsg: '查询失败' });
  64. }
  65. }
  66. }
  67. module.exports = ContentService;