Change Search Logic
This commit is contained in:
@@ -13,10 +13,10 @@
|
|||||||
|
|
||||||
Task RemoveIngredientFromRecipeAsync(Guid recipeId, Guid ingredientId);
|
Task RemoveIngredientFromRecipeAsync(Guid recipeId, Guid ingredientId);
|
||||||
|
|
||||||
Task<IEnumerable<Recipe>> GetRecipesByNameAndIngredientAsync(string name, string ingredient);
|
|
||||||
|
|
||||||
Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty);
|
Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty);
|
||||||
|
|
||||||
Task<Recipe> CreateRecipeForCategoryAsync(Category category, string name, string description, Difficulty difficulty, int servings, TimeSpan preparationTime, TimeSpan cookingTime);
|
Task<Recipe> CreateRecipeForCategoryAsync(Category category, string name, string description, Difficulty difficulty, int servings, TimeSpan preparationTime, TimeSpan cookingTime);
|
||||||
|
|
||||||
|
Task<IEnumerable<Recipe>> GetRecipesBySearchQueryAsync(string query);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -147,30 +147,24 @@
|
|||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<Recipe>> GetRecipesByNameAndIngredientAsync(string name, string ingredient)
|
public async Task<IEnumerable<Recipe>> GetRecipesBySearchQueryAsync(string query)
|
||||||
{
|
{
|
||||||
var query = _context.Recipes
|
if (string.IsNullOrWhiteSpace(query))
|
||||||
|
{
|
||||||
|
return await _context.Recipes
|
||||||
.Include(r => r.RecipeIngredients)
|
.Include(r => r.RecipeIngredients)
|
||||||
.ThenInclude(ri => ri.Ingredient)
|
.ThenInclude(ri => ri.Ingredient)
|
||||||
.AsQueryable();
|
.ToListAsync();
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(name))
|
|
||||||
{
|
|
||||||
query = query.Where(r => r.Name.Contains(name, StringComparison.OrdinalIgnoreCase));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(ingredient))
|
query = query.ToLower();
|
||||||
{
|
|
||||||
var ingredientMatches = await _ingredientRepository.GetIngredientsByNameAsync(ingredient);
|
|
||||||
var ingredientIds = ingredientMatches.Select(i => i.Id).ToList();
|
|
||||||
|
|
||||||
if (ingredientIds.Any())
|
return await _context.Recipes
|
||||||
{
|
.Include(r => r.RecipeIngredients)
|
||||||
query = query.Where(r => r.RecipeIngredients.Any(ri => ingredientIds.Contains(ri.Ingredient.Id)));
|
.ThenInclude(ri => ri.Ingredient)
|
||||||
}
|
.Where(r => r.Name.ToLower().Contains(query) ||
|
||||||
}
|
r.RecipeIngredients.Any(ri => ri.Ingredient.Name.ToLower().Contains(query)))
|
||||||
|
.ToListAsync();
|
||||||
return await query.ToListAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty)
|
public async Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty)
|
||||||
|
|||||||
Reference in New Issue
Block a user