/// <summary> /// Add sittings in database if there is no sitting. /// </summary> /// <returns></returns> public async Task AddSittings() { try { if (!_context.Sittings.Any()) { var breakfast = new Sitting { StartTime = new DateTime(2020, 01, 01, 07, 00, 00), EndTime = new DateTime(2020, 01, 01, 11, 00, 00), Capacity = 15, SittingType = SittingType.Breakfast }; var lunch = new Sitting { StartTime = new DateTime(2020, 01, 01, 11, 00, 00), EndTime = new DateTime(2020, 01, 01, 16, 00, 00), Capacity = 15, SittingType = SittingType.Lunch }; var dinner = new Sitting { StartTime = new DateTime(2020, 01, 01, 16, 00, 00), EndTime = new DateTime(2020, 01, 01, 23, 00, 00), Capacity = 15, SittingType = SittingType.Dinner }; _context.Sittings.AddRange(breakfast, lunch, dinner); await _context.SaveChangesAsync(); } } catch (Exception ex) { _logger.Error(ex.Message); throw new Exception(ex.Message); } }
/// <summary> /// Check if the reservation can be made /// </summary> /// <param name="currentReservations"></param> /// <param name="oldReservation"></param> /// <param name="reservation"></param> /// <param name="sitting"></param> /// <returns></returns> private bool CanMakeReservation(List <ReservationRequest> currentReservations, ReservationRequest oldReservation, ReservationRequest reservation, Sitting sitting) { try { //Remove old reservation from current reservations as it will cause duplicate calculation of number of guest. if (oldReservation != null) { currentReservations.Remove(oldReservation); } //Find the reservations with same date and sitting of the input reservation. var reservations = currentReservations .Where(r => r.StartDateTime.Date.Equals(reservation.StartDateTime.Date) && r.SittingType.Equals(reservation.SittingType) && (r.StartDateTime.TimeOfDay <reservation.EndDateTime.TimeOfDay && r.StartDateTime.TimeOfDay >= reservation.StartDateTime.TimeOfDay || r.EndDateTime.TimeOfDay> reservation.StartDateTime.TimeOfDay && r.EndDateTime.TimeOfDay <= reservation.EndDateTime.TimeOfDay)) .OrderBy(r => r.StartDateTime) .ToList(); //Create dictionary and initialize the time slot based on the start time and end time of each reservation. //Should consider the overlapped reservations as well. var dict = new Dictionary <TimeSpan, List <int?> > { { reservation.StartDateTime.TimeOfDay, new List <int?>() }, { reservation.EndDateTime.TimeOfDay, new List <int?>() } }; foreach (var r in reservations) { if (!dict.ContainsKey(r.StartDateTime.TimeOfDay)) { dict[r.StartDateTime.TimeOfDay] = new List <int?>(); } if (r.EndDateTime.TimeOfDay > reservation.EndDateTime.TimeOfDay) { continue; } if (!dict.ContainsKey(r.EndDateTime.TimeOfDay)) { dict[r.EndDateTime.TimeOfDay] = new List <int?> { r.NumberOfGuest } } ; else { dict[r.EndDateTime.TimeOfDay].Add(r.NumberOfGuest); } } //Add number of guest of input reservation into the corresponding time points. foreach (var record in dict) { record.Value.Add(reservation.NumberOfGuest); foreach (var r in reservations) { if (record.Key > r.StartDateTime.TimeOfDay && record.Key < r.EndDateTime.TimeOfDay) { dict[record.Key].Add(r.NumberOfGuest); } } } //Check if all the time points can hold the number of guest of input reservation, and return the result. var guestNo = dict.OrderBy(record => record.Key).Select(record => record.Value.Sum(v => v)).ToArray(); return(guestNo.All(number => number <= sitting.Capacity)); } catch (Exception ex) { _logger.Error(ex.Message); throw new Exception(ex.Message); } }