/// <summary> /// Replaces the course with the given id's StartDate and EndDate with the given information /// If course is not found exception is thrown. /// </summary> /// <param name="id">The ID of the course to update</param> /// <param name="model">The course object to update with</param> public CourseDTO UpdateCourse(int id, UpdateCourseViewModel model) { // Finds the course asked for var course = _db.Courses.SingleOrDefault(x => x.ID == id); if (course == null) { throw new AppObjectNotFoundException(); } course.StartDate = model.StartDate; course.EndDate = model.EndDate; _db.SaveChanges(); // Check if there is a template available for the course var templateId = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == course.TemplateID); if (templateId == null) { throw new AppServerErrorException(); } // Check how many students are enrolled in the class var studentCount = (from cr in _db.CourseRegistrations where course.ID == cr.ID select cr.StudentID).ToList().Count(); // Construct a new CourseDTO to return to the client var updatedCourse = new CourseDTO { ID = course.ID, EndDate = course.EndDate, StartDate = course.StartDate, Name = templateId.Name, Semester = course.Semester, StudentCount = studentCount }; return updatedCourse; }
public IHttpActionResult UpdateCourse(int id, UpdateCourseViewModel model) { if (ModelState.IsValid) { try { var result = _service.UpdateCourse(id, model); return Content(HttpStatusCode.Created, result); } catch (AppObjectNotFoundException) { //return 404 return NotFound(); } catch (AppServerErrorException) { //return 500 return InternalServerError(); } } else { return StatusCode(HttpStatusCode.PreconditionFailed); } }