public IActionResult GetHotelsFromSelection(int countryId, int cityId, string selectedHotelCategories, int page) { char[] sep = new char[] { ';' }; List <int> selectedHotelCategoriesList = new List <int>(); if (selectedHotelCategories != null) { foreach (string item in selectedHotelCategories.Trim().Split(sep)) { selectedHotelCategoriesList.Add(int.Parse(item)); } } List <Hotel> hotels = _context.Hotels.ToList(); if (countryId > 0) { hotels = hotels.Where(h => h.City.CountryId == countryId).ToList(); } if (cityId > 0) { hotels = hotels.Where(h => h.CityId == cityId).ToList(); } if (selectedHotelCategoriesList.Count > 0) { hotels = hotels.Where(h => selectedHotelCategoriesList.Contains(h.HotelCategoryId)).ToList(); } List <ShortHotelViewModel> hotelsModel = new List <ShortHotelViewModel>(); foreach (Hotel hotel in hotels) { List <decimal> prices = new List <decimal>(); foreach (Tour tour in _context.Tours.Where(t => t.HotelRoom.HotelId == hotel.Id).ToList()) { prices.Add(tour.Price); } ShortHotelViewModel item = new ShortHotelViewModel { Hotel = hotel, MinTourPrice = prices.Count > 0 ? prices.Min() : 0, MaxTourPrice = prices.Count > 0 ? prices.Max() : 0 }; hotelsModel.Add(item); } HotelOffersViewModel model = new HotelOffersViewModel { Hotels = hotelsModel, HotelOffersPagination = new HotelOffersPaginationViewModel(hotelsModel.Count, page) }; return(ViewComponent("HotelOffers", new { hotelOffersViewModel = model })); }
public IActionResult Index() { List <SelectListItem> countries = new List <SelectListItem> { new SelectListItem("Choose country", "-1", true, true), new SelectListItem("All Countries", "0") }; foreach (Country country in _context.Countries.ToList()) { countries.Add(new SelectListItem(country.Name, country.Id.ToString())); } List <SelectListItem> cities = new List <SelectListItem> { new SelectListItem("Choose city", "-1", true, true), new SelectListItem("All Cities", "0") }; List <SelectListItem> hotelCategories = new List <SelectListItem>(); foreach (HotelCategory category in _context.HotelCategories.ToList()) { hotelCategories.Add(new SelectListItem(category.Name, category.Id.ToString())); } HotelOffersPaginationViewModel paginationViewModel = new HotelOffersPaginationViewModel(_context.Hotels.ToList().Count, 1); List <ShortHotelViewModel> hotels = new List <ShortHotelViewModel>(); foreach (Hotel hotel in _context.Hotels.ToList()) { List <decimal> prices = new List <decimal>(); foreach (Tour tour in _context.Tours.Where(t => t.HotelRoom.HotelId == hotel.Id).ToList()) { prices.Add(tour.Price); } ShortHotelViewModel item = new ShortHotelViewModel { Hotel = hotel, MinTourPrice = prices.Count > 0 ? prices.Min() : 0, MaxTourPrice = prices.Count > 0 ? prices.Max() : 0 }; hotels.Add(item); } MainViewModel model = new MainViewModel { HotelSelection = new HotelSelectionViewModel { Countries = countries, Cities = cities, HotelCategories = hotelCategories }, HotelOffers = new HotelOffersViewModel { HotelOffersPagination = paginationViewModel, Hotels = hotels } }; return(View(model)); }