add recipe managment

This commit is contained in:
franc
2025-04-08 15:06:23 +02:00
parent 31fbafe806
commit ddd0cad36b
2 changed files with 34 additions and 18 deletions
@@ -2,12 +2,13 @@
{ {
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;
using Francesco.Recipes.World.Models.BackendModels.Unit;
public interface IRecipeRepository public interface IRecipeRepository
{ {
Task<Recipe> GetRecipeAsync(Guid recipeId); Task<Recipe> GetRecipeAsync(Guid recipeId);
Task<Recipe?> GetRecipeByIdAsync(Guid id);
Task AddOrCreateIngredientToRecipeAsync(Guid recipeId, string ingredientName, int quantity, Guid unitId); Task AddOrCreateIngredientToRecipeAsync(Guid recipeId, string ingredientName, int quantity, Guid unitId);
Task RemoveIngredientFromRecipeAsync(Guid recipeId, Guid ingredientId); Task RemoveIngredientFromRecipeAsync(Guid recipeId, Guid ingredientId);
@@ -16,8 +17,6 @@
Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty); Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty);
Task<Unit> AddUnitToRecipeAsync(string name, string symbol);
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);
} }
} }
@@ -5,7 +5,6 @@
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;
using Francesco.Recipes.World.Models.BackendModels.RecipeIngredient; using Francesco.Recipes.World.Models.BackendModels.RecipeIngredient;
using Francesco.Recipes.World.Models.BackendModels.Unit;
using Francesco.Recipes.World.Repositories.Ingredient; using Francesco.Recipes.World.Repositories.Ingredient;
using Francesco.Recipes.World.Repositories.Unit; using Francesco.Recipes.World.Repositories.Unit;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -30,6 +29,17 @@
return recipe ?? throw new InvalidDataException($"Address {recipeId} not found."); return recipe ?? throw new InvalidDataException($"Address {recipeId} not found.");
} }
public async Task<Recipe?> GetRecipeByIdAsync(Guid recipeId)
{
return await _context.Recipes
.Include(r => r.RecipeIngredients)
.ThenInclude(ri => ri.Ingredient)
.Include(r => r.RecipeIngredients)
.ThenInclude(ri => ri.Unit)
.Include(r => r.MediaFiles)
.FirstOrDefaultAsync(r => r.Id == recipeId);
}
public async Task AddOrCreateIngredientToRecipeAsync(Guid recipeId, string ingredientName, int quantity, Guid unitId) public async Task AddOrCreateIngredientToRecipeAsync(Guid recipeId, string ingredientName, int quantity, Guid unitId)
{ {
var recipe = await GetRecipeAsync(recipeId); var recipe = await GetRecipeAsync(recipeId);
@@ -77,13 +87,13 @@
} }
public async Task<Recipe> CreateRecipeForCategoryAsync( public async Task<Recipe> CreateRecipeForCategoryAsync(
Category category, Category category,
string name, string name,
string description, string description,
Difficulty difficulty, Difficulty difficulty,
int servings, int servings,
TimeSpan preparationTime, TimeSpan preparationTime,
TimeSpan cookingTime) TimeSpan cookingTime)
{ {
if (category == null) if (category == null)
{ {
@@ -162,14 +172,21 @@
public async Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty) public async Task<IReadOnlyCollection<Recipe>> GetRecipesByDifficultyAsync(Difficulty? difficulty)
{ {
return await _context.Recipes if (!difficulty.HasValue)
.Where(r => r.Difficulty == difficulty) {
.ToListAsync(); return await _context.Recipes
} .Include(r => r.Category)
.Include(r => r.RecipeIngredients)
.ThenInclude(ri => ri.Ingredient)
.ToListAsync();
}
public async Task<Unit> AddUnitToRecipeAsync(string name, string symbol) return await _context.Recipes
{ .Where(r => r.Difficulty == difficulty.Value)
return await _unitRepository.AddUnitAsync(name, symbol); .Include(r => r.Category)
.Include(r => r.RecipeIngredients)
.ThenInclude(ri => ri.Ingredient)
.ToListAsync();
} }
} }
} }