Made it possible to set a multiplier on recipeadd

This commit is contained in:
Jeas0001 2025-05-13 12:13:07 +02:00
parent f38333dc8c
commit 14cf727e22
2 changed files with 28 additions and 25 deletions

View File

@ -111,12 +111,14 @@ namespace API.BusinessLogic
} }
// Adds an entire recipes ingredients to the shoppinglist // Adds an entire recipes ingredients to the shoppinglist
public async Task<IActionResult> AddRecipeToShoppingList(int userId, int recipeId) public async Task<IActionResult> AddRecipeToShoppingList(int userId, int recipeId, int multiplier)
{ {
var user = await _dbAccess.ReadShoppingList(userId); var user = await _dbAccess.ReadShoppingList(userId);
var recipe = await _recipeDBAccess.ReadRecipe(recipeId); var recipe = await _recipeDBAccess.ReadRecipe(recipeId);
var ingredients = recipe.Ingredients; var ingredients = recipe.Ingredients;
for (int i = 0; i < multiplier; i++)
{
foreach (var ingredient in ingredients) foreach (var ingredient in ingredients)
{ {
List<ShoppingListItem> shoppingList = user.ShoppingList; List<ShoppingListItem> shoppingList = user.ShoppingList;
@ -145,6 +147,7 @@ namespace API.BusinessLogic
user.ShoppingList.Add(await UnitAdjustment(newItem)); user.ShoppingList.Add(await UnitAdjustment(newItem));
} }
} }
}
return await _dbAccess.UpdateShoppingList(user); return await _dbAccess.UpdateShoppingList(user);
} }

View File

@ -100,12 +100,12 @@ namespace API.Controllers
/// <returns>returns a okobjectresult with a boolean that is true if it fails it returns a confliftobjectresult with a message of why it failed</returns> /// <returns>returns a okobjectresult with a boolean that is true if it fails it returns a confliftobjectresult with a message of why it failed</returns>
[Authorize] [Authorize]
[HttpPost("recipeadd")] [HttpPost("recipeadd")]
public async Task<IActionResult> AddARecipesItems(int recipeId) public async Task<IActionResult> AddARecipesItems(int recipeId, int multiplier = 1)
{ {
var claims = HttpContext.User.Claims; var claims = HttpContext.User.Claims;
string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
int userId = Convert.ToInt32(userIdString); int userId = Convert.ToInt32(userIdString);
return await _shoppingListLogic.AddRecipeToShoppingList(userId, recipeId); return await _shoppingListLogic.AddRecipeToShoppingList(userId, recipeId, multiplier);
} }
} }
} }