public async Task <(List <Recipe>, int)> getRecipes(int startat, int quantity, List <string> fields, RecipeFields orderField, OrderEnum order) { var recipesQuery = _context.Recipes.AsQueryable(); recipesQuery = ApplyMultifilter(recipesQuery, fields); recipesQuery = ApplyOrder(recipesQuery, orderField, order); var recipesId = await recipesQuery .Skip(startat).Take(quantity) .Select(x => x.recipeId) .ToListAsync(); var queryCount = _context.Recipes.AsQueryable(); queryCount = ApplyMultifilter(recipesQuery, fields); queryCount = ApplyOrder(queryCount, orderField, order); var totalCount = queryCount.Count(); List <Recipe> recipes = new List <Recipe> (); foreach (var item in recipesId) { var recipe = await getRecipe(item); if (recipe != null) { if (recipe.recipeTypeId != null) { var recipeType = await _recipeTypeService.getRecipeType(recipe.recipeTypeId.Value); if (recipeType != null) { recipe.typeDescription = recipeType.typeDescription; } } recipe.phases = new List <Phase> (); foreach (var phaseId in recipe.phasesId) { var phase = await _phaseService.getPhase(phaseId); if (phase != null) { recipe.phases.Add(phase); } } recipes.Add(recipe); } } return(recipes, totalCount); }
private IQueryable <Recipe> ApplyOrder(IQueryable <Recipe> query, RecipeFields orderField, OrderEnum order) { switch (orderField) { case RecipeFields.recipeCode: if (order == OrderEnum.Ascending) { query = query.OrderBy(x => x.recipeCode); } else { query = query.OrderByDescending(x => x.recipeCode); } break; case RecipeFields.recipeName: if (order == OrderEnum.Ascending) { query = query.OrderBy(x => x.recipeName); } else { query = query.OrderByDescending(x => x.recipeName); } break; case RecipeFields.recipeTypeId: if (order == OrderEnum.Ascending) { query = query.OrderBy(x => x.recipeTypeId); } else { query = query.OrderByDescending(x => x.recipeTypeId); } break; default: query = query.OrderBy(x => x.recipeId); break; } return(query); }
private IQueryable <Recipe> ApplyFilter(IQueryable <Recipe> query, RecipeFields fieldFilter, string fieldValue) { switch (fieldFilter) { case RecipeFields.recipeCode: query = query.Where(x => x.recipeCode.Contains(fieldValue)); break; case RecipeFields.recipeName: query = query.Where(x => x.recipeName.Contains(fieldValue)); break; case RecipeFields.recipeTypeId: query = query.Where(x => x.recipeTypeId == Convert.ToInt32(fieldValue)); break; default: break; } return(query); }