From 02375260e51380499f3c7e2e5abbabf606fdd0c8 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sun, 16 Feb 2025 14:14:06 -0500 Subject: Implemented getNeeds method --- .../ufundapi/controller/CupboardController.java | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index 106b2e0..255eba0 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -11,13 +11,11 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.io.IOException; -import java.util.ArrayList; @RestController @RequestMapping("cupboard") public class CupboardController { private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); - private ArrayList needs; private Cupboard cupboard; @PostMapping("") @@ -25,9 +23,25 @@ public class CupboardController { cupboard.createNeed(need); } + /** + * Responds to the GET request for all {@linkplain Need needs} + * + * @return ResponseEntity with array of {@link Need needs} objects (may be empty) + * and + * HTTP status of OK
+ * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise + */ @GetMapping("") - public Need[] getNeeds() { - return cupboard.getNeeds(); + public ResponseEntity getNeeds() { + LOG.info("GET /needs"); + + try { + Need[] needs = cupboard.getNeeds(); + return new ResponseEntity<>(needs, HttpStatus.OK); + } catch (IOException e) { + LOG.log(Level.SEVERE, e.getLocalizedMessage()); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } @GetMapping("/") -- cgit v1.2.3