diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java index 15f1619d84f83f3a0e3ebe4e95089947f6183fd9..783d85b7385d0d335d5795e0fc0bee8c2c0a6cd8 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java @@ -59,21 +59,16 @@ public class FridgeController { */ @PostMapping("/group/product") public ResponseEntity<Object> addProductToFridge(@RequestBody FridgeProductRequest request) { - System.out.println(request); return fridgeService.addProductToFridge(request).map(ResponseEntity::ok).orElseGet(()-> ResponseEntity.notFound().build()); } - /** - * Removes a product from the fridge of a group - * todo: remove the date parameter when the frontend is done - * - * @param groupId the id of the group - * group must exist - * group must have a fridge - * @param productId the id of the product - * @return success if the product was removed, bad request if the product wasn't in the fridge, or not found if the group or product doesn't exist - */ + @DeleteMapping("/group/product/delete/{fridgeProductId}/{amount}") + public ResponseEntity<?> deleteAmountFridgeProduct(@PathVariable("fridgeProductId") long fridgeProductId, + @PathVariable("amount") int amount){ + return fridgeService.deleteAmountFromFridge(fridgeProductId,amount).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); + } + /* @DeleteMapping("/delete/{fridgeProductId}") diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeProductAssoRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeProductAssoRepository.java index 5255fccd80e32b249f7739dffa9f97624f4e5088..b16d73a187f94437d0c0cb0f38c3e2592276fbef 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeProductAssoRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeProductAssoRepository.java @@ -2,6 +2,9 @@ package ntnu.idatt2016.v233.SmartMat.repository.group; import ntnu.idatt2016.v233.SmartMat.entity.fridgeProduct.FridgeProductAsso; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; import java.util.Optional; @@ -11,6 +14,10 @@ import java.util.Optional; * @version 1.0 * @since 25.04.2023 */ + +@Repository public interface FridgeProductAssoRepository extends JpaRepository<FridgeProductAsso, Long> { Optional<FridgeProductAsso> findById(long id); + int findAmountById(long id); + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java index b888a8f66c46db34f14b29c3711a096e721bc18c..4fdd96211515055cb11437a91b370440ef11be2c 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java @@ -120,4 +120,13 @@ public class FridgeService { fridgeRepository.save(fridge.get()); } } + + public Optional<Object> deleteAmountFromFridge(long fridgeProductId, int amount) { + if(amount < fridgeProductAssoRepository.findAmountById(fridgeProductId)){ + System.out.println("Given amount " + amount + " < " + " Stored in db"); + } else { + System.out.println("Given amount " + amount + " > " + " Stored in db"); + } + return Optional.empty(); + } }