/// <summary> /// Method that adds a student to a course with a given ID. /// The attributes needed to add a student to a course are given with /// a view model class. /// </summary> /// <param name="id">ID of the course</param> /// <param name="model">Add student view model (ViewModel class)</param> /// <returns>The student that was added to the course (DTO class)</returns> public StudentDTO AddStudentToCourse(int id, AddStudentViewModel model) { var course = _db.Courses.SingleOrDefault(x => x.ID == id); var student = _db.Students.SingleOrDefault(x => x.SSN == model.SSN); if (course == null || student == null) { throw new AppObjectNotFoundException(); } var studentInCourse = _db.CourseStudents.SingleOrDefault(x => x.CourseID == id && x.StudentID == student.ID && x.IsActive == true); if(studentInCourse != null) { throw new AppObjectIllegalAddException(); } if (course.MaxStudents <= GetStudentsInCourse(id).Count) { throw new AppMaxReachedException(); } var studentInWaitingList = _db.WaitingLists.SingleOrDefault(x => x.CourseID == id && x.StudentID == student.ID); if(studentInWaitingList != null) { _db.WaitingLists.Remove(studentInWaitingList); } var courseStudent = _db.CourseStudents.SingleOrDefault(x => x.CourseID == id && x.StudentID == student.ID && x.IsActive == false); if(courseStudent != null) { courseStudent.IsActive = true; } else { var newCourseStudent = new CourseStudent { CourseID = course.ID, StudentID = student.ID, IsActive = true }; _db.CourseStudents.Add(newCourseStudent); } _db.SaveChanges(); var result = new StudentDTO { Name = student.Name, SSN = student.SSN }; return result; }
/// <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; }