public async Task OnGetAsync(string sortType, string searchString, string currentFilter, int?pageIndex) { RecipeData = new RecipeIndexData(); RecipeData.Recipes = await _context.Recipes .Include(r => r.RecipeCategories) .ThenInclude(r => r.Category).ToListAsync(); CurrentSort = sortType; TitleSort = String.IsNullOrEmpty(sortType) ? "title_desc" : ""; PrepTimeSort = sortType == "PrepTime" ? "preptime_desc" : "PrepTime"; CookTimeSort = sortType == "CookTime" ? "cooktime_desc" : "CookTime"; FeedsQtySort = sortType == "FeedsQty" ? "feedsqty_desc" : "FeedsQty"; if (searchString != null) { pageIndex = 1; } else { searchString = currentFilter; } CurrentFilter = searchString; var recipesIQ = RecipeData.Recipes.AsQueryable <Models.Recipe>(); var result = getFilteredSorted(sortType, CurrentFilter, recipesIQ); int pageSize = 3; Recipes = PaginatedList <Models.Recipe> .Create(result.AsNoTracking(), pageIndex ?? 1, pageSize); RecipeData.Recipes = Recipes; AvgReviews = new Dictionary <int, int>(); foreach (var r in Recipes) { var reviews = _context.Reviews.Where(rev => rev.RecipeId == r.Id); int totalRevs = reviews.Count(); if (totalRevs == 0) { AvgReviews.Add(r.Id, 0); } else { int reviewsSum = 0; foreach (var i in reviews) { reviewsSum += i.Rating; } AvgReviews.Add(r.Id, reviewsSum / totalRevs); } } }
public async Task OnGetAsync(string sortType, string searchString, string currentFilter, int?pageIndex) { RecipeData = new RecipeIndexData(); RecipeData.Recipes = await _context.Recipes .Include(r => r.RecipeCategories) .ThenInclude(r => r.Category).ToListAsync(); CurrentSort = sortType; TitleSort = String.IsNullOrEmpty(sortType) ? "title_desc" : ""; PrepTimeSort = sortType == "PrepTime" ? "preptime_desc" : "PrepTime"; CookTimeSort = sortType == "CookTime" ? "cooktime_desc" : "CookTime"; FeedsQtySort = sortType == "FeedsQty" ? "feedsqty_desc" : "FeedsQty"; CreatorIdSort = sortType == "CreatorID" ? "creatorid_desc" : "CreatorID"; if (searchString != null) { pageIndex = 1; } else { searchString = currentFilter; } CurrentFilter = searchString; var recipesIQ = RecipeData.Recipes.AsQueryable <Models.Recipe>(); var result = getFilteredSorted(sortType, CurrentFilter, recipesIQ); //var recipesIQ = getFilteredSorted(sortType, CurrentFilter); int pageSize = 3; Recipes = PaginatedList <Models.Recipe> .Create(result.AsNoTracking(), pageIndex ?? 1, pageSize); RecipeData.Recipes = Recipes; }