comment.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. }
  13. async query({ skip, limit, ...info }) {
  14. const total = await (await this.model.find(info)).length;
  15. const comments = await this.model
  16. .find(info)
  17. .skip(Number(skip))
  18. .limit(Number(limit));
  19. const newdatas = [];
  20. for (let comment of comments) {
  21. comment = JSON.parse(JSON.stringify(comment));
  22. const user = await this.umodel.findById(comment.uid);
  23. comment.uname = user.name;
  24. newdatas.push(comment);
  25. }
  26. return { data: newdatas, total };
  27. }
  28. async fetch({ id }) {
  29. let comment = await this.model.findById(id);
  30. comment = JSON.parse(JSON.stringify(comment));
  31. const user = await this.umodel.findById(comment.uid);
  32. comment.uname = user.name;
  33. return comment;
  34. }
  35. }
  36. module.exports = CommentService;