123456789101112131415161718192021222324252627282930313233343536373839404142 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class CommentService extends CrudService {
- constructor(ctx) {
- super(ctx, "comment");
- this.model = this.ctx.model.Comment;
- this.umodel = this.ctx.model.User;
- }
- async query({ skip, limit, ...info }) {
- const total = await (await this.model.find(info)).length;
- const comments = await this.model
- .find(info)
- .skip(Number(skip))
- .limit(Number(limit));
- const newdatas = [];
- for (let comment of comments) {
- comment = JSON.parse(JSON.stringify(comment));
- const user = await this.umodel.findById(comment.uid);
- comment.uname = user.name;
- newdatas.push(comment);
- }
- return { data: newdatas, total };
- }
- async fetch({ id }) {
- let comment = await this.model.findById(id);
- comment = JSON.parse(JSON.stringify(comment));
- const user = await this.umodel.findById(comment.uid);
- comment.uname = user.name;
- return comment;
- }
- }
- module.exports = CommentService;
|