question.js 999 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. // 问题
  7. class QuestionService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'question');
  10. this.model = this.ctx.model.Question;
  11. this.questionnaire = this.ctx.model.Questionnaire;
  12. }
  13. /**
  14. * 重写删除函数:因为删除题目同时也需要删除问卷保存该题目的id
  15. * @param {Object} { id } 数据id
  16. */
  17. async delete({ id }) {
  18. const res = await this.questionnaire.find({ questions: id }, '+questions');
  19. if (res.length > 0) {
  20. for (const i of res) {
  21. i.questions = i.questions.filter(f => f !== id);
  22. await i.save();
  23. }
  24. }
  25. await this.model.deleteOne({ _id: id });
  26. }
  27. async getQuestions(ids) {
  28. const res = await this.model.find({ _id: ids });
  29. return res;
  30. }
  31. }
  32. module.exports = QuestionService;