예제 #1
0
        /// <summary>
        /// This method adds a Course with the information from the CourseViewModel
        /// </summary>
        /// <param name="newCourse">Course containing all the information needed to add a new course</param>
        /// <returns></returns>
        public CourseDetailsDTO AddCourse(CourseViewModel newCourse)
        {
            // Check if the course exists
            var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == newCourse.CourseID);

            if (courseTemplate == null)
            {
                throw new TemplateCourseNotFoundException();
            }
            Entities.Course course = new Entities.Course
            {
                ID         = _db.Courses.Any() ? _db.Courses.Max(x => x.ID) + 1 : 1,
                TemplateID = courseTemplate.ID,
                Semester   = newCourse.Semester,
                StartDate  = newCourse.StartDate,
                EndDate    = newCourse.EndDate
            };
            _db.Courses.Add(course);

            _db.SaveChanges();

            return(new CourseDetailsDTO
            {
                ID = course.ID,
                TemplateID = courseTemplate.TemplateID,
                Name = courseTemplate.Name,
                Description = courseTemplate.Description,
                Semester = course.Semester,
                StartDate = newCourse.StartDate,
                EndDate = newCourse.EndDate,
                StudentCount = _db.StudentEnrollment.Count(x => x.CourseID == course.ID)
            });
        }
        /// <summary>
        /// adds a new instance of a course to the database
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CourseDTO AddNewCourse(CourseViewModel model)
        {
            //check if course already in database
          
            //add the course
            var newCourse = new Course
            {

                TemplateID = model.TemplateID,
                Semester = model.Semester,
                StartDate = model.StartDate,
                EndDate = model.EndDate,
                MaxStudents = model.MaxStudents
            };

            _db.Courses.Add(newCourse);
            _db.SaveChanges();

            //find the Name of the course
            var courseTemplate = (from ct in _db.CourseTemplates
                              where ct.TemplateID == model.TemplateID
                              select ct).SingleOrDefault();

            var result = new CourseDTO
            {
                ID = newCourse.ID,
                Name = courseTemplate.Name,
                StartDate = newCourse.StartDate,
                StudentCount = 0
            };

            return result;
        }
예제 #3
0
        /// <summary>
        /// Deletes a course
        /// Note : This is a course not a course template.
        /// </summary>
        /// <param name="id">The ID of the course to delete</param>
        public void DeleteCourse(int id)
        {
            // Get the course
            Entities.Course course = _db.Courses.SingleOrDefault(x => x.ID == id);
            if (course == null)
            {
                throw new CourseNotFoundException();
            }

            using (var transaction = _db.Database.BeginTransaction())
            {
                try
                {
                    // Remove all the students from the course
                    foreach (Entities.StudentEnrollment enrollment in _db.StudentEnrollment.Where(x => x.CourseID == course.ID))
                    {
                        _db.StudentEnrollment.Remove(enrollment);
                        _db.SaveChanges();
                    }
                    _db.Courses.Remove(course); // Remove the course
                    _db.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw new DeleteFromDatabaseException();
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Updates a course that already exists
        /// Note that this edits a Course, not a CourseTemplate.
        /// Only start and end date are editable
        /// </summary>
        /// <param name="courseID">The ID of the course to edit</param>
        /// <param name="updateCourse">a course with the information to edit</param>
        /// <returns></returns>
        public CourseDetailsDTO UpdateCourse(int courseID, UpdateCourseViewModel updateCourse)
        {
            // Get the course, throw exception if the course is not found
            Entities.Course course = _db.Courses.SingleOrDefault(x => x.ID == courseID);
            if (course == null)
            {
                throw new CourseNotFoundException();
            }

            // Check if the course tamplate exists
            var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.ID == course.TemplateID);

            if (courseTemplate == null)
            {
                throw new TemplateCourseNotFoundException();
            }

            // Update the start and end date based on the view model
            course.StartDate = updateCourse.StartDate;
            course.EndDate   = updateCourse.EndDate;

            // If all is successfull, we save our changes
            _db.SaveChanges();

            return(new CourseDetailsDTO
            {
                ID = course.ID,
                TemplateID = courseTemplate.TemplateID,
                Name = courseTemplate.Name,
                Description = courseTemplate.Description,
                Semester = course.Semester,
                StartDate = updateCourse.StartDate,
                EndDate = updateCourse.EndDate,
                StudentCount = _db.StudentEnrollment.Count(x => x.CourseID == course.ID),
                Students = (from sr in _db.StudentEnrollment
                            join s in _db.Students on sr.StudentID equals s.ID
                            where sr.CourseID == course.ID
                            select new StudentDTO
                {
                    SSN = s.SSN,
                    Name = s.Name
                }).ToList()
            });
        }
예제 #5
0
        /// <summary>
        /// Gets a student with the given SSN and that is registered in the given course
        /// </summary>
        /// <param name="courseID">The id of the course</param>
        /// <param name="newStudent">The student</param>
        /// <returns>The student</returns>
        public StudentDTO GetStudentInCourse(int courseID, string studentSSN)
        {
            Entities.Course course = _db.Courses.SingleOrDefault(x => x.ID == courseID);
            if (course == null)
            {
                throw new CourseNotFoundException();
            }

            StudentDTO student = (from s in _db.Students
                                  join sr in _db.StudentEnrollment on s.ID equals sr.StudentID
                                  where sr.CourseID == course.ID && s.SSN == studentSSN
                                  select new StudentDTO
            {
                SSN = s.SSN,
                Name = s.Name
            }).SingleOrDefault();

            if (student == null)
            {
                throw new StudentNotFoundException();
            }
            return(student);
        }
예제 #6
0
        /// <summary>
        /// Method that adds a new course to the database.
        /// The attributes needed to add a new course are given with a view model class.
        /// </summary>
        /// <param name="model">Add course view model (ViewModel class)</param>
        /// <returns>The newly added course (DTO class)</returns>
        public CourseDTO CreateCourse(AddCourseViewModel model)
        {
            var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == model.TemplateID);
            if (courseTemplate == null)
            {
                throw new AppInternalServerException();
            }

            var course = new Course
            {
                TemplateID = model.TemplateID,
                StartDate = model.StartDate,
                EndDate = model.EndDate,
                Semester = model.Semester,
                MaxStudents = model.MaxStudents
            };

            _db.Courses.Add(course);
            _db.SaveChanges();

            var result = new CourseDTO
            {
                ID = course.ID,
                Name = course.TemplateID,
                StartDate = course.StartDate,
                StudentCount = 0
            };

            return result;
        }
        /// <summary>
        /// This method adds a Course with the information from the CourseViewModel
        /// </summary>
        /// <param name="newCourse">Course containing all the information needed to add a new course</param>
        /// <returns></returns>
        public CourseDetailsDTO AddCourse(CourseViewModel newCourse)
        {
            // Check if the course exists
            var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == newCourse.TemplateID);
            if (courseTemplate == null)
            {
                throw new TemplateCourseNotFoundException();
            }

            Entities.Course course = new Entities.Course
            {
                ID          = _db.Courses.Any() ? _db.Courses.Max(x => x.ID) + 1 : 1,
                TemplateID  = courseTemplate.ID,
                Semester    = newCourse.Semester,
                StartDate   = newCourse.StartDate,
                EndDate     = newCourse.EndDate,
                MaxStudents = newCourse.MaxStudents
            };
            _db.Courses.Add(course);

            _db.SaveChanges();

            return new CourseDetailsDTO
            {
                ID = course.ID,
                TemplateID = courseTemplate.TemplateID,
                Name = courseTemplate.Name,
                Description = courseTemplate.Description,
                Semester = course.Semester,
                StartDate = newCourse.StartDate,
                EndDate = newCourse.EndDate,
                StudentCount = _db.StudentEnrollment.Count(x => x.CourseID == course.ID
                                                             && x.IsActive == true
                                                             && x.IsOnWaitingList == false),
                Students = (from sr in _db.StudentEnrollment
                            join s in _db.Students on sr.StudentID equals s.ID
                            where sr.CourseID == course.ID
                            && sr.IsActive == true
                            && sr.IsOnWaitingList == false
                            select new StudentDTO
                            {
                                SSN = s.SSN,
                                Name = s.Name
                            }).ToList()
            };
        }
예제 #8
0
        /// <summary>
        /// Adds a course. 
        /// </summary>
        /// <param name="courseVM">The course view model.</param>
        /// <returns>Returns the course DTO. Or null if there's no matching course template.</returns>
        public CourseDTO AddCourse(CourseViewModel courseVM)
        {
            //Find the name for the course
            string courseName = (from c in _context.Courses 
                                 join ct in _context.CourseTemplates
                                 on c.TemplateID equals ct.TemplateID
                                 where ct.TemplateID == courseVM.TemplateID
                                 select ct.Name).FirstOrDefault();

            if (string.IsNullOrEmpty(courseName))
            {
                return null; //If there is no course with the given templateID, we return null..
            }

            Course course = new Course { TemplateID = courseVM.TemplateID, StartDate = courseVM.StartDate, EndDate = courseVM.EndDate, Semester = courseVM.Semester };
            _context.Courses.Add(course);
            _context.SaveChanges();

            //EF is brilliant.. adds the ID to the Course.. 
            return new CourseDTO { ID = course.ID, Name = courseName, Semester = course.Semester, StartDate = course.StartDate };
        }
        /// <summary>
        /// This method adds a Course with the information from the CourseViewModel
        /// </summary>
        /// <param name="newCourse">Course containing all the information needed to add a new course</param>
        /// <returns></returns>
        public CourseDetailsDTO AddCourse(CourseViewModel newCourse)
        {
            // Check if the course exists
            var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == newCourse.CourseID);
            if (courseTemplate == null)
            {
                throw new TemplateCourseNotFoundException();
            }
            Entities.Course course = new Entities.Course
            {
                ID = _db.Courses.Any() ? _db.Courses.Max(x => x.ID) + 1 : 1,
                TemplateID = courseTemplate.ID,
                Semester = newCourse.Semester,
                StartDate = newCourse.StartDate,
                EndDate = newCourse.EndDate
            };
            _db.Courses.Add(course);

            _db.SaveChanges();

            return new CourseDetailsDTO
            {
                ID = course.ID,
                TemplateID = courseTemplate.TemplateID,
                Name = courseTemplate.Name,
                Description = courseTemplate.Description,
                Semester = course.Semester,
                StartDate = newCourse.StartDate,
                EndDate = newCourse.EndDate,
                StudentCount = _db.StudentEnrollment.Count(x => x.CourseID == course.ID)
            };
        }
예제 #10
0
        /// <summary>
        /// Adds a course. 
        /// </summary>
        /// <exception cref="AppObjectNotFoundException">Thrown if we can't find a course with the given templateID.</exception>
        /// <param name="courseVM">The course view model.</param>
        /// <returns>Returns the course DTO.</returns>
        public CourseDTO AddCourse(CourseViewModel courseVM)
        {
            //Find the name for the course
            string courseName = _context.CourseTemplates.Where(ct => ct.TemplateID == courseVM.TemplateID).Select(ct => ct.Name).SingleOrDefault();

            if (string.IsNullOrEmpty(courseName))
            {
                throw new AppObjectNotFoundException();
            }

            Course course = new Course { TemplateID = courseVM.TemplateID, StartDate = courseVM.StartDate, EndDate = courseVM.EndDate, Semester = courseVM.Semester, MaxStudents = courseVM.MaxStudents };
            _context.Courses.Add(course);
            _context.SaveChanges();

            //EF is brilliant.. adds the ID to the Course.. 
            return new CourseDTO { ID = course.ID, Name = courseName, Semester = course.Semester, StartDate = course.StartDate, MaxStudents = course.MaxStudents };
        }