From 70762c9486d19c48ee4c866b8fbb6646e9c97012 Mon Sep 17 00:00:00 2001 From: Stian Lyng <stianlyng@protonmail.com> Date: Wed, 26 Apr 2023 11:59:18 +0200 Subject: [PATCH] get fridge by id --- .../SmartMat/controller/group/FridgeController.java | 13 +++++++++++++ .../v233/SmartMat/service/group/FridgeService.java | 11 +++++++++++ 2 files changed, 24 insertions(+) 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 9053405f..1a02f49d 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 @@ -37,6 +37,19 @@ public class FridgeController { .orElseGet(() -> ResponseEntity.notFound().build()); } + /** + * Gets the fridge by its fridge id + * @param fridgeId the id of the fridge + * @return the fridge if it exists, or a 404 if it doesn't + */ + @GetMapping("/fridge/{fridgeId}") + public ResponseEntity<Fridge> getFridgeByFridgeId(@PathVariable("fridgeId") long fridgeId) { + return fridgeService.getFridgeByFridgeId(fridgeId) + .map(ResponseEntity::ok) + .orElseGet(() -> ResponseEntity.notFound().build()); + } + + /** * Adds a product to the fridge of a group * 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 8650f112..d6a0d519 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 @@ -37,6 +37,17 @@ public class FridgeService { public Optional<Fridge> getFridgeByGroupId(long groupId) { return fridgeRepository.findByGroupId(groupId); } + + /** + * Gets the fridge by its fridge id + * + * @param fridgeId the id of the fridge + * @return the fridge if it exists + */ + public Optional<Fridge> getFridgeByFridgeId(long fridgeId) { + return fridgeRepository.findById(fridgeId); + } + /** * Add a product to the fridge of a group -- GitLab