group.js 962 B

123456789101112131415161718192021222324252627282930313233343536
  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 GroupService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'group');
  10. this.model = this.ctx.model.Group;
  11. }
  12. async insert(data) {
  13. const { groupid, studentid } = data;
  14. const group = await this.model.findById(groupid);
  15. if (group.studentid.includes(studentid)) {
  16. throw new BusinessError(ErrorCode.DATA_EXIST, '您已加入该组,请勿重复操作');
  17. } else {
  18. group.studentid.push(studentid);
  19. await group.save();
  20. }
  21. }
  22. async exit(data) {
  23. const { groupid, studentid } = data;
  24. const group = await this.model.findById(groupid);
  25. group.studentid.remove(studentid);
  26. await group.save();
  27. }
  28. }
  29. module.exports = GroupService;