示例#1
0
        public JsonResult Add(CourseViewModel input)
        {
            // Validate model state
            if (!ModelState.IsValid)
                return Json(new { error = true, message = "There were errors in the submission" });

            // Attempt to add the course
            try
            {
                // Build the course object
                var course = new Course()
                {
                    CourseID = input.Number,
                    CourseTitle = input.Name,
                    HoursPerWeek = input.WeeklyHours
                };

                // Add the course
                _courseService.AddCourse(course);

            }
            catch (CourseExistsException)
            {
                // If the course already exists, display error
                return Json(new { error = true, message = "Course already exists" });
            }

            return Json(new { error = false, message = "Course successfully created!" });
        }
示例#2
0
        public void AddCourse(Course course)
        {
            // If a course offering already exists, throw custom exception
            if (_courseRepository.CourseExists(course))
                throw new CourseExistsException();

            _courseRepository.InsertCourse(course);
            _courseRepository.Save();
        } 
示例#3
0
 public void InsertCourse(Course course)
 {
     _dbContext.Courses.Add(course);
 }
示例#4
0
 public bool CourseExists(Course course)
 {
     return _dbContext.Courses.Any(c => c.CourseID == course.CourseID);
 }