/// <summary>
        /// This method adds a given student to the waitinglist for a give course
        /// </summary>
        /// <param name="courseID">The course id</param>
        /// <param name="newStudent">The new student</param>
        /// <returns>The new student</returns>
        public StudentDTO AddStudentToWaitinglist(int courseID, StudentViewModel newStudent)
        {
            var course = _db.Courses.SingleOrDefault(x => x.ID == courseID);
            if (course == null)
            {
                throw new CourseNotFoundException();
            }

            var student = _db.Students.SingleOrDefault(x => x.SSN == newStudent.SSN);
            if (student == null)
            {
                throw new StudentNotFoundException();
            }

            var studentEnrollment = _db.StudentEnrollment.SingleOrDefault(x => x.StudentID == student.ID
                                                                            && x.CourseID == course.ID);
            if (studentEnrollment == null)
            {
                _db.StudentEnrollment.Add(new Entities.StudentEnrollment
                {
                    CourseID = course.ID,
                    StudentID = student.ID,
                    IsActive = true,
                    IsOnWaitingList = true
                });
            }
            else if (studentEnrollment.IsOnWaitingList == true || studentEnrollment.IsActive == true)
            {   
                // Check if the student is already on the waitinglist or registered in the course
                throw new StudentAlreadyRegisteredInCourseException();
            }
            else if (studentEnrollment.IsActive == false) // Check if the student as been removed
            {
                studentEnrollment.IsOnWaitingList = true;
                studentEnrollment.IsActive        = true;
            }

            _db.SaveChanges();

            return new StudentDTO
            {
                Name = student.Name,
                SSN  = student.SSN
            };
        }
Example #2
0
        public HttpResponseMessage AddStudent(int courseID, StudentViewModel studentVM)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
            }

            bool studentAddedSuccessfully = _service.AddStudentToCourse(courseID, studentVM);

            if (!studentAddedSuccessfully)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound); //Í IHttpActionResult.. getur gert return StatusCode(...)
            }

            //Would return Created(location, studentDTO) but the API has no location for this object.. 
            //Instead of this, want to use IHttpActionResult and [ResponseType].. then return Content(HttpStatusCode.Created, studentDTO) 
            return new HttpResponseMessage(HttpStatusCode.Created);
        }
Example #3
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;
        }
        /// <summary>
        /// This method adds a student to a given course
        /// </summary>
        /// <param name="courseID">The id of the course</param>
        /// <param name="newStudent">The student to be added</param>
        /// <returns>Details about the course, including a list of all students registerd in the course</returns>
        public StudentDTO AddStudentToCourse(int courseID, StudentViewModel newStudent)
        {
            // Check if the course exists
            var course = _db.Courses.SingleOrDefault(x => x.ID == courseID);
            if (course == null)
            {
                throw new CourseNotFoundException();
            }

            // Check if the student exists
            var student = _db.Students.SingleOrDefault(x => x.SSN == newStudent.SSN);
            if (student == null)
            {
                throw new StudentNotFoundException();
            }

            // Check if the course is full
            if (course.MaxStudents <= _db.StudentEnrollment.Count(x => x.CourseID == course.ID 
                                                                    && x.IsActive == true 
                                                                    && x.IsOnWaitingList == false))
            {
                throw new CourseIsFullException();
            }
            
            // Check if the student is already registered in the course
            var studentEnrollment = _db.StudentEnrollment.SingleOrDefault(x => x.StudentID == student.ID && x.CourseID == course.ID);
            if (studentEnrollment == null) // Student has not registered for the course
            {
                _db.StudentEnrollment.Add(new Entities.StudentEnrollment
                {
                    StudentID       = student.ID,
                    CourseID        = course.ID,
                    IsOnWaitingList = false,
                    IsActive        = true
                });
            }
            else if (studentEnrollment.IsOnWaitingList == true) // Student is on the waitinglist
            {
                studentEnrollment.IsOnWaitingList = false;
                studentEnrollment.IsActive        = true;
            }
            else if (studentEnrollment.IsActive == false) // If the student enrollment has been deleted
            {
                studentEnrollment.IsActive = true;
            }
            else // Student is already in the course
            {
                throw new StudentAlreadyRegisteredInCourseException();
            }

            _db.SaveChanges();

            return new StudentDTO
            {
                SSN  = student.SSN,
                Name = student.Name
            };
        }
        /// <summary>
        /// This method adds a student to a given course
        /// </summary>
        /// <param name="courseID">The id of the course</param>
        /// <param name="newStudent">The student to be added</param>
        /// <returns>Details about the course, including a list of all students registerd in the course</returns>
        public StudentDTO AddStudentToCourse(int courseID, StudentViewModel newStudent)
        {
            // Check if the course exists
            var course = _db.Courses.SingleOrDefault(x => x.ID == courseID);
            var courseDetails = _db.CourseTemplates.SingleOrDefault(x => x.ID == course.TemplateID);
            if (course == null)
            {
                throw new CourseNotFoundException();
            }

            // Check if the student exists
            var student = _db.Students.SingleOrDefault(x => x.SSN == newStudent.SSN);
            if (student == null)
            {
                throw new StudentNotFoundException();
            }

            _db.StudentEnrollment.Add(new Entities.StudentEnrollment
            {
                StudentID = student.ID,
                CourseID = course.ID
            });

            _db.SaveChanges();

            return new StudentDTO
            {
                SSN = student.SSN,
                Name = student.Name
            };
        }
 public IHttpActionResult AddStudentToWaitinglist(int id, StudentViewModel newStudent)
 {
     if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); }
     try
     {
         return Ok(_service.AddStudentToWaitinglist(id, newStudent));
     }
     catch (CourseNotFoundException)
     {
         throw new HttpResponseException(HttpStatusCode.NotFound);
     }
     catch (StudentNotFoundException)
     {
         throw new HttpResponseException(HttpStatusCode.NotFound);
     }
     catch (StudentAlreadyRegisteredInCourseException)
     {
         throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
     }
 }
        public IHttpActionResult AddStudentToCourse(int id, StudentViewModel newStudent)
        {
            if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); }

            try
            {
                StudentDTO student = _service.AddStudentToCourse(id, newStudent);
                var location = Url.Link("GetStudentInCourse", new { id = id, ssn = student.SSN });
                return Created(location, student);
            }
            catch (CourseNotFoundException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            catch (StudentNotFoundException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            catch (StudentAlreadyRegisteredInCourseException)
            {
                throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
            }
            catch (CourseIsFullException)
            {
                throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
            }
        }
Example #8
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 #9
0
        public IHttpActionResult AddStudentToWaitingList(int courseID, StudentViewModel student)
        {
            try
            {
                _service.AddStudentToWaitingList(courseID, student);
            }
            catch (AppObjectNotFoundException)
            {
                return NotFound();
            }
            catch (DuplicateCourseRegistrationException)
            {
                return StatusCode(HttpStatusCode.PreconditionFailed);
            }

            return StatusCode(HttpStatusCode.OK); //Should return OK according to the tester..
        }