Compare commits

...

2 Commits

Author SHA1 Message Date
Jeas0001
c423a9e19d Merge branch 'master' of git.reim.ar:ReiMerc/easyeat 2025-05-13 12:13:18 +02:00
Jeas0001
14cf727e22 Made it possible to set a multiplier on recipeadd 2025-05-13 12:13:07 +02:00
2 changed files with 28 additions and 25 deletions
backend/API

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