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..ed34b140e88d6b30c565c031cd0b37d20bef60c8 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java @@ -0,0 +1,46 @@ +package ntnu.idatt2016.v233.SmartMat.controller.group; + +import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.entity.Fridge; +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 groupId the id of the group + * group must exist + * @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 + */ + @PostMapping("") + +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/FridgeRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/FridgeRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..f66d5b73a6b36312872d4fc6b708e08fc84a025e --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/FridgeRepository.java @@ -0,0 +1,16 @@ +package ntnu.idatt2016.v233.SmartMat.entity.group; + +import ntnu.idatt2016.v233.SmartMat.entity.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..495c183617178ad4e804fed39d6702fc8305baa4 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/FridgeService.java @@ -0,0 +1,75 @@ +package ntnu.idatt2016.v233.SmartMat.service.group; + +import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.entity.Fridge; +import ntnu.idatt2016.v233.SmartMat.entity.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 + * @since 24.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 addProduct(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()) { + fridge.getProducts().add(product.get()); + 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 removeProduct(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()) { + fridge.getProducts().remove(product.get()); + fridgeRepository.save(fridge); + return true; + } else { + return false; + } + } +}