/// <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 teaching by Id -- throws if not found /// </summary> /// <param name="teachingId"></param> /// <returns></returns> public Teaching GetTeachingById(int teachingId) { logger.Info("Get teaching by Id {@teachingId}", teachingId); var teaching = db.TeachingAssignmentsRepository.Get(ta => ta.Id == teachingId).FirstOrDefault(); if (teaching == null) { // TODO find a way out of this madness, cant be teacher and course in one place and teaching in another place... logger.Info("Teaching not found for Id {@teachingId}", teachingId); var ex = new TeachingNotFoundException(string.Format("Teaching not found for Id {0}", teachingId)); ex.Data.Add("teachingId", teachingId); throw ex; } return(teaching); }
/// <summary> /// Get teaching by course and teacher -- throws /// </summary> /// <param name="courseId"></param> /// <param name="teacherId"></param> /// <returns></returns> public Teaching GetTeaching(int courseId, int teacherId) { Course course = coursesService.Value.GetCourseById(courseId); TeacherUser teacherUser = teachersService.Value.GetTeacherById(teacherId); var teaching = db.TeachingAssignmentsRepository.Get(ta => ta.CourseId == courseId && ta.TeacherId == teacherId).FirstOrDefault(); if (teaching == null) { logger.Info("Teaching not found for course {@courseId} and teacher {@teacherId}", courseId, teacherId); var ex = new TeachingNotFoundException(string.Format("Teaching not found for course {0} and teacher {1}", courseId, teacherId)); ex.Data.Add("teacherId", teacherId); ex.Data.Add("courseId", courseId); throw ex; } return(teaching); }