diff --git a/backend/API/BusinessLogic/ShoppingListLogic.cs b/backend/API/BusinessLogic/ShoppingListLogic.cs index 1651896..d4ae617 100644 --- a/backend/API/BusinessLogic/ShoppingListLogic.cs +++ b/backend/API/BusinessLogic/ShoppingListLogic.cs @@ -84,7 +84,13 @@ namespace API.BusinessLogic user.ShoppingList.Add(newItem); } - return await _dbAccess.UpdateShoppingList(user); + bool succes = await _dbAccess.AddItemToShoppingList(user); + + var updatedShoppingList = await _dbAccess.ReadShoppingList(userId); + + if (succes) { return new OkObjectResult(updatedShoppingList.ShoppingList.Where(s => s.Name == listItemDTO.Name)); } + + return new ConflictObjectResult(new { message = "Could not save to database" }); } // Gets the shoppinglist and tries to find the item and when it does it checks/unchecks that item diff --git a/backend/API/DBAccess/ShoppingListDBAccess.cs b/backend/API/DBAccess/ShoppingListDBAccess.cs index a9e7c68..d45b43a 100644 --- a/backend/API/DBAccess/ShoppingListDBAccess.cs +++ b/backend/API/DBAccess/ShoppingListDBAccess.cs @@ -34,5 +34,17 @@ namespace API.DBAccess return new ConflictObjectResult(new { message = "Could not save to database" }); } + + // Adds an item to the shoppinglist + public async Task AddItemToShoppingList(User user) + { + _context.Entry(user).State = EntityState.Modified; + + bool saved = await _context.SaveChangesAsync() >= 1; + + if (saved) { return true; } + + return false; + } } }