public static PagedList <RecipeSearchModel> SearchRecipes(this IRecipeManager recipeManager, int page, int pageSize, RecipeSortType sortType, ViewModels.Filter filter) { if (pageSize < 1) { pageSize = 1; } ICollection <RecipeSearchModel> data = Mapper.Map <IEnumerable <Entities.Recipe>, IEnumerable <RecipeSearchModel> >( recipeManager.FindByName(filter.SearchKeyword ?? string.Empty)).ToList(); var orderedData = RecipeHelper.SortRecipes(recipeManager.FilterProducts(data, filter), sortType); int pages = (data.Count + pageSize - 1) / pageSize; if (page > pages) { page = pages; } if (page < 1) { page = 1; } return(new PagedList <RecipeSearchModel>(orderedData, page, pageSize)); }
private static IEnumerable <RecipeSearchModel> FilterProducts(this IRecipeManager recipeManager, IEnumerable <RecipeSearchModel> data, ViewModels.Filter filter) { if (filter.CategoryId.HasValue) { return(data.Where(p => (!filter.GlutenFree || p.GlutenFree) && recipeManager.InCategory(p.Id, filter.CategoryId.Value) && p.InRangeCarbohydrate(filter.CarbRange) && p.InRangeFat(filter.FatRange) && p.InRangeProtein(filter.ProteinRange))); } else { return(data.Where(p => (!filter.GlutenFree || p.GlutenFree) && p.InRangeCarbohydrate(filter.CarbRange) && p.InRangeFat(filter.FatRange) && p.InRangeProtein(filter.ProteinRange))); } }