From 5a5b3c5d8718e2fd0ecad86bd0f520674b347298 Mon Sep 17 00:00:00 2001 From: Francesco Lorenzo D'Amico Date: Tue, 27 May 2025 11:58:18 +0200 Subject: [PATCH] Add some Endpoint for DeleteOperation and update API Endpoint (Detail) --- .../ShoppingList/ShoppingListController.cs | 89 +++++++++++++++++-- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs b/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs index 7557edd..21b9973 100644 --- a/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs +++ b/Francesco.Recipes.World/Controller/ShoppingList/ShoppingListController.cs @@ -4,6 +4,7 @@ using Francesco.Recipes.World.Models; using Francesco.Recipes.World.Repositories.ShoppingList; using Microsoft.AspNetCore.Mvc; + using Microsoft.EntityFrameworkCore; [Route("ShoppingList")] public class ShoppingListController : Controller @@ -49,15 +50,28 @@ } // GET: /ShoppingList/Details/{id} - [HttpGet("Details/{id}")] - public async Task Details(Guid id) + [HttpGet("Details")] + public async Task Details() { - var shoppingList = await _shoppingListRepository.GetByIdAsync(id); var recipeCount = await _shoppingListRepository.CountAllRecipeShoppinglistsAsync(); - if (shoppingList == null) + var recipeIngredientToShoppingListMap = new Dictionary(); + var allEntries = await _context.RecipeIngredientsShoppingLists + .Where(risl => risl.RecipeIngredient != null) + .Select(risl => new + { + RecipeIngredientId = risl.RecipeIngredient.Id, + ShoppingListId = risl.Id, + }) + .ToListAsync(); + + foreach (var entry in allEntries) { - return NotFound(); + if (entry.RecipeIngredientId != Guid.Empty && + !recipeIngredientToShoppingListMap.ContainsKey(entry.RecipeIngredientId)) + { + recipeIngredientToShoppingListMap.Add(entry.RecipeIngredientId, entry.ShoppingListId); + } } var recipeInAnyShoppingList = (await _shoppingListRepository.GetAllShoppingListsAsync()) @@ -66,12 +80,75 @@ var viewModel = new ShoppingListDetailsViewModel { - ShoppingList = shoppingList, RecipeCount = recipeCount, RecipesInAnyShoppingList = recipeInAnyShoppingList, + RecipeIngredientToShoppingListMap = recipeIngredientToShoppingListMap, }; return View(viewModel); } + + [HttpDelete("RemoveIngredients")] + [ValidateAntiForgeryToken] + public async Task RemoveIngredients([FromBody] List recipeIngredientShoppingListIds) + { + if (recipeIngredientShoppingListIds == null || !recipeIngredientShoppingListIds.Any()) + { + return BadRequest("Keine Zutaten zum Entfernen angegeben."); + } + + try + { + var affectedRecipeIds = await _context.RecipeIngredientsShoppingLists + .Where(risl => recipeIngredientShoppingListIds.Contains(risl.Id)) + .Select(risl => risl.RecipeShoppingList.Recipe.Id) + .Distinct() + .ToListAsync(); + + await _shoppingListRepository.RemoveIngredientsFromShoppingListAsync(recipeIngredientShoppingListIds); + + var remainingRecipeIds = await _context.RecipeShoppingLists + .Select(rsl => rsl.Recipe.Id) + .ToListAsync(); + + var removedRecipeIds = affectedRecipeIds + .Where(id => !remainingRecipeIds.Contains(id)) + .ToList(); + + return Json(new { success = true, removedRecipeIds }); + } + catch (Exception ex) + { + return BadRequest(new { error = ex.Message }); + } + } + + [HttpDelete("RemoveRecipeFromList/{recipeId}")] + [ValidateAntiForgeryToken] + public async Task RemoveRecipeFromList(Guid recipeId) + { + try + { + var recipeShoppingLists = await _context.RecipeShoppingLists + .Where(rsl => rsl.Recipe.Id == recipeId) + .ToListAsync(); + + if (!recipeShoppingLists.Any()) + { + return NotFound($"Kein Einkaufslisten-Eintrag für Rezept mit ID {recipeId} gefunden."); + } + + foreach (var recipeShoppingList in recipeShoppingLists) + { + await _shoppingListRepository.RemoveRecipeFromShoppingListAsync(recipeShoppingList.Id); + } + + return Ok(new { success = true }); + } + catch (Exception ex) + { + return BadRequest(new { success = false, error = ex.Message }); + } + } } }