/// <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); }
public Task <Result <Course> > GetCourse(Guid courseId) { Result <Course> courseResult; if (_courses.ContainsKey(courseId)) { courseResult = _courses[courseId]; return(Task.FromResult(courseResult)); } courseResult = new CourseNotFoundException(courseId); return(Task.FromResult(courseResult)); }
/// <summary> /// Get a course by Id /// </summary> /// <param name="id"></param> /// <returns></returns> public Course GetCourseById(int id) { Course course = db.CoursesRepository.GetByID(id); if (course == null) { logger.Info("Course {@courseId} not found", id); var ex = new CourseNotFoundException(string.Format("Course {0} not found", id)); ex.Data.Add("courseId", id); throw ex; } return(course); }