12345678910111213141516171819202122232425 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- //
- class RelationStudentCoachService extends CrudService {
- constructor(ctx) {
- super(ctx, 'relationstudentcoach');
- this.model = this.ctx.model.Relation.RelationStudentCoach;
- }
- // 创建关系前,先查下有没有,有就别创建了
- async beforeCreate(data) {
- const { coach_id, student_id, school_id } = data;
- const num = await this.model.count({ student_id, coach_id, school_id });
- if (num <= 0) {
- return data;
- }
- throw new BusinessError(ErrorCode.DATA_EXISTED, '教练已在该学校有与学生的关联,无需重复添加');
- }
- }
- module.exports = RelationStudentCoachService;
|