public static Course FindOrMake(this DbSet<Course> courses, CourseType type, Term term, string fourDigitYear, DbContext ctx) { int year; Course course = null; if (Int32.TryParse(fourDigitYear, out year) && year > 1900 && year < 2100) { course = courses.FirstOrDefault(c => c.CourseTypeCode == type.CourseTypeCode && c.TermId == term.TermId && c.Year == year); if (course == default(Course)) { course = new Course() { Term = term, CourseType = type, Year = year }; courses.Add(course); ctx.SaveChanges(); } } return (course); }
public ActionResult Create(Course course) { if (ModelState.IsValid) { context.Courses.Add(course); ApplyCourseVolunteerAllocations(course); context.SaveChanges(); return RedirectToAction("Index"); } _initialisesVolunteerAllocationView.Initialise(ViewBag, context); ViewBag.PossibleTerms = context.Terms; return View(course); }
static void AddCourseVolunteer(SceneCRM context, Course course, string volunteerName, string jobTitle) { var vol = context.Volunteers.FindOrMake(volunteerName, context); if (vol == null) return; var job = context.Jobs.FindOrMake(jobTitle, context); if (!vol.CourseVolunteers.Any(cv => cv.Job == job && cv.Course == course)) { context.CourseVolunteers.Add(new CourseVolunteer() { Course = course, Volunteer = vol, Job = job }); } context.SaveChanges(); }
private void ApplyCourseVolunteerAllocations(Course course) { var allocatedVolunteers = _interpretsPostedVolunteerAllocations.Interpret(Request.Form); course.CourseVolunteers.Clear(); foreach (var volunteerAllocation in allocatedVolunteers) { course.CourseVolunteers.Add( new CourseVolunteer { JobId = volunteerAllocation.JobId, VolunteerId = volunteerAllocation.VolunteerId, Notes = volunteerAllocation.Notes }); } }
public ActionResult Edit(Course course) { if (ModelState.IsValid) { context.Entry(course).State = EntityState.Modified; ApplyCourseVolunteerAllocations(course); context.SaveChanges(); return RedirectToAction("Index"); } _initialisesVolunteerAllocationView.Initialise(ViewBag, context, course.CourseVolunteers.Select(cv => new VolunteerAllocation(cv.VolunteerId, cv.JobId, cv.Notes))); ViewBag.PossibleTerms = context.Terms; return View(course); }