Refactor some code in one method
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
namespace Francesco.Recipes.World.Repositories.Recipe
|
namespace Francesco.Recipes.World.Repositories.Recipe
|
||||||
{
|
{
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using Francesco.Recipes.World.Constants;
|
||||||
using Francesco.Recipes.World.Data;
|
using Francesco.Recipes.World.Data;
|
||||||
using Francesco.Recipes.World.Models;
|
using Francesco.Recipes.World.Models;
|
||||||
using Francesco.Recipes.World.Models.BackendModels.Category;
|
using Francesco.Recipes.World.Models.BackendModels.Category;
|
||||||
@@ -158,52 +160,17 @@
|
|||||||
return await _context.Recipes
|
return await _context.Recipes
|
||||||
.OrderByDescending(r => r.CreatedAt)
|
.OrderByDescending(r => r.CreatedAt)
|
||||||
.Take(20)
|
.Take(20)
|
||||||
.Select(r => new SearchViewModel
|
.Select(SearchViewModelSelector())
|
||||||
{
|
|
||||||
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();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
var normalizedSearchTerm = searchTerm.ToLower();
|
var normalizedSearchTerm = searchTerm.ToLower();
|
||||||
|
|
||||||
return await _context.Recipes
|
return await ApplyRecipeSearchFilter(_context.Recipes, normalizedSearchTerm)
|
||||||
.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)
|
.OrderByDescending(r => r.CreatedAt)
|
||||||
.Take(100)
|
.Take(100)
|
||||||
.Select(r => new SearchViewModel
|
.Select(SearchViewModelSelector())
|
||||||
{
|
.ToListAsync();
|
||||||
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -231,6 +198,35 @@
|
|||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Expression<Func<Recipe, SearchViewModel>> SearchViewModelSelector()
|
||||||
|
{
|
||||||
|
return 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(ContentType.Image))
|
||||||
|
.Select(m => m.Data)
|
||||||
|
.FirstOrDefault(),
|
||||||
|
MimeType = r.MediaFiles
|
||||||
|
.Where(m => m.MimeType != null && m.MimeType.StartsWith(ContentType.Image))
|
||||||
|
.Select(m => m.MimeType)
|
||||||
|
.FirstOrDefault(),
|
||||||
|
Ingredients = r.RecipeIngredients.Select(ri => ri.Ingredient.Name).ToList(),
|
||||||
|
TotalTime = r.PreparationTime.Add(r.CookingTime),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IQueryable<Recipe> ApplyRecipeSearchFilter(IQueryable<Recipe> query, string normalizedSearchTerm)
|
||||||
|
{
|
||||||
|
return query.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}%")));
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<bool> DeleteRecipeAsync(Guid recipeId)
|
public async Task<bool> DeleteRecipeAsync(Guid recipeId)
|
||||||
{
|
{
|
||||||
var recipe = await GetRecipeByIdAsync(recipeId);
|
var recipe = await GetRecipeByIdAsync(recipeId);
|
||||||
|
|||||||
Reference in New Issue
Block a user