public IActionResult Index(RoomIndexViewModel model) { //1. Initialize pager #region Pagination model.Pager = model.Pager ?? new PagerViewModel(); model.Pager.CurrentPage = model.Pager.CurrentPage <= 0 ? 1 : model.Pager.CurrentPage; model.Pager.ItemsPerPage = model.Pager.ItemsPerPage <= 0 ? 10 : model.Pager.ItemsPerPage; #endregion #region Filter //2. Initialize Filter model.Filter = model.Filter ?? new RoomFilterViewModel(); //TODO:Fix Filters //3. Check if the filter is active bool emptyCapacity = (model.Filter.Capacity <= 0); bool emptyType = (model.Filter.Type == null); bool emptyIsAvailable = !model.Filter.IsAvailable; //4. Query IQueryable <Room> rooms = _roomRepository.Items .Where(item => (emptyCapacity || (item.Capacity == model.Filter.Capacity)) && (emptyType || (item.Type == model.Filter.Type)) && (emptyIsAvailable || (item.IsAvailable == model.Filter.IsAvailable))); #endregion Filter //5. Build view model object //Calculate total pages model.Pager.Pages = (int)Math.Ceiling((double)rooms.Count() / model.Pager.ItemsPerPage); //Calculate which rooms to show on the current page and order them by whether they are available or not rooms = rooms.OrderByDescending(item => item.IsAvailable).ThenBy(item => item.RoomNumber) .Skip((model.Pager.CurrentPage - 1) * model.Pager.ItemsPerPage) .Take(model.Pager.ItemsPerPage); model.Items = rooms.Select(item => new RoomViewModel() { Id = item.Id, Capacity = item.Capacity, Type = item.Type, IsAvailable = item.IsAvailable, //Reservation AdultBedPrice = item.AdultBedPrice, ChildBedPrice = item.ChildBedPrice, RoomNumber = item.RoomNumber }); return(View(model)); }
public async Task <IActionResult> Index(int id = 1, int pageSize = 10, bool availableOnly = false, RoomType[] type = null, int minCapacity = 0) { var searchResults = await roomService.GetSearchResults <RoomViewModel>(availableOnly, type, minCapacity); var resultsCount = searchResults.Count(); if (pageSize <= 0) { pageSize = 10; } var pages = (int)Math.Ceiling((double)resultsCount / pageSize); if (id <= 0 || id > pages) { id = 1; } var model = new RoomIndexViewModel { PagesCount = pages, CurrentPage = id, Rooms = searchResults.GetPageItems(id, pageSize), Controller = "Rooms", Action = nameof(Index), BreakfastPrice = await memoryCache.GetBreakfastPrice(settingService), AllInclusivePrice = await memoryCache.GetAllInclusivePrice(settingService), MaxCapacity = await roomService.GetMaxCapacity(), AvailableOnly = availableOnly, MinCapacity = minCapacity, Types = type, }; return(View(model)); }