private bool OverlapFound(Dictionary <string, string> errors, ReactCinemaDbContext context) { ShowtimeGroup overlappedGroup = context.ShowtimeGroups .Where(s => s.FromDate <ToDate && s.ToDate> FromDate && s.MovieID == MovieID && s.ShowtimeGroupID != ShowtimeGroupID) .FirstOrDefault(); if (overlappedGroup != null) { errors.Add("general", "Another Showtime Group was found which overlaps with this one."); return(true); } List <ShowtimeGroupEntry> orderedEntries = ShowtimeGroupEntries.OrderBy(e => e.RoomID).ThenBy(e => e.StartTime).ToList(); for (int i = 0; i < orderedEntries.Count; i++) { if (i == orderedEntries.Count - 1) { break; } else if (orderedEntries[i].Conflicts(orderedEntries[i + 1], Movie.Duration)) { errors.Add("general", $"Showtime {orderedEntries[i].FormatStartTime} overlaps with {orderedEntries[i+1].FormatStartTime}."); return(true); } } foreach (ShowtimeGroupEntry entry in ShowtimeGroupEntries) { entry.SetInterval(); for (DateTime date = FromDate; date <= ToDate; date = date.AddDays(1)) { DateTime showtimeDate = date.Date + entry.Interval; Showtime showtime = context.Showtimes .Where(s => s.RoomID == entry.RoomID && s.StartTime <showtimeDate.AddMinutes(Movie.Duration) && s.EndTime> showtimeDate && s.MovieID != MovieID) .Include(s => s.Movie) .FirstOrDefault(); if (showtime != null) { errors.Add("general", $"Showtime conflict with {showtime.Movie.Title} at {showtime.StartTime}"); return(true); } } } return(false); }
public void UpdateEntries(ShowtimeGroup updatedGroup) { if (updatedGroup.FromDate < FromDate) { foreach (ShowtimeGroupEntry entry in ShowtimeGroupEntries) { DateTime earlier = updatedGroup.ToDate < FromDate.AddDays(-1) ? ToDate : FromDate.AddDays(-1); entry.GenerateShowtimes(Movie, updatedGroup.FromDate, earlier); } } else if (updatedGroup.FromDate > FromDate) { foreach (ShowtimeGroupEntry entry in ShowtimeGroupEntries) { entry.RemoveShowtimes(FromDate, updatedGroup.FromDate.AddDays(-1)); } } FromDate = updatedGroup.FromDate; if (updatedGroup.ToDate < ToDate) { foreach (ShowtimeGroupEntry entry in ShowtimeGroupEntries) { entry.RemoveShowtimes(updatedGroup.ToDate.AddDays(1), ToDate); } } else if (updatedGroup.ToDate > ToDate) { foreach (ShowtimeGroupEntry entry in ShowtimeGroupEntries) { DateTime later = ToDate.AddDays(1) > updatedGroup.FromDate ? ToDate.AddDays(1) : updatedGroup.FromDate; entry.GenerateShowtimes(Movie, later, updatedGroup.ToDate); } } ToDate = updatedGroup.ToDate; UpdateGroupEntries(updatedGroup); }
private void UpdateGroupEntries(ShowtimeGroup updatedGroup) { List <ShowtimeGroupEntry> entriesToDelete = ShowtimeGroupEntries .Where(e => !updatedGroup.ShowtimeGroupEntries.Any(ue => (e.StartTime == ue.StartTime && e.RoomID == ue.RoomID && e.ExperienceID == ue.ExperienceID) || (e.ShowtimeGroupEntryID == ue.ShowtimeGroupEntryID))) .ToList(); List <ShowtimeGroupEntry> entriesToCreate = updatedGroup.ShowtimeGroupEntries .Where(ue => !ShowtimeGroupEntries.Any(e => (e.StartTime == ue.StartTime && e.RoomID == ue.RoomID && e.ExperienceID == ue.ExperienceID) || (e.ShowtimeGroupEntryID == ue.ShowtimeGroupEntryID))) .ToList(); foreach (ShowtimeGroupEntry entry in entriesToDelete) { ShowtimeGroupEntries.Remove(entry); } foreach (ShowtimeGroupEntry entry in ShowtimeGroupEntries) { ShowtimeGroupEntry updatedEntry = updatedGroup.ShowtimeGroupEntries .Where(e => e.ShowtimeGroupEntryID == entry.ShowtimeGroupEntryID) .SingleOrDefault(); if (updatedEntry != null) { entry.UpdateData(updatedEntry, Movie.Duration); } } foreach (ShowtimeGroupEntry entry in entriesToCreate) { entry.GenerateShowtimes(Movie, FromDate, ToDate); ShowtimeGroupEntries.Add(entry); } }