Ejemplo n.º 1
0
 public IEnumerable <Room> AllRooms()
 {
     using (var context = new BookingsContext())
     {
         return(context.Rooms.ToList());
     }
 }
Ejemplo n.º 2
0
 public static void Initialize(BookingsContext cntxt)
 {
     if (!cntxt.Bookings.Any())
     {
         var b = Booking.CreateNew("AG-12", DateTime.Now, DateTime.Now.AddDays(1), false, null, 0, 0m, 4, 4, 4, 4, 4, 4, 4, 4, 4);
         cntxt.Bookings.AddAsync(b);
     }
 }
Ejemplo n.º 3
0
 public int GetMaxUsedPlaces(DateTime startDate, DateTime endDate, int roomId)
 {
     using (var context = new BookingsContext())
     {
         //return GetMaxUsedPlacesCountByPeriod(context, context.Rooms.FirstOrDefault(r => r.Id == roomId), startDate, endDate);
         return(GetMaxUsedPlacesCountByPeriod(context, context.Rooms.FirstOrDefault(r => r.Id == roomId), startDate, endDate));
     }
 }
Ejemplo n.º 4
0
        private bool IsFreeRoom(Room room, DateTime startDate, DateTime endDate, Gender gender, int usedPlaces)
        {
            using (var context = new BookingsContext())
            {
                var busyRooms = GetBusyRooms(context, startDate, endDate, gender, usedPlaces);

                return(context.Rooms.ToList().Except(busyRooms).Any(r => r.Id == room.Id));
            }
        }
Ejemplo n.º 5
0
        public IEnumerable <Booking> Index()
        {
            using (var context = new BookingsContext())
            {
                var list = context.Bookings.Include(b => b.Room).ToList();

                return(list);
            }
        }
Ejemplo n.º 6
0
        public IEnumerable <Room> FreeRooms(DateTime startDate, DateTime endDate, Gender gender, int usedPlaces = 1)
        {
            using (var context = new BookingsContext())
            {
                var busyRooms = GetBusyRooms(context, startDate, endDate, gender, usedPlaces);

                var freeRooms = context.Rooms.Except(busyRooms).ToList();

                return(freeRooms);
            }
        }
Ejemplo n.º 7
0
        private IQueryable <Room> GetBusyRooms(BookingsContext context, DateTime startDate, DateTime endDate, Gender gender, int usedPlaces)
        {
            var busyByPlacesRooms = context.Bookings.Where(b => b.StartDate <endDate && b.EndDate> startDate)
                                    .GroupBy(b => b.Room)
                                    .Where(g => g.Key.AvailablePlaces - g.Sum(b => b.UsedPlaces) < usedPlaces)
                                    .Select(r => r.Key);

            var busyByAnotherGenderRooms = context.Bookings.Where(b => b.StartDate <endDate && b.EndDate> startDate && b.Gender != gender)
                                           .Select(b => b.Room).Distinct();

            //return context.Bookings.Where(b => b.StartDate < endDate && b.EndDate > startDate).GroupBy(b => b.Room).Count();// . Select(b => b.Room).Distinct();

            return(busyByPlacesRooms.Union(busyByAnotherGenderRooms).Distinct());
        }
Ejemplo n.º 8
0
        private int GetMaxUsedPlacesCountByPeriod_Wrong(BookingsContext context, Room room, DateTime startDate, DateTime endDate)
        {
            if (room == null)
            {
                throw new ArgumentNullException("Room");
            }

            var bookings = context.Bookings.Where(b => b.StartDate <endDate && b.EndDate> startDate && b.RoomId == room.Id);

            if (!bookings.Any())
            {
                return(0);
            }

            int maxUsedPlaces = 0;

            foreach (var bookingsOrdered in
                     new List <IEnumerable <Booking> > {
                bookings.OrderBy(b => b.StartDate), bookings.OrderBy(b => b.EndDate)
            })
            {
                var firstBooking = bookingsOrdered.First();

                int usedPlacesCounter = firstBooking.UsedPlaces;
                var startDate1        = firstBooking.StartDate;
                var endDate1          = firstBooking.EndDate;



                foreach (var booking in bookingsOrdered.Skip(1))
                {
                    if (IsDateRangesIntersect(startDate1, endDate1, booking.StartDate, booking.EndDate))
                    {
                        usedPlacesCounter += booking.UsedPlaces;
                    }
                    else
                    {
                        maxUsedPlaces     = Math.Max(maxUsedPlaces, usedPlacesCounter);
                        usedPlacesCounter = booking.UsedPlaces;
                    }

                    startDate1 = booking.StartDate;
                    endDate1   = booking.EndDate;
                }

                maxUsedPlaces = Math.Max(maxUsedPlaces, usedPlacesCounter);
            }

            return(maxUsedPlaces);
        }
Ejemplo n.º 9
0
        private void GenerateRooms()
        {
            using (var context = new BookingsContext())
            {
                for (int i = 0; i < 50; i++)
                {
                    context.Rooms.Add(new Room()
                    {
                        AvailablePlaces = 1, Number = 200 + i
                    });
                }

                context.SaveChanges();
            }
        }
Ejemplo n.º 10
0
        public IActionResult Add(Booking booking)
        {
            bool isAdded = false;

            using (var context = new BookingsContext())
            {
                var room = context.Rooms.First(r => r.Id == booking.RoomId);
                if (IsFreeRoom(room, booking.StartDate, booking.EndDate, booking.Gender, booking.UsedPlaces))
                {
                    context.Bookings.Add(booking);
                    context.SaveChanges();

                    isAdded = true;
                }
            }

            return(Ok(isAdded));
        }
Ejemplo n.º 11
0
        private int GetMaxUsedPlacesCountByPeriod(BookingsContext context, Room room, DateTime startDate, DateTime endDate)
        {
            var bookings = context.Bookings.Where(b => b.StartDate <endDate && b.EndDate> startDate && b.RoomId == room.Id);

            var arrivals   = bookings.Select(b => new { Date = b.StartDate, Operation = Operation.Plus, b.UsedPlaces });
            var departures = bookings.Select(b => new { Date = b.EndDate, Operation = Operation.Minus, b.UsedPlaces });

            var maxUsedPlaces = 0;
            var counter       = 0;

            foreach (var bookingEvent in arrivals.Union(departures).OrderBy(b => b.Date))
            {
                counter      += bookingEvent.Operation == Operation.Plus ? bookingEvent.UsedPlaces : -1 * bookingEvent.UsedPlaces;
                maxUsedPlaces = Math.Max(counter, maxUsedPlaces);
            }

            return(maxUsedPlaces);
        }
Ejemplo n.º 12
0
        private void GenerateBookings()
        {
            var random = new Random();

            using (var context = new BookingsContext())
            {
                var rooms = context.Rooms.ToListAsync();

                for (int i = 0; i < 100; i++)
                {
                    DateTime startDate = GetRandomStartDate(random);
                    context.Bookings.Add(new Booking()
                    {
                        Room = GetRandomRoom(random, rooms.Result), StartDate = startDate, EndDate = startDate.AddDays(random.Next(2, 10)), Gender = GetRamdomGender(random), UsedPlaces = 1
                    });
                }

                context.SaveChanges();
            }
        }
Ejemplo n.º 13
0
 public BookingsController(BookingsContext context)
 {
     _context = context;
 }
Ejemplo n.º 14
0
 public CreateModel(BookingsContext context)
 {
     _context = context;
 }