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!" }); }
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(); }
public void InsertCourse(Course course) { _dbContext.Courses.Add(course); }
public bool CourseExists(Course course) { return _dbContext.Courses.Any(c => c.CourseID == course.CourseID); }