/// <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;
        }
        /// <summary>
        /// Adds a course with a pre existing CourseTemplate to the database,
        /// If there is no template available an exception is thrown,
        /// Or if the course object is not valid an exception is thrown
        /// </summary>
        /// <param name="course">A course object containing the information to create the new course</param>
        /// <returns>The course object</returns>
        public CourseDTO AddCourse(AddCourseViewModel course)
        {
            //Check if there is a template for the course
            var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == course.TemplateID);

            if (courseTemplate == null)
            {
                throw new AppObjectNotFoundException();
            }

            //Create a new Course object to add to the database
            var newCourse = new Course
            {
                EndDate = course.EndDate,
                StartDate = course.StartDate,
                TemplateID = course.TemplateID,
                MaxStudents = course.MaxStudents,
                Semester = course.Semester

            };

            if (newCourse == null)
            {
                throw new AppBadRequestException();
            }

            //Add the new course to the database
            _db.Courses.Add(newCourse);
            _db.SaveChanges();

            // Get the course object for the ID of the returned CourseDTO
            var courseId = _db.Courses.SingleOrDefault(x => x.ID == newCourse.ID);

            if (courseId == null)
            {
                // If the course is not found, there are inconsistancies
                // in the database:
                throw new AppServerErrorException();
            }

            var studentCount = (from cr in _db.CourseRegistrations
                               where courseId.ID == cr.ID
                               where cr.Active
                               select cr.StudentID).ToList().Count();

            //Make a new CourseDTO to return to the client
            var courseDto = new CourseDTO
            {
                ID = courseId.ID,
                StartDate = course.StartDate,
                EndDate = course.EndDate,
                Name = courseTemplate.Name,
                Semester = course.Semester,
                StudentCount = studentCount
            };

            if (courseDto == null)
            {
                throw new AppBadRequestException();
            }

            return courseDto;
        }