From 030ba38cdcaa69a0e616ce6d4c5539fb8ada7d6f Mon Sep 17 00:00:00 2001 From: birkon <birkon@stud.ntnu.no> Date: Wed, 26 Apr 2023 16:02:06 +0200 Subject: [PATCH] added remove + add item to shopping list methods --- .../v233/SmartMat/entity/ShoppingList.java | 11 +++++ .../v233/SmartMat/entity/product/Product.java | 10 +++++ .../SmartMat/service/ShoppingListService.java | 41 ++++++++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/ShoppingList.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/ShoppingList.java index 74d21777..45fe9b8c 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/ShoppingList.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/ShoppingList.java @@ -9,6 +9,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import ntnu.idatt2016.v233.SmartMat.entity.product.Product; +import java.util.ArrayList; import java.util.List; /** @@ -42,4 +43,14 @@ public class ShoppingList { @JsonIgnoreProperties("shoppingList") private List<Product> products; + + /** + * Adds a product to the shopping list + * @param product the product to add to the shopping list + */ + public void addProduct(Product product) { + if (products == null) + products = new ArrayList<>(); + products.add(product); + } } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java index 49aaad66..1772931f 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java @@ -97,4 +97,14 @@ public class Product{ public String toString(){ return String.valueOf(this.ean); } + + /** + * Adds a shopping list to the product + * @param shoppingList the shopping list to add to the product + */ + public void addShoppingList(ShoppingList shoppingList) { + if (shoppingLists == null) + shoppingLists = new ArrayList<>(); + shoppingLists.add(shoppingList); + } } \ No newline at end of file diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java index 033a13c7..a5d65b7c 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Optional; import ntnu.idatt2016.v233.SmartMat.entity.product.Product; +import ntnu.idatt2016.v233.SmartMat.repository.product.ProductRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -22,6 +23,9 @@ public class ShoppingListService { @Autowired ShoppingListRepository shoppingListRepository; + + @Autowired + ProductRepository productRepository; /** * Create and save a shopping list to the database @@ -76,8 +80,41 @@ public class ShoppingListService { } } - public Product addProductToShoppingList(long ean, long shoppingListId){ - return null; + /** + * Adds a product to a shopping list + * @param ean the ean of the product to add + * @param shoppingListId the id of the shopping list to add the product to + * @return the product that was added to the shopping list + */ + public Optional<ShoppingList> addProductToShoppingList(long ean, long shoppingListId){ + shoppingListRepository.findById(shoppingListId).ifPresent(shoppingList -> { + productRepository.findById(ean).ifPresent(product -> { + shoppingList.addProduct(product); + product.addShoppingList(shoppingList); + productRepository.save(product); + shoppingListRepository.save(shoppingList); + }); + }); + + return shoppingListRepository.findById(shoppingListId); } + /** + * Removes a product from a shopping list + * @param ean the ean of the product to remove + * @param shoppingListId the id of the shopping list to remove the product from + * @return the shopping list that the product was removed from + */ + public Optional<ShoppingList> removeProductFromShoppingList(long ean, long shoppingListId){ + shoppingListRepository.findById(shoppingListId).ifPresent(shoppingList -> { + productRepository.findById(ean).ifPresent(product -> { + shoppingList.getProducts().remove(product); + product.getShoppingLists().remove(shoppingList); + productRepository.save(product); + shoppingListRepository.save(shoppingList); + }); + }); + + return shoppingListRepository.findById(shoppingListId); + } } -- GitLab