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 new file mode 100644 index 0000000000000000000000000000000000000000..6e6ce1e9aaa065dc0e066b664933686eb301bb9d --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java @@ -0,0 +1,92 @@ +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; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +/** + * Controller for fridges API, providing endpoints for fridge management + * + * @author Anders Austlid + * @version 1.0 + * @since 24.04.2023 + */ +@AllArgsConstructor +@RestController +@RequestMapping("/api/fridges") +public class FridgeController { + + private final FridgeService fridgeService; + + /** + * Gets the fridge of a group + * @param groupId the id of the group + * group must exist + * @return the fridge of the group if it exists, or a 404 if it doesn't + */ + @GetMapping("/group/{groupId}") + public ResponseEntity<Fridge> getFridgeByGroupId(@PathVariable("groupId") long groupId) { + return fridgeService.getFridgeByGroupId(groupId) + .map(ResponseEntity::ok) + .orElseGet(() -> ResponseEntity.notFound().build()); + } + + /** + * 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(); + } + + try { + if (fridgeService.addProductToFridge(groupId, productId)) { + return ResponseEntity.ok("Success"); + } + return ResponseEntity.badRequest().body("Product already exists in the fridge"); + } catch (Exception e) { + return ResponseEntity.status(500).body("Internal server error"); + } + } + + /** + * 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 removed, bad request if the product wasn't in the fridge, or not found if the group or product doesn't exist + */ + @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(); + } + + try { + if (fridgeService.removeProductFromFridge(groupId, productId)) { + return ResponseEntity.ok("Success"); + } + return ResponseEntity.badRequest().body("Product not found in the fridge"); + } catch (Exception e) { + return ResponseEntity.status(500).body("Internal server error"); + } + } + +} 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 new file mode 100644 index 0000000000000000000000000000000000000000..57bee46c5621b016e24b0934dc41633e5a4f843a --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/FridgeProductRequest.java @@ -0,0 +1,9 @@ +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/Fridge.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java similarity index 85% rename from src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Fridge.java rename to src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java index a8a0e0145c3cb273ae61b293da8d24b002abfef6..056767ad0f14dbdc5d49ddfd2ff68efe95804a80 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Fridge.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Fridge.java @@ -1,4 +1,4 @@ -package ntnu.idatt2016.v233.SmartMat.entity; +package ntnu.idatt2016.v233.SmartMat.entity.group; import java.util.List; @@ -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/entity/product/Product.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java index 67f6757f466112a0f2d7ea5d9e1eb074834bb976..3030ef93b53a973e98def00b4e458fef9896eef8 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 @@ -6,7 +6,7 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import ntnu.idatt2016.v233.SmartMat.entity.Fridge; +import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; import ntnu.idatt2016.v233.SmartMat.entity.Recipe; import java.util.List; diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/FridgeRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/FridgeRepository.java deleted file mode 100644 index c8013568ebfa29f2ecdfc700477c90a5f1e1fb29..0000000000000000000000000000000000000000 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/FridgeRepository.java +++ /dev/null @@ -1,24 +0,0 @@ -package ntnu.idatt2016.v233.SmartMat.repository; - -import ntnu.idatt2016.v233.SmartMat.entity.Fridge; - -import java.util.List; - -import org.springframework.data.jpa.repository.JpaRepository; - -/** - * Repository for Products - * @author Stian - * @version 1.0 - */ -public interface FridgeRepository extends JpaRepository<Fridge, Long> { - - /** - * Gets all fridge items by their group id - * - * @param id the id of the group - * @return a list of fridge items - */ - List<Fridge> findAllByGroupId(long id); - -} \ No newline at end of file diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..ae9b147f80c33aced58baeb0c644be96e73bcb96 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/FridgeRepository.java @@ -0,0 +1,16 @@ +package ntnu.idatt2016.v233.SmartMat.repository.group; + +import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface FridgeRepository extends JpaRepository<Fridge, Long> { + /** + * Gets the fridge of a group + * + * @param groupId the id of the group + * @return an Optional containing the fridge of the group if it exists + */ + Optional<Fridge> findByGroupId(long groupId); +} 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 new file mode 100644 index 0000000000000000000000000000000000000000..c36c80847d01bace953ebdacab1494ae1a1db874 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java @@ -0,0 +1,85 @@ +package ntnu.idatt2016.v233.SmartMat.service.group; + +import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; +import ntnu.idatt2016.v233.SmartMat.repository.group.FridgeRepository; +import ntnu.idatt2016.v233.SmartMat.entity.product.Product; +import ntnu.idatt2016.v233.SmartMat.service.product.ProductService; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +/** + * Service for management of a group fridge + * + * @author Anders Austlid + * @version 1.0.1 + * @since 25.04.2023 + */ +@AllArgsConstructor +@Service +public class FridgeService { + + private final FridgeRepository fridgeRepository; + private final ProductService productService; + + /** + * Gets the fridge of a group + * + * @param groupId the id of the group + * @return the fridge of the group + */ + public Optional<Fridge> getFridgeByGroupId(long groupId) { + return fridgeRepository.findByGroupId(groupId); + } + + /** + * Add a product to the fridge of a group + * + * @param groupId the id of the group + * group must exist + * @param ean the ean of the product + * @return true if the product was added + */ + public boolean addProductToFridge(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()) { + Product productToAdd = product.get(); + if (fridge.getProducts().contains(productToAdd)) { + return false; + } + fridge.getProducts().add(productToAdd); + fridgeRepository.save(fridge); + return true; + } else { + return false; + } + } + + /** + * Remove a product from the fridge of a group + * + * @param groupId the id of the group + * group must exist + * @param ean the ean of the product + * @return true if the product was removed + */ + 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()) { + Product productToRemove = product.get(); + if (!fridge.getProducts().contains(productToRemove)) { + return false; + } + fridge.getProducts().remove(productToRemove); + fridgeRepository.save(fridge); + return true; + } else { + return false; + } + } +} 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 ff2dd81dd1e764ee594f1a0541ef5946aca80afe..ba29faf21cce6d02752edd7c9057b6c466585e5c 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 @@ -12,8 +12,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;