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
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 recipe = await _recipeDBAccess.ReadRecipe(recipeId);
var ingredients = recipe.Ingredients;
foreach (var ingredient in ingredients)
for (int i = 0; i < multiplier; i++)
{
List<ShoppingListItem> 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<ShoppingListItem> 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));
}
}
}

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>
[Authorize]
[HttpPost("recipeadd")]
public async Task<IActionResult> AddARecipesItems(int recipeId)
public async Task<IActionResult> 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);
}
}
}