public IHttpActionResult UpdateCourse(int id, [FromBody] UpdateCourseDetailViewModel course) { if (!ModelState.IsValid) { return(BadRequest("Course model not valid!")); } try { return(Ok(_service.UpdateCourse(id, course))); } catch (NotFoundException) { return(NotFound()); } catch (DbException) { return(InternalServerError()); } }
/// <summary> /// Update the Start and End dates for a single course. Throws NotFound and Db Exceptions. /// </summary> /// <param name="id">The ID of the course to be updated</param> /// <param name="addCourseModel">A valid Update View Model for a course</param> /// <returns>The updated CourseDetailDTO</returns> public CourseDetailDTO UpdateCourse(int id, UpdateCourseDetailViewModel addCourseModel) { var result = _db.Courses.SingleOrDefault(c => c.ID == id); if (result == null) { throw new NotFoundException($"No course with ID: {id}"); } try { result.EndDate = addCourseModel.EndDate; result.StartDate = addCourseModel.StartDate; result.MaxStudents = addCourseModel.MaxStudents; _db.SaveChanges(); } catch (Exception e) { throw new DbException(e); } return(GetCourseById(result.ID)); }