/// <summary>
        /// Adds the course to the list of courses.
        /// If an entry for the given CourseID does not exist 
        /// in the course template list, an object not found exception is thrown.
        /// </summary>
        /// <param name="course"></param>
        /// <returns></returns>
        public CourseDetailsDTO AddCourse(AddCourseViewModel course)
        {
            // 1. Validation
            var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.CourseID == course.CourseID);

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

            // 2. Create the database object
            var courseEntity = new Course
            {
                CourseID  = course.CourseID,
                StartDate = course.StartDate,
                EndDate   = course.EndDate,
                Semester  = course.Semester
            };

            _db.Courses.Add(courseEntity);
            _db.SaveChanges();

            // 3. Create the return object
            var createdCourseEntity = _db.Courses.SingleOrDefault(x => x.CourseID == course.CourseID && x.Semester == course.Semester);

            CourseDetailsDTO courseDTO = new CourseDetailsDTO
            {
                ID        = createdCourseEntity.ID,
                CourseID  = course.CourseID,
                StartDate = course.StartDate,
                EndDate   = course.EndDate,
                Name      = courseTemplate.Name,
                Students  = null
            };

            return courseDTO;
        }
        /// <summary>
        /// Returns a single course with the specified ID
        /// If no course is found then a course not found exception is thrown
        /// </summary>
        /// <param name="courseModel"></param>
        /// <returns></returns>
        public CourseDetailsDTO GetCourseByID(int ID)
        {
            var course = _db.Courses.SingleOrDefault(x => x.ID == ID);

            // 1. Validation
            if(course == null)
            {
                throw new CourseNotFoundException();
            }

            var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.CourseID == course.CourseID);
            if(courseTemplate == null)
            {
                throw new ApplicationException("Something went horribly wrong");
            }

            // Populating the list of students enrolled in the course
            var students = (from c in _db.CourseRegistrations
                            from p in _db.Persons
                            where c.CourseID == course.ID
                            where c.StudentID == p.ID
                            select new StudentDTO
                            {
                                ID   = c.StudentID,
                                Name = p.Name,
                                SSN  = p.SSN
                            }).ToList();

            var returnValue = new CourseDetailsDTO
            {
                ID        = course.ID,
                CourseID  = course.CourseID,
                Name      = courseTemplate.Name,
                StartDate = course.StartDate,
                EndDate   = course.EndDate,
                Students  = students
            };

            return returnValue;
        }