public IHttpActionResult UpdateCourse(int ID, CourseUpdateViewModel courseVM) { if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); } bool updateSuccessful = _service.UpdateCourse(ID, courseVM); if (!updateSuccessful) { return NotFound(); } return Ok(); //Skila líklega Ok(object) eða content(StatusCode.Ok, CourseDTO); }
public IHttpActionResult UpdateCourse(int ID, CourseUpdateViewModel courseVM) { if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); } CourseDTO courseDTO = null; try { courseDTO = _service.UpdateCourse(ID, courseVM); } catch (AppObjectNotFoundException) { return NotFound(); } return Ok(courseDTO); }
/// <summary> /// Updates the given course. /// </summary> /// <param name="ID">The ID of the course to update.</param> /// <param name="courseVM">The course update viewmodel.</param> /// <returns>Returns true if the update was successful, otherwise false (if not found).</returns> public bool UpdateCourse(int ID, CourseUpdateViewModel courseVM) { Course courseToUpdate = _context.Courses.Where(c => c.ID == ID).SingleOrDefault(); if (courseToUpdate == null) return false; courseToUpdate.StartDate = courseVM.StartDate; courseToUpdate.EndDate = courseVM.EndDate; _context.SaveChanges(); return true; }
/// <summary> /// Updates the given course. /// </summary> /// <exception cref="AppObjectNotFoundException">Thrown if we can't find the course.</exception> /// <param name="ID">The ID of the course to update.</param> /// <param name="courseVM">The course update viewmodel.</param> /// <returns>Returns a course DTO for the updated course.</returns> public CourseDTO UpdateCourse(int ID, CourseUpdateViewModel courseVM) { Course courseToUpdate = _context.Courses.Where(c => c.ID == ID).SingleOrDefault(); if (courseToUpdate == null) throw new AppObjectNotFoundException(); courseToUpdate.StartDate = courseVM.StartDate; courseToUpdate.EndDate = courseVM.EndDate; courseToUpdate.MaxStudents = courseVM.MaxStudents; _context.SaveChanges(); //Create the return object string courseName = _context.CourseTemplates.Where(c => c.TemplateID == courseToUpdate.TemplateID).Select(c => c.Name).SingleOrDefault(); CourseDTO courseDTO = new CourseDTO { ID = courseToUpdate.ID, Name = courseName, MaxStudents = courseToUpdate.MaxStudents, Semester = courseToUpdate.Semester, StartDate = courseToUpdate.StartDate, StudentCount = _context.StudentEnrollment.Count(se => se.CourseID == courseToUpdate.ID) }; return courseDTO; }