From 7e1583f4853ad35421680fc0a3404ea722f8160d Mon Sep 17 00:00:00 2001 From: Jeas0001 Date: Thu, 8 May 2025 10:44:34 +0200 Subject: [PATCH] Changed delete item in shopping list to work and add item to only return an item and not an array with one item --- .../API/BusinessLogic/ShoppingListLogic.cs | 20 +++++++++---------- backend/API/BusinessLogic/UserLogic.cs | 2 +- backend/API/DBAccess/DBContext.cs | 2 +- backend/API/DBAccess/ShoppingListDBAccess.cs | 10 ++++++++++ .../{ShoppingList.cs => ShoppingListItem.cs} | 2 +- backend/API/Models/UserModels/User.cs | 2 +- 6 files changed, 24 insertions(+), 14 deletions(-) rename backend/API/Models/ShoppingListModels/{ShoppingList.cs => ShoppingListItem.cs} (88%) diff --git a/backend/API/BusinessLogic/ShoppingListLogic.cs b/backend/API/BusinessLogic/ShoppingListLogic.cs index d4ae617..6386134 100644 --- a/backend/API/BusinessLogic/ShoppingListLogic.cs +++ b/backend/API/BusinessLogic/ShoppingListLogic.cs @@ -28,11 +28,11 @@ namespace API.BusinessLogic { var user = await _dbAccess.ReadShoppingList(userId); - List shoppingList = user.ShoppingList; + List shoppingList = user.ShoppingList; if (shoppingList.Any(s => s.Name == listItemDTO.Name)) { - ShoppingList item = shoppingList.Where(s => s.Name == listItemDTO.Name).FirstOrDefault(); + ShoppingListItem item = shoppingList.Where(s => s.Name == listItemDTO.Name).FirstOrDefault(); shoppingList.Remove(item); if (item.Unit == listItemDTO.Unit) @@ -76,7 +76,7 @@ namespace API.BusinessLogic } else { - ShoppingList newItem = new ShoppingList(); + ShoppingListItem newItem = new ShoppingListItem(); newItem.Name = listItemDTO.Name; newItem.Amount = listItemDTO.Amount; newItem.Unit = listItemDTO.Unit; @@ -88,7 +88,7 @@ namespace API.BusinessLogic var updatedShoppingList = await _dbAccess.ReadShoppingList(userId); - if (succes) { return new OkObjectResult(updatedShoppingList.ShoppingList.Where(s => s.Name == listItemDTO.Name)); } + if (succes) { return new OkObjectResult(updatedShoppingList.ShoppingList.Where(s => s.Name == listItemDTO.Name).First()); } return new ConflictObjectResult(new { message = "Could not save to database" }); } @@ -140,11 +140,11 @@ namespace API.BusinessLogic { var user = await _dbAccess.ReadShoppingList(userId); - int itemIndex = user.ShoppingList.FindIndex(x => x.Id == itemId); + var item = user.ShoppingList.Where(x => x.Id == itemId).FirstOrDefault(); - user.ShoppingList.RemoveAt(itemIndex); + if (item == null) { return new ConflictObjectResult(new { message = "Could not find item" }); } - return await _dbAccess.UpdateShoppingList(user); + return await _dbAccess.DeleteItemFromShoppinglist(item); } // Adds an entire recipes ingredients to the shoppinglist @@ -156,11 +156,11 @@ namespace API.BusinessLogic foreach (var ingredient in ingredients) { - List shoppingList = user.ShoppingList; + List shoppingList = user.ShoppingList; if (shoppingList.Any(s => s.Name == ingredient.Name)) { - ShoppingList item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault(); + ShoppingListItem item = shoppingList.Where(s => s.Name == ingredient.Name).FirstOrDefault(); shoppingList.Remove(item); if (item.Unit == ingredient.Unit) @@ -205,7 +205,7 @@ namespace API.BusinessLogic } else { - ShoppingList newItem = new ShoppingList(); + ShoppingListItem newItem = new ShoppingListItem(); newItem.Name = ingredient.Name; newItem.Amount = ingredient.Amount; newItem.Unit = ingredient.Unit; diff --git a/backend/API/BusinessLogic/UserLogic.cs b/backend/API/BusinessLogic/UserLogic.cs index f32bd2e..1cbf349 100644 --- a/backend/API/BusinessLogic/UserLogic.cs +++ b/backend/API/BusinessLogic/UserLogic.cs @@ -67,7 +67,7 @@ namespace API.BusinessLogic Password = hashedPassword, Salt = salt, Recipes = new List(), - ShoppingList = new List(), + ShoppingList = new List(), }; return await _dbAccess.CreateUser(user); diff --git a/backend/API/DBAccess/DBContext.cs b/backend/API/DBAccess/DBContext.cs index b18f88e..058193a 100644 --- a/backend/API/DBAccess/DBContext.cs +++ b/backend/API/DBAccess/DBContext.cs @@ -11,7 +11,7 @@ namespace API.DBAccess public DbSet Recipes { get; set; } - public DbSet ShoppingList { get; set; } + public DbSet ShoppingList { get; set; } public DBContext(DbContextOptions options) : base(options) { } } diff --git a/backend/API/DBAccess/ShoppingListDBAccess.cs b/backend/API/DBAccess/ShoppingListDBAccess.cs index d45b43a..dd8b50a 100644 --- a/backend/API/DBAccess/ShoppingListDBAccess.cs +++ b/backend/API/DBAccess/ShoppingListDBAccess.cs @@ -46,5 +46,15 @@ namespace API.DBAccess return false; } + + public async Task DeleteItemFromShoppinglist(ShoppingListItem item) + { + _context.ShoppingList.Remove(item); + bool saved = await _context.SaveChangesAsync() >= 0; + + if (saved) { return new OkObjectResult(saved); } + + return new ConflictObjectResult(new { message = "Could not save to database" }); + } } } diff --git a/backend/API/Models/ShoppingListModels/ShoppingList.cs b/backend/API/Models/ShoppingListModels/ShoppingListItem.cs similarity index 88% rename from backend/API/Models/ShoppingListModels/ShoppingList.cs rename to backend/API/Models/ShoppingListModels/ShoppingListItem.cs index 648e9dc..badee1d 100644 --- a/backend/API/Models/ShoppingListModels/ShoppingList.cs +++ b/backend/API/Models/ShoppingListModels/ShoppingListItem.cs @@ -1,6 +1,6 @@ namespace API.Models.ShoppingListModels { - public class ShoppingList + public class ShoppingListItem { public int Id { get; set; } diff --git a/backend/API/Models/UserModels/User.cs b/backend/API/Models/UserModels/User.cs index 3b13e51..dd196d5 100644 --- a/backend/API/Models/UserModels/User.cs +++ b/backend/API/Models/UserModels/User.cs @@ -21,6 +21,6 @@ namespace API.Models.UserModels public List Recipes { get; set; } - public List ShoppingList { get; set; } + public List ShoppingList { get; set; } } }