diff --git a/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs b/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs index f021a8f..0d9ddf3 100644 --- a/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs +++ b/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs @@ -45,7 +45,7 @@ } [HttpPost("ReplaceInstructionImage")] - [ValidateAntiForgeryToken] + [AutoValidateAntiforgeryToken] public async Task ReplaceInstructionImage(Guid instructionId, Guid mediaFileIdToReplace, IFormFile? newPhoto) { if (newPhoto is null) diff --git a/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs b/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs index 7747880..648cc7a 100644 --- a/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs +++ b/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs @@ -4,7 +4,6 @@ using Francesco.Recipes.World.Models; using Francesco.Recipes.World.Repositories.ShoppingList; using Microsoft.AspNetCore.Mvc; - using Microsoft.EntityFrameworkCore; public class ShoppingListController : Controller { @@ -21,17 +20,17 @@ [ValidateAntiForgeryToken] public async Task CreateOrAddIngredients([FromBody] CreateOrAddIngredientRequestModel request) { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + if (request == null || request.IngredientIds == null || !request.IngredientIds.Any()) { return BadRequest("No ingredients provided."); } - await _shoppingListRepository.AddIngredientsToShoppingListAsync(request.RecipeId, request.IngredientIds); - - var shoppingList = await _context.ShoppingLists - .Include(sl => sl.RecipeShoppingList) - .ThenInclude(rsl => rsl.Recipe) - .FirstOrDefaultAsync(sl => sl.RecipeShoppingList.Any(rsl => rsl.Recipe.Id == request.RecipeId)); + var shoppingList = await _shoppingListRepository.AddIngredientsToShoppingListAsync(request.RecipeId, request.IngredientIds); if (shoppingList == null) { diff --git a/Francesco.Recipes.World/Program.cs b/Francesco.Recipes.World/Program.cs index f5f164d..40317b6 100644 --- a/Francesco.Recipes.World/Program.cs +++ b/Francesco.Recipes.World/Program.cs @@ -7,7 +7,6 @@ using Francesco.Recipes.World.Repositories.MediaFile; using Francesco.Recipes.World.Repositories.Recipe; using Francesco.Recipes.World.Repositories.ShoppingList; using Francesco.Recipes.World.Repositories.Unit; - using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); diff --git a/Francesco.Recipes.World/Repositories/MediaFile/MediaFileRepository.cs b/Francesco.Recipes.World/Repositories/MediaFile/MediaFileRepository.cs index 15f1fc6..ef3babb 100644 --- a/Francesco.Recipes.World/Repositories/MediaFile/MediaFileRepository.cs +++ b/Francesco.Recipes.World/Repositories/MediaFile/MediaFileRepository.cs @@ -92,7 +92,7 @@ { await RemoveExistingMediaAsync(recipe, "image/"); } - else if (isVideo) + else { await RemoveExistingMediaAsync(recipe, "video/"); } diff --git a/Francesco.Recipes.World/Repositories/Recipe/RecipeRepository.cs b/Francesco.Recipes.World/Repositories/Recipe/RecipeRepository.cs index 1b7470a..f71303f 100644 --- a/Francesco.Recipes.World/Repositories/Recipe/RecipeRepository.cs +++ b/Francesco.Recipes.World/Repositories/Recipe/RecipeRepository.cs @@ -52,9 +52,11 @@ } var ingredients = await _ingredientRepository.GetIngredientsByNameAsync(ingredientName); + var exactMatch = ingredients + .FirstOrDefault(i => i.Name.Equals(ingredientName, StringComparison.OrdinalIgnoreCase)); Ingredient ingredient; - if (!ingredients.Any()) + if (exactMatch == null) { ingredient = new Ingredient { @@ -66,7 +68,7 @@ } else { - ingredient = ingredients.First(); + ingredient = exactMatch; } var existingEntry = await _context.RecipeIngredients @@ -83,6 +85,7 @@ Unit = unit, Quantity = quantity, }; + _context.Add(recipeIngredient); await _context.SaveChangesAsync(); } diff --git a/Francesco.Recipes.World/Repositories/ShoppingList/IShoppingListRepository.cs b/Francesco.Recipes.World/Repositories/ShoppingList/IShoppingListRepository.cs index 5411e89..90c35c0 100644 --- a/Francesco.Recipes.World/Repositories/ShoppingList/IShoppingListRepository.cs +++ b/Francesco.Recipes.World/Repositories/ShoppingList/IShoppingListRepository.cs @@ -2,10 +2,11 @@ { using Francesco.Recipes.World.Models.BackendModels.IngredientShoppingList; using Francesco.Recipes.World.Models.BackendModels.Recipe; + using Francesco.Recipes.World.Models.BackendModels.Shoppinglist; public interface IShoppingListRepository { - Task AddIngredientsToShoppingListAsync(Guid shoppingListId, List ingredientIds); + Task AddIngredientsToShoppingListAsync(Guid recipeId, List ingredientIds); Task> GetIngredientsOfRecipeInListAsync(Guid shoppingListRecipeId); diff --git a/Francesco.Recipes.World/Repositories/ShoppingList/ShoppingListRepository.cs b/Francesco.Recipes.World/Repositories/ShoppingList/ShoppingListRepository.cs index c3e8590..16d8934 100644 --- a/Francesco.Recipes.World/Repositories/ShoppingList/ShoppingListRepository.cs +++ b/Francesco.Recipes.World/Repositories/ShoppingList/ShoppingListRepository.cs @@ -16,7 +16,7 @@ _context = context; } - public async Task AddIngredientsToShoppingListAsync(Guid recipeId, List ingredientIds) + public async Task AddIngredientsToShoppingListAsync(Guid recipeId, List ingredientIds) { if (ingredientIds == null || !ingredientIds.Any()) { @@ -29,13 +29,12 @@ if (recipe == null) { - throw new Exception("Rezept nicht gefunden."); + throw new Exception("Recipe not found."); } var shoppingList = await _context.ShoppingLists .Include(sl => sl.RecipeShoppingList) .ThenInclude(rsl => rsl.SelectedIngredients) - .ThenInclude(si => si.RecipeIngredient) .Include(sl => sl.RecipeShoppingList) .ThenInclude(rsl => rsl.Recipe) .FirstOrDefaultAsync(sl => sl.RecipeShoppingList.Any(rsl => rsl.Recipe.Id == recipeId)); @@ -46,7 +45,7 @@ if (!recipeIngredients.Any()) { - throw new Exception("Keine gültigen Zutaten gefunden."); + throw new Exception("No valid ingredients found."); } if (shoppingList == null) @@ -55,7 +54,6 @@ { Id = Guid.NewGuid(), CreatedAt = DateTime.UtcNow, - ModifiedAt = null, RecipeShoppingList = new List(), }; @@ -93,10 +91,7 @@ foreach (var ri in recipeIngredients) { - var alreadyExists = existingRecipeList.SelectedIngredients - .Any(si => si.RecipeIngredient.Id == ri.Id); - - if (!alreadyExists) + if (!existingRecipeList.SelectedIngredients.Any(si => si.RecipeIngredient.Id == ri.Id)) { existingRecipeList.SelectedIngredients.Add(new RecipeIngredientShoppingList { @@ -109,6 +104,9 @@ shoppingList.ModifiedAt = DateTime.UtcNow; } + + await _context.SaveChangesAsync(); + return shoppingList; } public async Task> GetIngredientsOfRecipeInListAsync(Guid shoppingListRecipeId) @@ -153,8 +151,6 @@ public async Task DeleteShoppingListAsync(Guid shoppingListId) { var list = await _context.ShoppingLists - .Include(sl => sl.RecipeShoppingList) - .ThenInclude(r => r.SelectedIngredients) .FirstOrDefaultAsync(sl => sl.Id == shoppingListId); if (list != null) diff --git a/Francesco.Recipes.World/Views/Shared/_Layout.cshtml b/Francesco.Recipes.World/Views/Shared/_Layout.cshtml index 1f862ba..9d445a9 100644 --- a/Francesco.Recipes.World/Views/Shared/_Layout.cshtml +++ b/Francesco.Recipes.World/Views/Shared/_Layout.cshtml @@ -44,6 +44,7 @@ + @await RenderSectionAsync("Scripts", required: false)