/// <summary> /// 更新课程 /// </summary> /// <param name="classesIds"></param> /// <param name="courseDto"></param> /// <returns></returns> /// <exception cref="Exception"></exception> public async Task <CourseDto> UpdateCourse(int[] classesIds, CourseDto courseDto) { var course = await _courseRepository .Entities .FirstOrDefaultAsync(x => x.Id == courseDto.Id); if (course == null) { throw Oops.Oh( CourseErrorCodes.c1501 ); } // 查询系别是否存在 var dept = await _courseRepository.Change <TbDept>().Entities .FirstOrDefaultAsync(x => x.Id == course.DeptId); if (dept == null) { throw Oops.Oh(DeptErrorCodes.d1301); } // 查询教师是否存在 var teacher = await _courseRepository.Change <TbTeacher>().Entities .FirstOrDefaultAsync(x => x.Id == course.TeacherId); if (teacher == null) { throw Oops.Oh(TeacherErrorCodes.t1402); } //删除关联表 await _courseRepository .Change <TbCourseclass>() .DeleteAsync( _courseRepository .Change <TbCourseclass>() .Where(u => u.CourseId == course.Id, false).ToList() ); // 重新为课程分配班级 var courseClasses = new List <TbCourseclass>(); foreach (var item in classesIds) { courseClasses.Add(new TbCourseclass { CourseId = course.Id, ClassesId = item }); } await _courseRepository.Change <TbCourseclass>().InsertAsync(courseClasses); var changeCourse = courseDto.Adapt(course); await changeCourse .UpdateExcludeAsync(u => u.CreateTime); return(changeCourse.Adapt <CourseDto>()); }
/// <summary> /// 创建课程 /// </summary> /// <param name="classesIds"></param> /// <param name="courseDto"></param> /// <returns></returns> public async Task <CourseDto> CreateCourse(int[] classesIds, CourseDto courseDto) { // 查询系别是否存在 var dept = await _courseRepository.Change <TbDept>().Entities .FirstOrDefaultAsync(x => x.Id == courseDto.DeptId); if (dept == null) { throw Oops.Oh(DeptErrorCodes.d1301); } // 查询教师是否存在 var teacher = await _courseRepository.Change <TbTeacher>().Entities .FirstOrDefaultAsync(x => x.Id == courseDto.TeacherId); if (teacher == null) { throw Oops.Oh(TeacherErrorCodes.t1402); } // courseDto.CreateTime = TimeUtil.GetTimeStampNow(); var course = await _courseRepository .InsertNowAsync(courseDto.Adapt <TbCourse >()); // 为课程分配班级 var courseClasses = new List <TbCourseclass>(); foreach (var item in classesIds) { courseClasses.Add(new TbCourseclass { CourseId = course.Entity.Id, ClassesId = item }); } await _courseRepository.Change <TbCourseclass>().InsertAsync(courseClasses); return(course.Entity.Adapt <CourseDto>()); }