From 905220496b1586140bbe09c2a9f63690fae0980d Mon Sep 17 00:00:00 2001
From: birkon <birkon@stud.ntnu.no>
Date: Wed, 3 May 2023 09:46:43 +0200
Subject: [PATCH] fixed remove from shopping list endpoint

---
 .../SmartMat/controller/ShoppingListController.java |  4 +++-
 .../v233/SmartMat/service/ShoppingListService.java  | 13 +++++++------
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/ShoppingListController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/ShoppingListController.java
index 103d16a0..352447b2 100644
--- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/ShoppingListController.java
+++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/ShoppingListController.java
@@ -134,8 +134,10 @@ public class ShoppingListController {
         if(product.isEmpty())
             return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
 
+        System.out.println("Removing product from shopping list : " + shoppingListId + " - " + ean);
+
         Optional<ShoppingList> returnVal = shoppingListService
-                .removeProductFromShoppingList(Long.parseLong(shoppingListId), Long.parseLong(ean));
+                .removeProductFromShoppingList(Long.parseLong(ean), Long.parseLong(shoppingListId));
 
         return returnVal.map(list -> ResponseEntity.status(HttpStatus.OK).body(list))
                 .orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).build());
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 9472a997..c7867e9c 100644
--- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java
+++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java
@@ -93,13 +93,14 @@ public class ShoppingListService {
      * @return the shopping list that the product was removed from
      */
     public Optional<ShoppingList> removeProductFromShoppingList(long ean, long shoppingListId){
+        System.out.println("shopping list status : " + shoppingListRepository.findById(shoppingListId).isPresent());
+
             shoppingListRepository.findById(shoppingListId).ifPresent(shoppingList -> {
-            productRepository.findById(ean).ifPresent(product -> {
-                shoppingList.getProducts().remove(product);
-                product.getShoppingLists().remove(shoppingList);
-                productRepository.save(product);
-                shoppingListRepository.save(shoppingList);
-            });
+                productRepository.findById(ean).ifPresent(product -> {
+                    shoppingList.getProducts().remove(product);
+                    product.getShoppingLists().remove(shoppingList);
+                    shoppingListRepository.save(shoppingList);
+                });
             });
 
         return shoppingListRepository.findById(shoppingListId);
-- 
GitLab