implement ingredient management methods

This commit is contained in:
franc
2025-04-08 14:30:12 +02:00
parent e438e8a114
commit ffebc376e1
2 changed files with 17 additions and 41 deletions
@@ -1,21 +1,16 @@
namespace Francesco.Recipes.World.Repositories.Ingredient namespace Francesco.Recipes.World.Repositories.Ingredient
{ {
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.RecipeIngredient; using Francesco.Recipes.World.Models.BackendModels.RecipeIngredient;
public interface IIngredientRepository public interface IIngredientRepository
{ {
Task<Ingredient> CreateIngredientToRecipeAsync(Recipe recipe, string ingredientName);
Task UpdateIngredientAsync(Ingredient ingredient); Task UpdateIngredientAsync(Ingredient ingredient);
Task<List<RecipeIngredient>> GetIngredientsByRecipeIdAsync(Guid recipeId); Task<List<RecipeIngredient>> GetIngredientsByRecipeIdAsync(Guid recipeId);
Task<List<Ingredient>> GetIngredientsByNameAsync(string name); Task<List<Ingredient>> GetIngredientsByNameAsync(string name);
Task RemoveIngredientFromRecipeAsync(Recipe recipe, Guid ingredientId);
Task<Ingredient> GetIngredientByIdAsync(Guid ingredientId); Task<Ingredient> GetIngredientByIdAsync(Guid ingredientId);
} }
} }
@@ -1,8 +1,8 @@
namespace Francesco.Recipes.World.Repositories.Ingredient
namespace Francesco.Recipes.World.Repositories.Ingredient
{ {
using Francesco.Recipes.World.Data; using Francesco.Recipes.World.Data;
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.RecipeIngredient; using Francesco.Recipes.World.Models.BackendModels.RecipeIngredient;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -16,48 +16,29 @@
_context = context; _context = context;
} }
public async Task<Ingredient> CreateIngredientToRecipeAsync(Recipe recipe, string ingredientName) public async Task UpdateRecipeIngredientAsync(RecipeIngredient recipeIngredient)
{ {
if (recipe is null) if (recipeIngredient == null)
{ {
throw new ArgumentException("Recipe not found", nameof(recipe)); throw new ArgumentNullException(nameof(recipeIngredient));
} }
var newIngredient = new Ingredient var existingRecipeIngredient = await _context.RecipeIngredients
{ .Include(ri => ri.Ingredient)
Id = Guid.NewGuid(), .Include(ri => ri.Unit)
Name = ingredientName, .FirstOrDefaultAsync(ri => ri.Id == recipeIngredient.Id);
};
var recipeIngredient = new RecipeIngredient if (existingRecipeIngredient == null)
{ {
Id = Guid.NewGuid(), throw new InvalidOperationException($"RecipeIngredient with ID {recipeIngredient.Id} not found.");
Recipe = recipe, }
Ingredient = newIngredient,
Quantity = 1,
};
_context.Ingredients.Add(newIngredient); existingRecipeIngredient.Quantity = recipeIngredient.Quantity;
_context.RecipeIngredients.Add(recipeIngredient); existingRecipeIngredient.Ingredient = recipeIngredient.Ingredient;
existingRecipeIngredient.Unit = recipeIngredient.Unit;
_context.RecipeIngredients.Update(existingRecipeIngredient);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return newIngredient;
}
public async Task RemoveIngredientFromRecipeAsync(Recipe recipe, Guid ingredientId)
{
if (recipe == null)
{
throw new ArgumentNullException(nameof(recipe));
}
var ingredientToRemove = recipe.RecipeIngredients.FirstOrDefault(i => i.Id == ingredientId);
if (ingredientToRemove != null)
{
recipe.RecipeIngredients.Remove(ingredientToRemove);
await _context.SaveChangesAsync();
}
} }
public async Task<List<Ingredient>> GetIngredientsByNameAsync(string name) public async Task<List<Ingredient>> GetIngredientsByNameAsync(string name)