comment.js 909 B

12345678910111213141516171819202122232425262728293031
  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.find(info).skip(Number(skip)).limit(Number(limit));
  16. const newdatas = [];
  17. for (const comment of comments) {
  18. const user = await this.umodel.findById(comment.uid);
  19. comment.uname = user.data.name;
  20. newdatas.push(comment);
  21. }
  22. return { data: newdatas, total };
  23. }
  24. }
  25. module.exports = CommentService;