/// <summary> /// Adds the course. /// </summary> /// <param name="course">The course.</param> /// <exception cref="CourseNumberNotUniqueException"></exception> public void AddCourse(Course course) { using (_courseRepository) { // Check Unique on Name if (_courseRepository.CourseNumberExists(course.CourseNumber)) throw new CourseNumberNotUniqueException(); if (_courseRepository.CourseNameExists(course.CourseName)) throw new CourseNameNotUniqueException(); _courseRepository.AddCourse(course); } }
/// <summary> /// Adds the course. /// </summary> /// <param name="course">The course.</param> public void AddCourse(Course course) { using (var dbTransaction = _db.Database.BeginTransaction()) { try { _db.Courses.Add(course); _db.SaveChanges(); dbTransaction.Commit(); } catch (Exception) { dbTransaction.Rollback(); } } }
public JsonResult CreateUpdate(CourseViewModel viewModel) { if (!ModelState.IsValid) return Json(new { error = true, message = "There were errors in the submission" }); var currentCourse = _courseService.GetCourseById(viewModel.Number); // If the course does not exist in the db, add if (currentCourse == null) { try { // Build the course object var course = new Course() { CourseID = viewModel.Number, CourseTitle = viewModel.Name, HoursPerWeek = viewModel.WeeklyHours, Description = viewModel.Description }; // Add the course _courseService.AddCourse(course); return Json(new { error = false, message = "Course successfully created!" }); } catch (CourseExistsException) { // If the course already exists, display error return Json(new {error = true, message = "Course already exists"}); } } // Otherwise, update the fields currentCourse.CourseID = viewModel.Number; currentCourse.CourseTitle = viewModel.Name; currentCourse.HoursPerWeek = viewModel.WeeklyHours; currentCourse.Description = viewModel.Description; // Update the course with the changes _courseService.EditCourse(currentCourse); return Json(new {error = false, message = "The changes have been made!"}); }
public void DeleteCourse(Course course) { var offerings = _dbContext.CourseOfferings.Where(o => o.Course_CourseID == course.CourseID).ToList(); foreach (var offering in offerings.ToList()) { offerings.Remove(offering); foreach (var studentsInOffering in _dbContext.Students) { foreach (var o in studentsInOffering.CourseOfferings.ToList()) { studentsInOffering.CourseOfferings.Remove(o); } } } _dbContext.Courses.Remove(course); }
public void EditCourse(Course course) { _dbContext.Entry(course).State = EntityState.Modified; }
public void InsertCourse(Course course) { _dbContext.Courses.Add(course); }
public bool CourseExists(Course course) { return _dbContext.Courses.Any(c => c.CourseID == course.CourseID); }
/// <summary> /// Edit a Course /// </summary> /// <param name="course">The course.</param> /// <param name="courseName">Name of the course.</param> /// <param name="courseDescription">The course description.</param> /// <param name="weeklyHours">The weekly hours.</param> public void EditCourse(Course course, string courseName, string courseDescription, int weeklyHours) { using (var dbTransaction = _db.Database.BeginTransaction()) { try { course.CourseName = courseName; course.CourseDescription = courseDescription; course.WeeklyHours = weeklyHours; _db.SaveChanges(); dbTransaction.Commit(); } catch (Exception) { dbTransaction.Rollback(); } } }