/// <summary>
        /// Method that updates a course with a given ID.
        /// The attributes that are updated are given with a view model class.
        /// </summary>
        /// <param name="id">ID of the course</param>
        /// <param name="model">Update course view model (ViewModel class)</param>
        /// <returns>The updated course (DTO class)</returns>
        public CourseDetailsDTO UpdateCourse(int id, UpdateCourseViewModel model)
        {
            var course = _db.Courses.SingleOrDefault(x => x.ID == id);
            if (course == null)
            {
                throw new AppObjectNotFoundException();
            }

            var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == course.TemplateID);
            if (courseTemplate == null)
            {
                throw new AppInternalServerException();
            }

            course.StartDate = model.StartDate;
            course.EndDate = model.EndDate;
            _db.SaveChanges();

            var students = GetStudentsInCourse(id);

            var result = new CourseDetailsDTO
            {
                ID = course.ID,
                Name = courseTemplate.Name,
                TemplateID = courseTemplate.TemplateID,
                StartDate = course.StartDate,
                EndDate = course.EndDate,
                StudentCount = students.Count,
                Students = students,
                Semester = course.Semester
            };

            return result;
        }
Example #2
0
 public IHttpActionResult UpdateCourse(int id, UpdateCourseViewModel model)
 {
     try
     {
         return Ok(_service.UpdateCourse(id, model));
     }
     catch (AppObjectNotFoundException)
     {
         throw new HttpResponseException(HttpStatusCode.NotFound);
     }
     catch (AppInternalServerException)
     {
         throw new HttpResponseException(HttpStatusCode.InternalServerError);
     }
 }