intact.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. const assert = require('assert');
  3. const Service = require('egg').Service;
  4. class LntactService extends Service {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.model = this.ctx.model.Intact;
  8. }
  9. async create({ thumbnail, name, describe, status, url, date, column, type }) {
  10. assert(name, '名称不存在');
  11. assert(date, '发布时间不存在');
  12. assert(status, '状态不存在');
  13. try {
  14. const res = await this.model.create({ thumbnail, name, describe, status, url, date, column, type });
  15. return { errcode: 0, errmsg: 'ok', data: res };
  16. } catch (error) {
  17. throw error;
  18. }
  19. }
  20. async update({ id, thumbnail, name, describe, status, url, date, column, type }) {
  21. assert(id, 'id不存在');
  22. try {
  23. await this.model.updateOne({ _id: id }, { thumbnail, name, describe, status, url, date, column, type });
  24. return { errcode: 0, errmsg: 'ok', data: '' };
  25. } catch (error) {
  26. throw error;
  27. }
  28. }
  29. async delete({ id }) {
  30. assert(id, 'id不存在');
  31. try {
  32. await this.model.deleteOne({ _id: id });
  33. return { errcode: 0, errmsg: 'ok', data: '' };
  34. } catch (error) {
  35. throw error;
  36. }
  37. }
  38. async query({ skip, limit, name, status, url, date, column, type }) {
  39. const filter = {};
  40. const arr = { name, status, url, date, column, type };
  41. for (const e in arr) {
  42. const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
  43. if (arr[e]) {
  44. filter.$or = [];
  45. filter.$or.push(JSON.parse(data));
  46. }
  47. }
  48. try {
  49. const total = await this.model.find({ ...filter });
  50. let res;
  51. if (skip && limit) {
  52. res = await this.model.find({ ...filter }, { url: false }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  53. } else {
  54. res = await this.model.find({ ...filter }, { url: false });
  55. }
  56. return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
  57. } catch (error) {
  58. throw error;
  59. }
  60. }
  61. async fetch({ id }) {
  62. assert(id, 'id不存在');
  63. try {
  64. const res = await this.model.findOne({ _id: id });
  65. return { errcode: 0, errmsg: 'ok', data: res };
  66. } catch (error) {
  67. throw error;
  68. }
  69. }
  70. }
  71. module.exports = LntactService;