public ActionResult CreateMultiple(InterviewCreateMultipleViewModel ViewModel) { Interview interview = ViewModel.interview; var openStatus = db.InterviewStatus.FirstOrDefault(s => s.default_status); if (openStatus == null) { Session["FlashMessage"] = "Default Status not found."; return RedirectToAction("CreateMultiple"); } int openedStatusId = openStatus.id; List<bool> availableDaysOfWeek = PrepareAvailableDaysOfWeek(ViewModel.config); List<Timeslot> timeslots = new List<Timeslot>(); DateTime datecursor = ViewModel.config.start_date.Date; //initialize skipped_dates to avoid null reference var skipped_dates = new List<DateTime>(); if (ViewModel.skipped_dates != null) { skipped_dates = ViewModel.skipped_dates.ToList(); } try { while (datecursor <= ViewModel.config.end_date.Date) { if (availableDaysOfWeek[Convert.ToInt16(datecursor.DayOfWeek)] && !skipped_dates.Any(d => d.Date == datecursor.Date)) { foreach (var session in ViewModel.sessions.Where(s => !s.excluded).ToList()) { TimeSpan starttime = session.start_time.TimeOfDay; TimeSpan duration = new TimeSpan(0, ViewModel.interview.duration + ViewModel.interview.buffer, 0); TimeSpan endtime = starttime.Add(duration); while (endtime <= session.end_time.TimeOfDay) { bool overlapped = false; TimeSpan newstarttime = new TimeSpan(); foreach (var ex_session in ViewModel.sessions.Where(x => x.excluded)) { TimeSpan ex_starttime = ex_session.start_time.TimeOfDay; TimeSpan ex_endtime = ex_session.end_time.TimeOfDay; if ((starttime >= ex_starttime && starttime < ex_endtime) || (endtime > ex_starttime && endtime <= ex_endtime) || (starttime <= ex_starttime && endtime >= ex_endtime)) { overlapped = true; newstarttime = ex_endtime > newstarttime ? ex_endtime : newstarttime; goto Action; } } foreach (var ex_session in db.Interviews.Where(i => i.venue_id == interview.venue_id)) { DateTime ex_starttime = ex_session.start_time; DateTime ex_endtime = ex_session.end_time; if ((datecursor.Add(starttime) >= ex_starttime && datecursor.Add(starttime) < ex_endtime) || (datecursor.Add(endtime) > ex_starttime && datecursor.Add(endtime) <= ex_endtime) || (datecursor.Add(starttime) <= ex_starttime && datecursor.Add(endtime) >= ex_endtime)) { overlapped = true; newstarttime = ex_endtime.TimeOfDay > newstarttime ? ex_endtime.TimeOfDay : newstarttime; goto Action; } } Action: if (!overlapped && endtime <= session.end_time.TimeOfDay) { interview.start_time = datecursor.Add(starttime); interview.end_time = datecursor.Add(endtime); interview.status_id = openedStatusId; db.Interviews.Add(interview); db.SaveChanges(); starttime = endtime; } else { starttime = newstarttime; } endtime = starttime.Add(duration); } } } datecursor = datecursor.AddDays(1); } return RedirectToAction("Index"); } catch (Exception e) { ViewBag.programList = new SelectList(db.Programs.Where(p => p.require_interview), "id", "name"); ViewBag.venueList = new SelectList(db.InterviewVenues.Where(v => v.status), "id", "name"); Session["FlashMessage"] = "Failed to create interview timeslots." + e.Message; return View(ViewModel); } }
public ActionResult CreateMultiple() { InterviewCreateMultipleViewModel ViewModel = new InterviewCreateMultipleViewModel(); ViewModel.interview = new Interview(); ViewBag.programList = new SelectList(db.Programs.Where(p => p.require_interview), "id", "name"); ViewBag.venueList = new SelectList(db.InterviewVenues.Where(v => v.status), "id", "name"); return View(ViewModel); }