Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ public async Task<IActionResult> SearchIngredients([FromQuery] string? name, [Fr
return Ok(results);
}

// [HttpGet("recipes-Detailed")]
// public async Task<IActionResult> SearchDetailedRecipes([FromQuery] RecipeSearchRequest request)
// {
// var results = await recipeService.SearchRecipesDetailedAsync(request);
// return Ok(results);
// }

[HttpGet("recipes")]
public async Task<IActionResult> SearchRecipes([FromQuery] RecipeSearchRequest request)
{
var results = await recipeService.SearchRecipesDetailedAsync(request);
var results = await recipeService.SearchRecipesAsync(request);
return Ok(results);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public record RecipeSearchRequest(
float? MinProtein, float? MaxProtein,
float? MinFat, float? MaxFat,
float? MinCalories, float? MaxCalories,
int? Difficulty,
[FromQuery] int[]? Difficulty,
[FromQuery] int[]? TagIds,
[FromQuery] int[]? IngredientIds
);
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public async Task<List<RecipeDetailDto>> SearchRecipesDetailedAsync(RecipeSearch
}

if (request.MaxCookingTime.HasValue) query = query.Where(r => r.CookingTimeMin <= request.MaxCookingTime);
if (request.Difficulty.HasValue) query = query.Where(r => r.Difficulty == request.Difficulty);
//if (request.Difficulty.HasValue) query = query.Where(r => r.Difficulty == request.Difficulty);

// Фильтры по БЖУ и Калориям
if (request.MinCalories.HasValue) query = query.Where(r => r.Calories100g >= request.MinCalories);
Expand All @@ -221,6 +221,11 @@ public async Task<List<RecipeDetailDto>> SearchRecipesDetailedAsync(RecipeSearch
query = query.Where(r => r.Ingredients.Any(i => request.IngredientIds.Contains(i.IngredientId)));
}

if (request.Difficulty is { Length: > 0 })
{
query = query.Where(r => request.Difficulty.Contains(r.Difficulty));
}

var recipes = await query
.AsSplitQuery()
.Select(r => new RecipeDetailDto
Expand Down Expand Up @@ -267,4 +272,78 @@ public async Task<List<RecipeDetailDto>> SearchRecipesDetailedAsync(RecipeSearch

return recipes;
}

public async Task<List<RecipeListDto>> SearchRecipesAsync(RecipeSearchRequest request)
{
var query = context.Recipes.AsNoTracking();

if (!string.IsNullOrWhiteSpace(request.Title))
{
var recipeIds = await searchService.SearchRecipeIdsAsync(request.Title);

// Если Meili ничего не нашел, возвращаем пустой список, чтобы не делать запрос к БД
if (recipeIds.Length == 0)
{
Console.WriteLine($"No recipes found for {request.Title}");
return new List<RecipeListDto>();
}

query = query.Where(r => recipeIds.Contains(r.Id));
}

if (request.MaxCookingTime.HasValue) query = query.Where(r => r.CookingTimeMin <= request.MaxCookingTime);
//if (request.Difficulty.HasValue) query = query.Where(r => r.Difficulty == request.Difficulty);

// Фильтры по БЖУ и Калориям
if (request.MinCalories.HasValue) query = query.Where(r => r.Calories100g >= request.MinCalories);
if (request.MaxCalories.HasValue) query = query.Where(r => r.Calories100g <= request.MaxCalories);

if (request.MinProtein.HasValue) query = query.Where(r => r.Protein100g >= request.MinProtein);
if (request.MaxProtein.HasValue) query = query.Where(r => r.Protein100g <= request.MaxProtein);

if (request.MinFat.HasValue) query = query.Where(r => r.Fat100g >= request.MinFat);
if (request.MaxFat.HasValue) query = query.Where(r => r.Fat100g <= request.MaxFat);

if (request.MinCarb.HasValue) query = query.Where(r => r.Carb100g >= request.MinCarb);
if (request.MaxCarb.HasValue) query = query.Where(r => r.Carb100g <= request.MaxCarb);

if (request.Difficulty is { Length: > 0 })
{
query = query.Where(r => request.Difficulty.Contains(r.Difficulty));
}

if (request.TagIds is { Length: > 0 })
{
var targetIds = request.TagIds.Distinct().ToList();
int targetCount = targetIds.Count;
query = query.Where(r => r.Tags.Count(i => targetIds.Contains(i.TagId)) == targetCount);
}

if (request.IngredientIds is { Length: > 0 })
{
var targetIds = request.IngredientIds.Distinct().ToList();
int targetCount = targetIds.Count;

query = query.Where(r => r.Ingredients.Count(i => targetIds.Contains(i.IngredientId)) == targetCount);
}

var recipes = await query
.AsSplitQuery()
.Select(r => new RecipeListDto()
{
Id = r.Id,
Title = r.Title,
CookingTimeMin = r.CookingTimeMin,
Servings = r.Servings,
Difficulty = r.Difficulty,
PreviewImageUrl = r.Images
.OrderBy(i => i.Order)
.Select(i => i.Url)
.FirstOrDefault(),
Tags = r.Tags.Select(m2m => m2m.Tag.Name).ToList(),
})
.ToListAsync();

return recipes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ namespace CookifyAPI.Services;

public interface IRecipeService
{
Task<IEnumerable<RecipeListDto>> GetRecipesListAsync();
//Task<IEnumerable<RecipeListDto>> GetRecipesListAsync();
Task<RecipeDetailDto?> GetRecipeByIdAsync(int id);
Task<OffsetPagedResult<RecipeListDto>> GetRecipesOffsetAsync(int page);
//Task<OffsetPagedResult<RecipeListDto>> GetRecipesOffsetAsync(int page);
Task<KeysetPagedResult<RecipeListDto>> GetRecipesKeysetAsync(int? lastId);
Task<List<RecipeDetailDto>> SearchRecipesDetailedAsync(RecipeSearchRequest request);
//Task<List<RecipeDetailDto>> SearchRecipesDetailedAsync(RecipeSearchRequest request);
Task<List<RecipeListDto>> SearchRecipesAsync(RecipeSearchRequest request);
}
Loading