Compare commits

..

No commits in common. "c423a9e19d5a3eada28b46789f863b074c226996" and "cb2df85e5b6170d44d1cabb728bb7304138410ac" have entirely different histories.

2 changed files with 25 additions and 28 deletions

View File

@ -111,41 +111,38 @@ 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, int multiplier) public async Task<IActionResult> AddRecipeToShoppingList(int userId, int recipeId)
{ {
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;
if (shoppingList.Any(s => s.Name == ingredient.Name))
{ {
List<ShoppingListItem> shoppingList = user.ShoppingList; ShoppingListItem item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault();
shoppingList.Remove(item);
if (shoppingList.Any(s => s.Name == ingredient.Name)) ShoppingListItemDTO listItemDTO = new ShoppingListItemDTO();
{ listItemDTO.Name = ingredient.Name;
ShoppingListItem item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault(); listItemDTO.Amount = ingredient.Amount;
shoppingList.Remove(item); listItemDTO.Unit = ingredient.Unit;
listItemDTO.Checked = false;
ShoppingListItemDTO listItemDTO = new ShoppingListItemDTO(); user.ShoppingList.Add(await UnitAdjustmentSameName(listItemDTO, item));
listItemDTO.Name = ingredient.Name; }
listItemDTO.Amount = ingredient.Amount; else
listItemDTO.Unit = ingredient.Unit; {
listItemDTO.Checked = false; ShoppingListItem newItem = new ShoppingListItem();
newItem.Name = ingredient.Name;
newItem.Amount = ingredient.Amount;
newItem.Unit = ingredient.Unit;
newItem.Checked = false;
user.ShoppingList.Add(await UnitAdjustmentSameName(listItemDTO, item)); user.ShoppingList.Add(await UnitAdjustment(newItem));
}
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, int multiplier = 1) public async Task<IActionResult> AddARecipesItems(int recipeId)
{ {
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, multiplier); return await _shoppingListLogic.AddRecipeToShoppingList(userId, recipeId);
} }
} }
} }