/// <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> /// Method that returns a student with a given SSN in a /// course with a given ID. /// </summary> /// <param name="id">ID of the course</param> /// <param name="SSN">SSN of the student</param> /// <returns>Single student (DTO class)</returns> public StudentDTO GetStudentInCourse(int id, string SSN) { var course = _db.Courses.SingleOrDefault(x => x.ID == id); var student = _db.Students.SingleOrDefault(x => x.SSN == SSN); var courseStudent = _db.CourseStudents.SingleOrDefault(x => x.CourseID == id && x.StudentID == student.ID && x.IsActive == true); if (course == null || student == null || courseStudent == null) { throw new AppObjectNotFoundException(); } var result = new StudentDTO { Name = student.Name, SSN = student.SSN }; return result; }