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 DemandService extends CrudService {
- constructor(ctx) {
- super(ctx, 'demand');
- this.model = this.ctx.model.Demand;
- this.umodel = this.ctx.model.User;
- }
- async query({ skip, limit, ...info }) {
- const total = await (await this.model.find(info)).length;
- const demands = await this.model
- .find(info)
- .skip(Number(skip))
- .limit(Number(limit));
- const newdatas = [];
- for (let demand of demands) {
- demand = JSON.parse(JSON.stringify(demand));
- const user = await this.umodel.findById(demand.uid);
- demand.uname = user.name;
- newdatas.push(demand);
- }
- return { data: newdatas, total };
- }
- async fetch({ id }) {
- let demand = await this.model.findById(id);
- demand = JSON.parse(JSON.stringify(demand));
- const user = await this.umodel.findById(demand.uid);
- demand.uname = user.name;
- return demand;
- }
- }
- module.exports = DemandService;
|