/// <summary> /// Adds student to a waitinglist in the course with the given ID. /// If there is no course with the given ID, exception is thrown, /// </summary> /// <param name="id">ID of the course's waitinglist</param> /// <param name="student">AddStudentViewModel object containing the students information</param> public void AddStudentToWaitingList(int id, AddStudentViewModel student) { // Check if course exists var courseExistance = _db.Courses.SingleOrDefault(x => x.ID == id); if (courseExistance == null) { throw new AppObjectNotFoundException(); } // Check if students exists var studentExistance = _db.Students.SingleOrDefault(x => x.SSN == student.SSN); if (studentExistance == null) { throw new AppObjectNotFoundException(); } // Check if student is already enrolled in course var studentAlreadyInCourse = (from cr in _db.CourseRegistrations where cr.CourseID == id where studentExistance.ID == cr.StudentID where cr.Active select cr).SingleOrDefault(); if (studentAlreadyInCourse != null) { throw new AppConflictException(); } // Check if student is already on the waitinglist in the course var studentWaitingListExistance = (from wl in _db.WaitingListEntries where wl.CourseID == id where studentExistance.ID == wl.StudentID select wl).SingleOrDefault(); if (studentWaitingListExistance != null) { throw new AppConflictException(); } // Create a new WaitingListEntry var waitingListEntry = new WaitingListEntry { CourseID = id, StudentID = studentExistance.ID }; // Add the entry to the database _db.WaitingListEntries.Add(waitingListEntry); _db.SaveChanges(); }
public IHttpActionResult AddStudentToCourse(int id, AddStudentViewModel student) { if (ModelState.IsValid) { try { var result = _service.AddStudentToCourse(id, student); return Content(HttpStatusCode.Created, result); } catch (AppObjectNotFoundException) { //return 404 return NotFound(); } catch (AppPreconditionFailedException) { //return 409 return StatusCode(HttpStatusCode.PreconditionFailed); } } else { return StatusCode(HttpStatusCode.PreconditionFailed); } }
/// <summary> /// Adds a pre existing student to a pre existing course. /// </summary> /// <param name="id">The ID of the course to add the student to</param> /// <param name="model">A AddStudentViewModel containing the student that is to be added</param> public StudentDTO AddStudentToCourse(int id, AddStudentViewModel model) { // Checking if student exists in the database var student = _db.Students.SingleOrDefault(x => x.SSN == model.SSN); if (student == null) { // If the student cannot be found: throw new AppObjectNotFoundException(); } // Checking if the course exists in the database var course = _db.Courses.SingleOrDefault(x => x.ID == id); if (course == null) { // If the course cannot be found: throw new AppObjectNotFoundException(); } var studentCount = (from cr in _db.CourseRegistrations where id == cr.CourseID where cr.Active select cr.StudentID).ToList().Count(); // If the max count of student has been reached for this class, // return 412 if (studentCount == course.MaxStudents) { throw new AppPreconditionFailedException(); } // Check if the student is on the waiting list for the course var studentOnWaitingList = (from wl in _db.WaitingListEntries where wl.CourseID == id where student.ID == wl.StudentID select wl).SingleOrDefault(); // Checking if the student is already enrolled in the course var studentAlreadyInCourse = (from cr in _db.CourseRegistrations where cr.CourseID == id where student.ID == cr.StudentID where cr.Active select cr).SingleOrDefault(); // If he is not enrolled, we enroll him in the course if (studentAlreadyInCourse == null) { //remove the student from waitinglist if he was on it if (studentOnWaitingList != null) { _db.WaitingListEntries.Remove(studentOnWaitingList); _db.SaveChanges(); } //Create a new CourseRegistration var newRegistration = new CourseRegistration { CourseID = id, StudentID = student.ID, Active = true }; //Add it to the database _db.CourseRegistrations.Add(newRegistration); _db.SaveChanges(); //Make a new StudentDTO to return to the client var studentDto = new StudentDTO { Name = student.Name, SSN = student.SSN }; return studentDto; } else { throw new AppPreconditionFailedException(); } }
public IHttpActionResult AddStudentToWaitingList(int id, AddStudentViewModel student) { if (ModelState.IsValid) { try { _service.AddStudentToWaitingList(id, student); return StatusCode(HttpStatusCode.OK); } catch (AppObjectNotFoundException) { //return 404 return NotFound(); } catch (AppConflictException) { //return 409 return StatusCode(HttpStatusCode.PreconditionFailed); } } else { return StatusCode(HttpStatusCode.PreconditionFailed); } }