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,38 +111,41 @@ 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;
foreach (var ingredient in ingredients) for (int i = 0; i < multiplier; i++)
{ {
List<ShoppingListItem> shoppingList = user.ShoppingList; foreach (var ingredient in ingredients)
if (shoppingList.Any(s => s.Name == ingredient.Name))
{ {
ShoppingListItem item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault(); List<ShoppingListItem> shoppingList = user.ShoppingList;
shoppingList.Remove(item);
ShoppingListItemDTO listItemDTO = new ShoppingListItemDTO(); if (shoppingList.Any(s => s.Name == ingredient.Name))
listItemDTO.Name = ingredient.Name; {
listItemDTO.Amount = ingredient.Amount; ShoppingListItem item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault();
listItemDTO.Unit = ingredient.Unit; shoppingList.Remove(item);
listItemDTO.Checked = false;
user.ShoppingList.Add(await UnitAdjustmentSameName(listItemDTO, item)); ShoppingListItemDTO listItemDTO = new ShoppingListItemDTO();
} listItemDTO.Name = ingredient.Name;
else listItemDTO.Amount = ingredient.Amount;
{ listItemDTO.Unit = ingredient.Unit;
ShoppingListItem newItem = new ShoppingListItem(); listItemDTO.Checked = false;
newItem.Name = ingredient.Name;
newItem.Amount = ingredient.Amount;
newItem.Unit = ingredient.Unit;
newItem.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));
}
} }
} }

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);
} }
} }
} }