//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;
        }
Example #2
0
        public bool IsBookingPossible(Booking newBooking)
        {
            var result = Bookings.All(existingBooking => newBooking.From >= existingBooking.To || newBooking.To <= existingBooking.From);

            return(result);
        }
Example #3
0
 public void CreateBooking(Booking booking) => Bookings.Add(booking);