Example #1
0
        private List <Room> FilterRooms(AvailableHotelsViewModel vm, List <Room> rooms)
        {
            var hours = Convert.ToDecimal((vm.CheckOut - vm.CheckIn).TotalHours / 24);

            if (vm.IsFamilyRoom)
            {
                rooms = rooms.Where(r => r.IsFamilyRoom == vm.IsFamilyRoom).ToList();
            }
            if (vm.HasParkinglot)
            {
                rooms = rooms.Where(r => r.Hotel.HasParkinglot == vm.HasParkinglot).ToList();
            }
            if (vm.HasFreeWiFi)
            {
                rooms = rooms.Where(r => r.Hotel.HasFreeWiFi == vm.HasFreeWiFi).ToList();
            }
            if (vm.IsPetFriendly)
            {
                rooms = rooms.Where(r => r.Hotel.IsPetFriendly == vm.IsPetFriendly).ToList();
            }
            if (vm.Breakfast)
            {
                rooms = rooms.Where(r => r.Hotel.HasBreakfast == vm.Breakfast).ToList();
            }
            return(rooms);
        }
Example #2
0
        public async Task <IActionResult> AvailableHotels(AvailableHotelsViewModel vm)
        {
            ViewBag.Title = "Результаты поиска";
            if (vm.CheckIn < DateTime.Now)             // проверка дата заезда и текущей даты
            {
                ModelState.AddModelError(string.Empty, "Дата заезда должна быть в будущем");
            }
            if (vm.CheckOut < vm.CheckIn)             // проверка дата заезда и отъезда
            {
                ModelState.AddModelError(string.Empty, "Дата отъезда не может быть раньше заезда");
            }
            if (ModelState.ErrorCount > 0)
            {
                return(View("Index", vm));
            }
            decimal days = Convert.ToDecimal((vm.CheckOut - vm.CheckIn).TotalHours / 24);

            var rooms = await db.Rooms
                        .Include(r => r.Hotel)
                        .Include(r => r.BookingHistories)
                        .Where(r => (r.Hotel.City.Name.ToUpper() == vm.City.Trim().ToUpper()) &&
                               (!r.BookingHistories.Any(bh => ((bh.CheckOut >= vm.CheckIn && bh.CheckOut <= vm.CheckOut) ||
                                                               (bh.CheckIn >= vm.CheckIn && bh.CheckIn <= vm.CheckOut))) ||
                                r.BookingHistories.Count == 0) &&
                               (r.Price * days >= vm.MinPrice && r.Price * days <= vm.MaxPrice))
                        .ToListAsync();

            if (vm.Filter)
            {
                rooms = FilterRooms(vm, rooms).ToList();
            }

            var groupedRooms = rooms.GroupBy(r => r.HotelId);
            List <AvailableHotelDTO> hotels = new List <AvailableHotelDTO>();

            foreach (var a in groupedRooms)
            {
                var availableHotel = new AvailableHotelDTO();
                foreach (var b in a)
                {
                    availableHotel.HotelName          = b.Hotel.Name;
                    availableHotel.HotelAddress       = b.Hotel.Address;
                    availableHotel.DistanceFromCenter = b.Hotel.DistanceFromCenter;
                    availableHotel.HotelRating        = b.Hotel.StarRating;
                    availableHotel.HasParkinglot      = b.Hotel.HasParkinglot;
                    availableHotel.IsPetFriendly      = b.Hotel.IsPetFriendly;
                    availableHotel.HasFreeWiFi        = b.Hotel.HasFreeWiFi;
                    availableHotel.HasBreakfast       = b.Hotel.HasBreakfast;
                    availableHotel.Rooms.Add(b);
                }
                hotels.Add(availableHotel);
            }

            return(View(new AvailableHotelsViewModel()
            {
                Hotels = hotels,
                City = vm.City,
                CheckIn = vm.CheckIn,
                CheckOut = vm.CheckOut,
                MaxPrice = vm.MaxPrice,
                MinPrice = vm.MinPrice,
                Breakfast = vm.Breakfast,
                HasFreeWiFi = vm.HasFreeWiFi,
                IsFamilyRoom = vm.IsFamilyRoom,
                HasParkinglot = vm.HasParkinglot,
                IsPetFriendly = vm.IsPetFriendly,
                Filter = vm.Filter
            }));
        }