//Returns the new id if success, and "0" if not //TODO: Refactor? public int SaveBooking(Booking booking) { Booking bookingToSave = new Booking() { Rooms=new List<Room>()}; ICollection<Category> allCategories = context.Categories.ToList(); int numberOfRoomsInCategory; int[] roomIds; foreach(Category c in allCategories) { numberOfRoomsInCategory = booking.Rooms.Where(r => r.TheCategory.Id == c.Id).Count(); if(numberOfRoomsInCategory>0) { roomIds = FindAvailableRooms(c.Id, booking.CheckInDate, booking.CheckOutDate).ToArray(); if(roomIds.Count()>=numberOfRoomsInCategory) { for (int j = 0; j < numberOfRoomsInCategory; j++) { bookingToSave.Rooms.Add(context.Rooms.Find(roomIds[j])); } } else { return 0; } } } bookingToSave.CheckInDate = booking.CheckInDate; bookingToSave.CheckOutDate = booking.CheckOutDate; bookingToSave.UserId = booking.UserId; context.Bookings.Add(bookingToSave); context.SaveChanges(); return bookingToSave.Id; }
public bool IsBookingPossible(Booking newBooking) { var result = Bookings.All(existingBooking => newBooking.From >= existingBooking.To || newBooking.To <= existingBooking.From); return(result); }
public void CreateBooking(Booking booking) => Bookings.Add(booking);