demand.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 DemandService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'demand');
  10. this.model = this.ctx.model.Demand;
  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 demands = await this.model
  16. .find(info)
  17. .skip(Number(skip))
  18. .limit(Number(limit));
  19. const newdatas = [];
  20. for (let demand of demands) {
  21. demand = JSON.parse(JSON.stringify(demand));
  22. const user = await this.umodel.findById(demand.uid);
  23. demand.uname = user.name;
  24. newdatas.push(demand);
  25. }
  26. return { data: newdatas, total };
  27. }
  28. async fetch({ id }) {
  29. let demand = await this.model.findById(id);
  30. demand = JSON.parse(JSON.stringify(demand));
  31. const user = await this.umodel.findById(demand.uid);
  32. demand.uname = user.name;
  33. return demand;
  34. }
  35. }
  36. module.exports = DemandService;