public Booking AddBooking(Booking booking) { List<ITimeSlot> availableTimeSlots = GetAvailableTimeSlots(booking); if (!availableTimeSlots.Any()) { throw new ArgumentException("This is no place for booking"); } ITimeSlot firstTimeSlot = availableTimeSlots .FirstOrDefault(ts => ts.StartTime <= booking.DateTime.TimeOfDay && ts.StartTime.Add(ts.Duration) > booking.DateTime.TimeOfDay); if (firstTimeSlot != null) { int amount = firstTimeSlot.GetSlotsAmount(booking.GetDuration()); for (int i = 0; i < amount; i++) { TimeSlots.Find(ts => ts.Number == firstTimeSlot.Number + i).IsAvailable = false; } Bookings.Add(booking); return booking; } throw new ArgumentException("This is no place for booking"); }
public List<ITimeSlot> GetAvailableTimeSlots(Booking booking) { List<ITimeSlot> timeSlots = new List<ITimeSlot>(); List<ITimeSlot> allAvailableTimeSlots = GetAllAvailableTimeSlots(); TimeSpan bookingDuration = booking.GetDuration().Duration(); if (allAvailableTimeSlots.Any()) { int amount = allAvailableTimeSlots.First().GetSlotsAmount(bookingDuration); for (int i = 0; i <= allAvailableTimeSlots.Count - amount; i++) { bool flag = true; ITimeSlot timeSlot = allAvailableTimeSlots[i]; for (int j = 1; j < amount; j++) { if ( allAvailableTimeSlots[i+j].Number != timeSlot.Number + j) { flag = false; break; } } if (flag) { timeSlots.Add(timeSlot); } } } return timeSlots; }
public Booking RemoveBooking(Booking booking) { if (booking.DateTime <= DateTime.Now.AddDays(1)) { throw new OperationCanceledException("Can not remove booking 24 hours before booking time"); } Bookings.Remove(booking); ITimeSlot firstTimeSlot = TimeSlots .FirstOrDefault(ts=>ts.StartTime == booking.DateTime.TimeOfDay); if (firstTimeSlot !=null) { int amount = firstTimeSlot.GetSlotsAmount(booking.GetDuration()); for (int i = 0; i < amount; i++) { TimeSlots.Find(ts => ts.Number == firstTimeSlot.Number + i).IsAvailable = true; } return booking; } throw new Exception("Can not delete booking"); }