public IActionResult AddToWaitingList(int id, [FromBody] StudentSSN Student)
        {
            StudentSSN addStudent = new StudentSSN();

            if (ModelState.IsValid)
            {
                try
                {
                    addStudent = _service.AddToWaitingList(id, Student);
                }
                catch (AppObjectNotFoundException)
                {
                    return(NotFound());
                }
                catch (StudentNonExistException)
                {
                    return(NotFound());
                }
                catch (StudentIsInCourseException)
                {
                    return(StatusCode(412));
                }
                catch (StudentOnWaitingListException)
                {
                    return(StatusCode(412));
                }
            }
            else
            {
                return(BadRequest());
            }
            return(Ok());
        }
Example #2
0
        /// <summary>
        /// Deleats a student from a course.
        /// </summary>
        /// <param name="id">Id of the course</param>
        /// <param name="SSN">The social security number of the student</param>
        /// <returns>The student deleted SSN if we are able to save to database else FailedToSaveToDatabaseException().</returns>
        public StudentSSN DeleteStudent(int id, long SSN)
        {
            var        course        = GetCourseByID(id);
            StudentSSN studentdelete = new StudentSSN();

            studentdelete.SSN = SSN;

            var listStudents    = course.listOfStudents;
            var studentinCourse = listStudents.Exists(x => x.SSN == SSN);

            if (studentinCourse == false)
            {
                throw new AppObjectNotFoundException();
            }
            //else we find and delete
            listStudents.RemoveAll(x => x.SSN == SSN);

            var student2 = (from x in _db.StudentsInCourses
                            where x.SSN == SSN
                            select x).SingleOrDefault();

            student2.Active = 0;

            try {
                _db.SaveChanges();
            }
            catch (Exception e) {
                Console.WriteLine(e);
                throw new FailedToSaveToDatabaseException();
            }
            return(studentdelete);
        }
        public IActionResult DeleteCourse(int id, long SSN)
        {
            StudentSSN studentdelete = new StudentSSN();

            try
            {
                studentdelete = _service.DeleteStudent(id, SSN);
            }
            catch (AppObjectNotFoundException)
            {
                return(NotFound());
            }
            catch (FailedToSaveToDatabaseException)
            {
                return(StatusCode(500));
            }
            return(NoContent());
        }
        public IActionResult AddStudentToCourse(int id, [FromBody] StudentSSN Student)
        {
            Console.WriteLine("AddStudentToCoursecontroller");
            StudentSSN addStudent = new StudentSSN();

            if (ModelState.IsValid)
            {
                try
                {
                    addStudent = _service.AddStudentToCourse(id, Student);
                }
                catch (StudentIsInCourseException)
                {
                    return(StatusCode(412));
                }
                catch (MaxNumberOfStudentsException)
                {
                    return(StatusCode(412));
                }
                catch (FailedToSaveToDatabaseException)
                {
                    return(StatusCode(500));
                }
                catch (StudentNonExistException)
                {
                    return(NotFound());
                }
                catch (AppObjectNotFoundException)
                {
                    return(NotFound());
                }
            }
            else
            {
                return(BadRequest());
            }

            var location = Url.Link("AddStudentToCourse", new { id = id });

            return(Created(location, Student));
        }
Example #5
0
        /// <summary>
        /// Should add a student to the waiting list of a spesific course
        /// </summary>
        /// <param name="id">The id of the Course</param>
        /// <param name="student">The student we are adding to the waiting list</param>
        /// <returns> If the student does not exist int the database we throw StudentNonExistException().
        /// If the student is alredy on the waiting list we throw StudentOnWaitingListException().
        /// If the student is alredy in the course that the waiting list is for we throw StudentIsInCourseException().
        /// If it fails to save to database we throw FailedToSaveToDatabaseException().
        /// Else we return the student that we added to the waiting list.
        /// </returns>
        public StudentSSN AddToWaitingList(int id, StudentSSN student)
        {
            var course = GetCourseByID(id);

            var newStudent = new Entities.WaitingList {
                CourseID = id,
                SSN      = student.SSN
            };
            var studentExists = (from x in _db.Students
                                 where student.SSN == x.SSN
                                 select new StudentSSN {
                SSN = x.SSN
            }).SingleOrDefault();

            if (studentExists == null)
            {
                throw new StudentNonExistException();
            }
            List <StudentDTO> students = GetWaitingList(id);
            var studentinlist          = students.Exists(x => x.SSN == student.SSN);

            if (studentinlist)
            {
                throw new StudentOnWaitingListException();
            }
            List <StudentDTO> studentsInCourse = GetListOfStudentsByCourseId(id);
            var studentInCourse = studentsInCourse.Exists(x => x.SSN == student.SSN);

            if (studentInCourse)
            {
                throw new StudentIsInCourseException();
            }
            _db.WaitingList.Add(newStudent);
            try {
                _db.SaveChanges();
            } catch (Exception e) {
                Console.WriteLine(e);
                throw new FailedToSaveToDatabaseException();
            }
            return(student);
        }
Example #6
0
        /// <summary>
        /// Adds student to the table StudentsInCourse
        /// </summary>
        /// <param name="id">The id of the couse we are going to add in</param>
        /// <param name="student">The Student of type StudentSSN containing the students SSN </param>
        /// <returns>If maximum number of students has reached we throw MaxNumberOfStudentsException().
        /// If the student does not exsist in the database we throw StudentNonExistException().
        /// If it fails to save to database we return FailedToSaveToDatabaseException().
        /// If nothing fails we return the student that was created.
        /// ///</returns>
        public StudentSSN AddStudentToCourse(int id, StudentSSN student)
        {
            Console.WriteLine("AddStudentToCourse");

            List <StudentDTO> listofStudents = GetListOfStudentsByCourseId(id);
            var numberOfStudents             = listofStudents.Count;
            var course = GetCourseByID(id);

            if (numberOfStudents == course.MaxStudents)
            {
                throw new MaxNumberOfStudentsException();
            }

            var studentExists = (from x in _db.Students
                                 where student.SSN == x.SSN
                                 select new StudentSSN {
                SSN = x.SSN
            }).SingleOrDefault();

            if (studentExists == null)
            {
                throw new StudentNonExistException();
            }

            var isStudentInCourse = (from x in _db.StudentsInCourses
                                     where x.SSN == student.SSN
                                     where x.CourseID == id
                                     select x).SingleOrDefault();

            if (isStudentInCourse == null)
            {
                var studentInCourse = new Entities.StudentsInCourse {
                    CourseID = id,
                    SSN      = student.SSN,
                    Active   = 1
                };
                _db.StudentsInCourses.Add(studentInCourse);
            }
            else
            {
                if (isStudentInCourse.Active == 1)
                {
                    throw new StudentIsInCourseException();
                }
                else
                {
                    isStudentInCourse.Active = 1;
                }
            }

            var studentonwaitinglist = (from x in _db.WaitingList
                                        where x.SSN == student.SSN
                                        where x.CourseID == id
                                        select x).SingleOrDefault();

            if (studentonwaitinglist != null)
            {
                _db.WaitingList.Remove(studentonwaitinglist);
            }

            try {
                _db.SaveChanges();
            } catch (Exception e) {
                Console.WriteLine(e);
                throw new FailedToSaveToDatabaseException();
            }

            return(student);
        }