Example #1
0
        /// <summary>
        /// Adds a student to the waiting list of a course. 
        /// </summary>
        /// <exception cref="AppObjectNotFoundException">Thrown if the course is not found.</exception>
        /// <exception cref="DuplicateCourseRegistrationException">Thrown if the student is already enrolled to the course or to the waiting list.</exception>
        /// <param name="courseID">ID of the course.</param>
        /// <param name="studentID">ID of the student.</param>
        public void AddStudentToWaitingList(int courseID, StudentViewModel student) 
        {
            //Check if course exists
            Course course = _context.Courses.SingleOrDefault(c => c.ID == courseID);

            if (course == null)
            {
                throw new AppObjectNotFoundException();
            }

            int studentID = _context.Students.Where(s => s.SSN == student.SSN).Select(s => s.ID).SingleOrDefault();
            
            if (studentID == 0)
            {
                throw new AppObjectNotFoundException();
            }

            //Check to see if the user is already on the waiting list (or registered)
            StudentEnrollment enrollment = _context.StudentEnrollment.SingleOrDefault(se => se.StudentID == studentID && se.CourseID == courseID && se.IsDeleted == false);

            if (enrollment != null)
            {
                throw new DuplicateCourseRegistrationException();
            }

            //Check if the user was deleted before from the course.. if so, we undelete him and put ont he waitin glist
            enrollment = _context.StudentEnrollment.SingleOrDefault(se => se.StudentID == studentID && se.CourseID == courseID && se.IsDeleted == true);

            if (enrollment != null)
            {
                enrollment.IsDeleted = false;
                enrollment.IsOnWaitingList = true;
            }

            else
            {
                enrollment  = new StudentEnrollment
                {
                    CourseID = courseID,
                    StudentID = studentID,
                    IsOnWaitingList = true,
                };

                _context.StudentEnrollment.Add(enrollment);
            }

            _context.SaveChanges();
        }
Example #2
0
        /// <summary>
        /// Adds a student to a course. If the student doesn't exist, it is created. 
        /// </summary>
        /// <param name="courseID">The ID of the course.</param>
        /// <param name="studentVM">The student VM.</param>
        /// <returns>True if successful, false if the course does not exist.</returns>
        public bool AddStudentToCourse(int courseID, StudentViewModel studentVM) 
        {
            //Does the course exist?
            Course course = _context.Courses.Where(c => c.ID == courseID).SingleOrDefault();
            if (course == null)
            {
                return false;
            }

            Student student = _context.Students.Where(s => s.SSN == studentVM.SSN).SingleOrDefault();

            if (student == null)
            {
                student = new Student { Name = studentVM.Name, SSN = studentVM.SSN };
                _context.Students.Add(student);
                _context.SaveChanges();
            }

            StudentEnrollment studentEnrollment = new StudentEnrollment { StudentID = student.ID, CourseID = courseID };
            _context.StudentEnrollment.Add(studentEnrollment);
            _context.SaveChanges();
            return true;
        }
Example #3
0
        /// <summary>
        /// Adds a student to a course. Assumes the student already exists.
        /// Will not allow more than MaxStudents students. 
        /// </summary>
        /// <exception cref="AppObjectNotFoundException">Thrown if either the course or the student doesn't exist.</exception>
        /// <param name="courseID">The ID of the course.</param>
        /// <param name="SSN">The SSN of the student.</param>
        /// <returns>A student object.</returns>
        public StudentDTO AddStudentToCourse(int courseID, string SSN) 
        {
            //Does the course exist?
            Course course = _context.Courses.Where(c => c.ID == courseID).SingleOrDefault();
            if (course == null)
            {
                throw new AppObjectNotFoundException();
            }

            Student student = _context.Students.Where(s => s.SSN == SSN).SingleOrDefault();

            if (student == null)
            {
                throw new AppObjectNotFoundException();
            }

            //Verify that we won't violate the MaxStudents property
            int numStudents = _context.StudentEnrollment.Count(se => se.CourseID == courseID && se.IsDeleted == false && se.IsOnWaitingList == false);

            if (numStudents == course.MaxStudents)
            {
                throw new FullCourseException();
            }

            StudentEnrollment studentEnrollment = new StudentEnrollment { StudentID = student.ID, CourseID = courseID, IsOnWaitingList = false, IsDeleted = false };
            
            //Verify that the user isn't already registered.. 
            if (_context.StudentEnrollment.SingleOrDefault(se => se.StudentID == student.ID && se.CourseID == courseID && se.IsDeleted == false && se.IsOnWaitingList == false) != null)
            {
                throw new DuplicateCourseRegistrationException();
            }

            //If the user is already on a waiting list, we remove it from the waiting list..
            StudentEnrollment enrollment = _context.StudentEnrollment.SingleOrDefault(se => se.StudentID == student.ID && se.CourseID == courseID && se.IsOnWaitingList == true);

            if (!(enrollment == null))
            {
                _context.StudentEnrollment.Remove(enrollment);
            }

            //If the user has already registered on the course but the enrollment has been deleted.. we simply "undelete" it..
            enrollment = _context.StudentEnrollment.SingleOrDefault(se => se.CourseID == courseID && se.StudentID == student.ID && se.IsDeleted == true);

            if (enrollment != null)
            {
                enrollment.IsDeleted = false;
            }
            else
            {
                _context.StudentEnrollment.Add(studentEnrollment);
            }

            _context.SaveChanges();
            return new StudentDTO { ID = student.ID, Name = student.Name, SSN = student.SSN };
        }