/// <summary> /// Remove teacher from course /// NOTE better use DELETE to courses/{courseId}/teachers/{teachersId} /// it is more RESTful /// NOTE this is the old method /// </summary> /// <param name="courseId"></param> /// <param name="teacherId"></param> /// <returns></returns> public Teaching RemoveTeacherFromCourse(int courseId, int teacherId) { // 4 errors: // 1) courseId not found // 2) teacherId not found // 3) teaching not found // 4) cannot remove association because it is in use Course course = db.CoursesRepository.GetByID(courseId); if (course == null) { logger.Error("Course {@courseId} not found", courseId); var ex = new CourseNotFoundException(String.Format("Course {0} not found", courseId)); ex.Data.Add("courseId", courseId); throw ex; } TeacherUser teacher = db.TeachersRepository.Get(t => t.Id == teacherId).FirstOrDefault(); if (teacher == null) { logger.Error("Teacher {@teacherId} not found", teacherId); var ex = new TeacherNotFoundException(String.Format("Teacher {0} not found", courseId)); ex.Data.Add("teacherId", teacherId); throw ex; } // Maybe we don't need to check Course and Teacher at all? Teaching teaching = db.TeachingAssignmentsRepository.Get(ta => ta.CourseId == courseId && ta.TeacherId == teacherId).FirstOrDefault(); if (teaching == null) { logger.Error("Teaching assignment for teacher {@teacherId} and course {@courseId} not found", teacherId, courseId); var ex = new TeachingNotFoundException(String.Format("Teaching assignment for teacher {0} and course {1} not found", teacherId, courseId)); ex.Data.Add("teacherId", teacherId); ex.Data.Add("courseId", courseId); throw ex; } try { // Probably another method in service? db.TeachingAssignmentsRepository.Delete(teaching); db.Save(); } catch (Exception ex) { logger.Error(ex, "Removal of teaching assignment failed for teacher {@teacherId} and course {@courseId}", teacherId, courseId); throw; } return(null); }
/// <summary> /// Get a teacher by Id /// </summary> /// <param name="teacherId"></param> /// <returns></returns> public TeacherUser GetTeacherById(int teacherId) { TeacherUser teacher = db.TeachersRepository.Get(t => t.Id == teacherId).FirstOrDefault(); if (teacher == null) { logger.Info("Teacher {@teacherId} not found", teacherId); var ex = new TeacherNotFoundException(string.Format("Teacher {0} not found", teacherId)); ex.Data.Add("teacherId", teacherId); throw ex; } return(teacher); }