Optimize Searchfilter method
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
namespace Francesco.Recipes.World.Repositories.Recipe
|
namespace Francesco.Recipes.World.Repositories.Recipe
|
||||||
{
|
{
|
||||||
|
using Francesco.Recipes.World.Models;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Category;
|
using Francesco.Recipes.World.Models.BackendModels.Category;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Recipe;
|
using Francesco.Recipes.World.Models.BackendModels.Recipe;
|
||||||
|
|
||||||
@@ -17,8 +18,7 @@
|
|||||||
|
|
||||||
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>> SearchInRecipesAndIngredients(string searchterm);
|
|
||||||
|
|
||||||
Task<bool> DeleteRecipeAsync(Guid recipeId);
|
Task<bool> DeleteRecipeAsync(Guid recipeId);
|
||||||
|
Task<IEnumerable<SearchViewModel>> SearchInRecipesAndIngredients(string searchTerm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
namespace Francesco.Recipes.World.Repositories.Recipe
|
namespace Francesco.Recipes.World.Repositories.Recipe
|
||||||
{
|
{
|
||||||
using Francesco.Recipes.World.Data;
|
using Francesco.Recipes.World.Data;
|
||||||
|
using Francesco.Recipes.World.Models;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Category;
|
using Francesco.Recipes.World.Models.BackendModels.Category;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Ingredient;
|
using Francesco.Recipes.World.Models.BackendModels.Ingredient;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Recipe;
|
using Francesco.Recipes.World.Models.BackendModels.Recipe;
|
||||||
@@ -148,21 +149,67 @@
|
|||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<Recipe>> SearchInRecipesAndIngredients(string searchTerm)
|
public async Task<IEnumerable<SearchViewModel>> SearchInRecipesAndIngredients(string searchTerm)
|
||||||
{
|
{
|
||||||
var queryable = _context.Recipes
|
try
|
||||||
.Include(r => r.RecipeIngredients)
|
|
||||||
.ThenInclude(ri => ri.Ingredient)
|
|
||||||
.AsQueryable();
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(searchTerm))
|
|
||||||
{
|
{
|
||||||
searchTerm = searchTerm.ToLower();
|
if (string.IsNullOrWhiteSpace(searchTerm))
|
||||||
queryable = queryable.Where(r => r.Name.ToLower().Contains(searchTerm) ||
|
{
|
||||||
r.RecipeIngredients.Any(ri => ri.Ingredient.Name.ToLower().Contains(searchTerm)));
|
return await _context.Recipes
|
||||||
}
|
.OrderByDescending(r => r.CreatedAt)
|
||||||
|
.Take(20)
|
||||||
|
.Select(r => new SearchViewModel
|
||||||
|
{
|
||||||
|
Id = r.Id,
|
||||||
|
Name = r.Name,
|
||||||
|
Description = r.Description,
|
||||||
|
IsFavorite = r.IsFavorite,
|
||||||
|
ImageData = r.MediaFiles
|
||||||
|
.Where(m => m.MimeType != null && m.MimeType.StartsWith("image/"))
|
||||||
|
.Select(m => m.Data)
|
||||||
|
.FirstOrDefault(),
|
||||||
|
MimeType = r.MediaFiles
|
||||||
|
.Where(m => m.MimeType != null && m.MimeType.StartsWith("image/"))
|
||||||
|
.Select(m => m.MimeType)
|
||||||
|
.FirstOrDefault(),
|
||||||
|
Ingredients = r.RecipeIngredients.Select(ri => ri.Ingredient.Name).ToList(),
|
||||||
|
TotalTime = r.PreparationTime.Add(r.CookingTime),
|
||||||
|
})
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
return await queryable.ToListAsync();
|
var normalizedSearchTerm = searchTerm.ToLower();
|
||||||
|
|
||||||
|
return await _context.Recipes
|
||||||
|
.Where(r => EF.Functions.Like(r.Name.ToLower(), $"%{normalizedSearchTerm}%") ||
|
||||||
|
(r.Description != null && EF.Functions.Like(r.Description.ToLower(), $"%{normalizedSearchTerm}%")) ||
|
||||||
|
r.RecipeIngredients.Any(ri => EF.Functions.Like(ri.Ingredient.Name.ToLower(), $"%{normalizedSearchTerm}%")))
|
||||||
|
.OrderByDescending(r => r.CreatedAt)
|
||||||
|
.Take(100)
|
||||||
|
.Select(r => new SearchViewModel
|
||||||
|
{
|
||||||
|
Id = r.Id,
|
||||||
|
Name = r.Name,
|
||||||
|
Description = r.Description,
|
||||||
|
IsFavorite = r.IsFavorite,
|
||||||
|
ImageData = r.MediaFiles
|
||||||
|
.Where(m => m.MimeType != null && m.MimeType.StartsWith("image/"))
|
||||||
|
.Select(m => m.Data)
|
||||||
|
.FirstOrDefault(),
|
||||||
|
MimeType = r.MediaFiles
|
||||||
|
.Where(m => m.MimeType != null && m.MimeType.StartsWith("image/"))
|
||||||
|
.Select(m => m.MimeType)
|
||||||
|
.FirstOrDefault(),
|
||||||
|
Ingredients = r.RecipeIngredients.Select(ri => ri.Ingredient.Name).ToList(),
|
||||||
|
TotalTime = r.PreparationTime.Add(r.CookingTime),
|
||||||
|
})
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error in SearchInRecipesAndIngredientsOptimized: {ex.Message}");
|
||||||
|
return new List<SearchViewModel>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty)
|
public async Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty)
|
||||||
|
|||||||
Reference in New Issue
Block a user