/// <summary>
        /// Adds a student to waiting list of specific course given by ID
        /// </summary>
        /// <param name="id">ID of the course</param>
        /// <param name="model">A model containing the SSN of the student</param>
        /// <returns>A personDTO of the student</returns>
        public StudentDTO AddStudentToWaitingList(int id, AddStudentViewModel model)
        {
            // Validate
            var courseEntity = _db.Courses.SingleOrDefault(x => x.ID == id);
            if (courseEntity == null)
            {
                throw new AppObjectNotFoundException();
            }

            //verify that the person exists!
            var person = _db.Persons.SingleOrDefault(x => x.SSN == model.SSN);
            if (person == null)
            {
                throw new AppObjectNotFoundException();
            }

            //check if student already in course
            var studentAlreadyInCourse = (from cs in _db.CourseStudents
                                          where cs.CourseID == id
                                          && person.ID == cs.PersonID
                                          select cs).SingleOrDefault();

            if (studentAlreadyInCourse != null)
            {
                throw new DuplicateEntryException();
            }

            //make sure person is not currently on the waiting list
            var onWaitingList = (from cwl in _db.CourseWaitingList
                                 where cwl.CourseID == id
                                 && person.ID == cwl.PersonID
                                 select cwl).SingleOrDefault();

            if(onWaitingList != null)
            {
                throw new DuplicateEntryException();
            }

            var waitingEntity = new CourseWaitingList
            {
                CourseID        = id,
                PersonID        = person.ID
            };

            _db.CourseWaitingList.Add(waitingEntity);
            _db.SaveChanges();

            var result = new StudentDTO
            {
                SSN     = model.SSN,
                Name    = person.Name
            };

            return result;
        }
 public IHttpActionResult AddStudentToWaitingList(int id, AddStudentViewModel model)
 {
     if(ModelState.IsValid)
     {
         try
         {
             var result = _service.AddStudentToWaitingList(id, model);
             return Content(HttpStatusCode.OK, result);
         }
         catch (AppObjectNotFoundException)
         {
             return StatusCode(HttpStatusCode.NotFound);
         }
         catch(DuplicateEntryException)
         {
             return StatusCode(HttpStatusCode.PreconditionFailed);
         }
     }
     else
     {
         return StatusCode(HttpStatusCode.PreconditionFailed);
     }
 }
        /// <summary>
        /// Adds a student to a given course
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public StudentDTO AddStudentToCourse(int id, AddStudentViewModel model)
        {
            
            var course = _db.Courses.SingleOrDefault(x => x.ID == id);
            //if course doesn't exist an error will be thrown. Not possible to add student to non existing courses
            if (course == null)
            {
                throw new AppObjectNotFoundException();
            }

            //verify that the person exists!
            var person = _db.Persons.SingleOrDefault(x => x.SSN == model.SSN);
            if(person == null)
            {
                throw new AppObjectNotFoundException();
            }

            //count students in the course
            var countStudents = _db.CourseStudents.Count(x => x.CourseID == course.ID);

            //stop adding new students if course is full
            if(countStudents >= course.MaxStudents)
            {
                throw new MaxStudentException();
            }

            //check if student already in course
            var studentAlreadyInCourse = (from cs in _db.CourseStudents
                                         where cs.CourseID == id
                                         && person.ID == cs.PersonID
                                         select cs).SingleOrDefault();

            if (studentAlreadyInCourse != null)
            {
                throw new DuplicateEntryException();
            }


            //check if person is on the waitinglist
            var isOnWaitList = (from cwl in _db.CourseWaitingList
                                //join p in _db.Persons on cwl.PersonID equals p.ID
                                where cwl.CourseID == id
                                && person.ID == cwl.PersonID
                                select cwl).SingleOrDefault();   

            //person is on the waitinglist
            if(isOnWaitList != null)
            {
                _db.CourseWaitingList.Remove(isOnWaitList);
                _db.SaveChanges();
            }

            //Actually add the record
            var courseStudent = new CourseStudent
            {
                PersonID = person.ID,
                CourseID = course.ID
            };

            _db.CourseStudents.Add(courseStudent);
            _db.SaveChanges();

            //3.Figure out what to return.
            var returnValue = new StudentDTO
            {
                Name = person.Name,
                SSN = person.SSN
            };
            return returnValue;
        }
 public IHttpActionResult AddStudentToCourse(int id, AddStudentViewModel model)
 {
     // Validation
     if (ModelState.IsValid)
     {
         try
         {
             // Add student to the course and return the StudentDTO
             var result = _service.AddStudentToCourse(id, model);
             return Content(HttpStatusCode.Created, result);
         }
         catch(AppObjectNotFoundException)
         {
             return StatusCode(HttpStatusCode.NotFound);
         }
         catch (MaxStudentException)
         {
             return StatusCode(HttpStatusCode.PreconditionFailed);
         }
         catch (DuplicateEntryException)
         {
             return StatusCode(HttpStatusCode.PreconditionFailed);
         }
         
     }
     else
     {
         return StatusCode(HttpStatusCode.PreconditionFailed);
     }
 }