From 14cf727e2202ced4816e6e453a5994e17aac797a Mon Sep 17 00:00:00 2001 From: Jeas0001 Date: Tue, 13 May 2025 12:13:07 +0200 Subject: [PATCH] Made it possible to set a multiplier on recipeadd --- .../API/BusinessLogic/ShoppingListLogic.cs | 49 ++++++++++--------- .../API/Controllers/ShoppingListController.cs | 4 +- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/backend/API/BusinessLogic/ShoppingListLogic.cs b/backend/API/BusinessLogic/ShoppingListLogic.cs index bcadc5c..dd61249 100644 --- a/backend/API/BusinessLogic/ShoppingListLogic.cs +++ b/backend/API/BusinessLogic/ShoppingListLogic.cs @@ -111,38 +111,41 @@ namespace API.BusinessLogic } // Adds an entire recipes ingredients to the shoppinglist - public async Task AddRecipeToShoppingList(int userId, int recipeId) + public async Task AddRecipeToShoppingList(int userId, int recipeId, int multiplier) { var user = await _dbAccess.ReadShoppingList(userId); var recipe = await _recipeDBAccess.ReadRecipe(recipeId); var ingredients = recipe.Ingredients; - - foreach (var ingredient in ingredients) + + for (int i = 0; i < multiplier; i++) { - List shoppingList = user.ShoppingList; - - if (shoppingList.Any(s => s.Name == ingredient.Name)) + foreach (var ingredient in ingredients) { - ShoppingListItem item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault(); - shoppingList.Remove(item); + List shoppingList = user.ShoppingList; - ShoppingListItemDTO listItemDTO = new ShoppingListItemDTO(); - listItemDTO.Name = ingredient.Name; - listItemDTO.Amount = ingredient.Amount; - listItemDTO.Unit = ingredient.Unit; - listItemDTO.Checked = false; + if (shoppingList.Any(s => s.Name == ingredient.Name)) + { + ShoppingListItem item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault(); + shoppingList.Remove(item); - user.ShoppingList.Add(await UnitAdjustmentSameName(listItemDTO, item)); - } - else - { - ShoppingListItem newItem = new ShoppingListItem(); - newItem.Name = ingredient.Name; - newItem.Amount = ingredient.Amount; - newItem.Unit = ingredient.Unit; - newItem.Checked = false; + ShoppingListItemDTO listItemDTO = new ShoppingListItemDTO(); + listItemDTO.Name = ingredient.Name; + listItemDTO.Amount = ingredient.Amount; + listItemDTO.Unit = ingredient.Unit; + listItemDTO.Checked = false; - user.ShoppingList.Add(await UnitAdjustment(newItem)); + user.ShoppingList.Add(await UnitAdjustmentSameName(listItemDTO, item)); + } + else + { + ShoppingListItem newItem = new ShoppingListItem(); + newItem.Name = ingredient.Name; + newItem.Amount = ingredient.Amount; + newItem.Unit = ingredient.Unit; + newItem.Checked = false; + + user.ShoppingList.Add(await UnitAdjustment(newItem)); + } } } diff --git a/backend/API/Controllers/ShoppingListController.cs b/backend/API/Controllers/ShoppingListController.cs index ddeb6c1..e4c049c 100644 --- a/backend/API/Controllers/ShoppingListController.cs +++ b/backend/API/Controllers/ShoppingListController.cs @@ -100,12 +100,12 @@ namespace API.Controllers /// returns a okobjectresult with a boolean that is true if it fails it returns a confliftobjectresult with a message of why it failed [Authorize] [HttpPost("recipeadd")] - public async Task AddARecipesItems(int recipeId) + public async Task AddARecipesItems(int recipeId, int multiplier = 1) { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); - return await _shoppingListLogic.AddRecipeToShoppingList(userId, recipeId); + return await _shoppingListLogic.AddRecipeToShoppingList(userId, recipeId, multiplier); } } }