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 74d21777fd9bb9c54c4456b8cfa15b4c1bc9be88..45fe9b8c82e33d53a3ed7b0370e65f62788766f4 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 49aaad66bb2b5b6e56b2a20f5fe8e4086f26c0ab..1772931f36915d996b1f2c6cc163243a444366b1 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 033a13c7aba5fdf122d8a57857fc6b6a55e0ceef..a5d65b7c390400436edded7e78dea00e2166f63e 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); + } }