comment.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class CommentService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'comment');
  10. this.model = this.ctx.model.Comment;
  11. this.umodel = this.ctx.model.User;
  12. this.nmodel = this.ctx.model.News;
  13. }
  14. async query({ skip, limit, ...info }) {
  15. const total = await (await this.model.find(info)).length;
  16. const comments = await this.model
  17. .find(info)
  18. .skip(Number(skip))
  19. .limit(Number(limit));
  20. const newdatas = [];
  21. for (let comment of comments) {
  22. comment = JSON.parse(JSON.stringify(comment));
  23. const user = await this.umodel.findById(comment.uid);
  24. const news = await this.nmodel.findById(comment.newsid);
  25. comment.nname = news.title;
  26. comment.uname = user.name;
  27. newdatas.push(comment);
  28. }
  29. return { data: newdatas, total };
  30. }
  31. async fetch({ id }) {
  32. let comment = await this.model.findById(id);
  33. comment = JSON.parse(JSON.stringify(comment));
  34. const user = await this.umodel.findById(comment.uid);
  35. const news = await this.nmodel.findById(comment.newsid);
  36. comment.nname = news.title;
  37. comment.uname = user.name;
  38. return comment;
  39. }
  40. }
  41. module.exports = CommentService;