group.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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, stuid, stuname } = data;
  14. const group = await this.model.findById(groupid);
  15. const stuids = [];
  16. for (const student of group.students) {
  17. stuids.push(student.stuid);
  18. }
  19. if (stuids.includes(stuid)) {
  20. throw new BusinessError(ErrorCode.DATA_EXIST, '您已加入该组,请勿重复操作');
  21. } else {
  22. group.students.push({ stuid, stuname });
  23. await group.save();
  24. }
  25. }
  26. async exit(data) {
  27. const { groupid, stuid } = data;
  28. const group = await this.model.findById(groupid);
  29. const students = group.students;
  30. for (const student of students) {
  31. if (student.stuid === stuid) {
  32. students.remove(student);
  33. }
  34. }
  35. await group.save();
  36. }
  37. }
  38. module.exports = GroupService;