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 bb6abb5c68ada27a2b968dc6a1f17b4828ef6acc..fb012aae049f3db7aadc4948e3fddf0ae7797e8e 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 @@ -1,6 +1,7 @@ package ntnu.idatt2016.v233.SmartMat.controller.group; import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.dto.request.FridgeProductRequest; import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.service.group.FridgeService; @@ -37,14 +38,42 @@ public class FridgeController { /** * Adds a product to the fridge of a group * + * @param request the request containing the group id and product id + * @return success if the product was added, bad request if the product was already in the fridge, or not found if the group or product doesn't exist + */ + @PostMapping("/group/product") + public ResponseEntity<String> addProductToFridge(@RequestBody FridgeProductRequest request) { + long groupId = request.groupId(); + long productId = request.productId(); + + try { + fridgeService.getFridgeByGroupId(groupId).orElseThrow(); + } catch (Exception e) { + return ResponseEntity.notFound().build(); + } + if (fridgeService.addProductToFridge(groupId, productId)) { + return ResponseEntity.ok("Success"); + } + return ResponseEntity.badRequest().build(); + } + + /** + * Removes a product from the fridge of a group + * * @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 added, bad request if the product was already in the fridge, or not found if the group or product doesn't exist + * @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 */ - @PostMapping("/group/{groupId}/product/{productId}") - public ResponseEntity<String> addProductToFridge(@PathVariable("groupId") long groupId, @PathVariable("productId") long productId) { - if(fridgeService.addProductToFridge(groupId, productId)) { + @DeleteMapping("/group/{groupId}/product/{productId}") + public ResponseEntity<String> removeProductFromFridge(@PathVariable("groupId") long groupId, @PathVariable("productId") long productId) { + try { + fridgeService.getFridgeByGroupId(groupId).orElseThrow(); + } catch (Exception e) { + return ResponseEntity.notFound().build(); + } + if (fridgeService.removeProductFromFridge(groupId, productId)) { return ResponseEntity.ok("Success"); } return ResponseEntity.badRequest().build(); diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/FridgeProductRequest.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/FridgeProductRequest.java index 6f3415ea2c8cbc5cd9ea26d5ea618ad82709555f..57bee46c5621b016e24b0934dc41633e5a4f843a 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/FridgeProductRequest.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/FridgeProductRequest.java @@ -1,2 +1,9 @@ -package ntnu.idatt2016.v233.SmartMat.dto.request;public record FridgeProductRequest() { +package ntnu.idatt2016.v233.SmartMat.dto.request; + +/** + * FridgeProductRequest is a record class representing a request to add a product to a fridge. + * @param groupId the id of the group + * @param productId the id of the product + */ +public record FridgeProductRequest(long groupId, long productId) { } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java index a15c7cf5499635a662af71f070ed4984fcf86017..056767ad0f14dbdc5d49ddfd2ff68efe95804a80 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java @@ -15,9 +15,8 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product; * Fridge is an entity class representing a fridge in the system. * * @author Anders - * @version 1.1.001 - * @since 19.04.2023 - * + * @version 1.1.002 + * @since 15.04.2023 */ @NoArgsConstructor @@ -38,6 +37,6 @@ public class Fridge{ @JoinTable(name = "fridge_product", joinColumns = @JoinColumn(name = "fridge_id"), inverseJoinColumns = @JoinColumn(name = "ean")) - @JsonIgnoreProperties("fridges") + @JsonIgnoreProperties({"allergies", "fridges"}) List<Product> products; } 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 f3cac665aa9f2cae38998f41ac3b72ce51b10866..3ac7943626711aefedd436633b10d8484c153bad 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 @@ -13,8 +13,8 @@ import java.util.Optional; * Service for management of a group fridge * * @author Anders Austlid - * @version 1.0 - * @since 24.04.2023 + * @version 1.0.1 + * @since 25.04.2023 */ @AllArgsConstructor @Service @@ -61,7 +61,7 @@ public class FridgeService { * @param ean the ean of the product * @return true if the product was removed */ - public boolean removeProduct(long groupId, long ean) { + public boolean removeProductFromFridge(long groupId, long ean) { Optional<Product> product = productService.getProductById(ean); Fridge fridge = fridgeRepository.findByGroupId(groupId).orElseThrow(() -> new IllegalArgumentException("Fridge does not exist")); if (product.isPresent()) { diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java index 0c7392532d8e47a54c89238fc5321128957a6b9a..c1965a3b372a1bfec89e473861f087ae0dc5f4f2 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java @@ -11,8 +11,8 @@ import java.util.Optional; * Service for groups * * @author Anders Austlid - * @version 1.0 - * @since 20.04.2023 + * @version 1.1 + * @since 25.04.2023 */ @AllArgsConstructor @Service diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/product/ProductService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/product/ProductService.java index 2155ade614420bca5186af4aa764007ecf8b4c3f..13d85862d479f6691e17009beb661867173d444d 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/product/ProductService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/product/ProductService.java @@ -3,6 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.service.product; import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.repository.ProductRepository; import ntnu.idatt2016.v233.SmartMat.util.ProductUtil; +import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @@ -14,6 +15,7 @@ import java.util.Optional; * @version 1.1 * @since 05.04.2023 */ +@Service public class ProductService { ProductRepository productRepository;