diff options
author | benal01 <bja4245@rit.edu> | 2025-02-16 14:58:13 -0500 |
---|---|---|
committer | benal01 <bja4245@rit.edu> | 2025-02-16 14:58:13 -0500 |
commit | 2c67a912f415a698754192e49028640f3cdedd28 (patch) | |
tree | 72f6f60e162ece692d79db525bdcbaff4041a7a7 /ufund-api/src | |
parent | 5da896b89f099a338fe880bbcd488c3fcad8ced3 (diff) | |
parent | 82479c054fbbd7fca52e2cabd7b646a4455d7e66 (diff) | |
download | JellySolutions-2c67a912f415a698754192e49028640f3cdedd28.tar.gz JellySolutions-2c67a912f415a698754192e49028640f3cdedd28.tar.bz2 JellySolutions-2c67a912f415a698754192e49028640f3cdedd28.zip |
u-fund XML file
Diffstat (limited to 'ufund-api/src')
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java | 117 | ||||
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java | 4 |
2 files changed, 105 insertions, 16 deletions
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..dbd4394 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,43 +11,132 @@ 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<Need> needs; private Cupboard cupboard; + /** + * Creates a Need with the provided object + * + * @param need The need to create + * @return OK response and the need if it was successful, INTERNAL_SERVER_ERROR otherwise + */ @PostMapping("") - public void createNeed(@RequestBody Need need) { - cupboard.createNeed(need); + public ResponseEntity<Need> createNeed(@RequestBody Need need) { + try { + cupboard.createNeed(need); + return new ResponseEntity<>(need, HttpStatus.OK); + } catch (IOException ex) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } + /** + * 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<br> + * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise + */ @GetMapping("") - public Need[] getNeeds() { - return cupboard.getNeeds(); + public ResponseEntity<Need[]> 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); + } } + /** + * Responds to the GET request for all {@linkplain Need need} whose name contains + * the text in name + * + * @param name The name parameter which contains the text used to find the {@link Need need} + * + * @return ResponseEntity with array of {@link Need need} objects (may be empty) and + * HTTP status of OK<br> + * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise + * <p> + */ @GetMapping("/") - public Need searchNeeds(@RequestParam String name) { - return cupboard.findNeeds(name); + public ResponseEntity<Need[]> searchNeeds(@RequestParam String name) { + LOG.info("GET /need/?name="+name); + + try { + Need[] needArray = cupboard.findNeeds(name); + return new ResponseEntity<Need[]>(needArray,HttpStatus.OK); + } catch (IOException e) { + LOG.log(Level.SEVERE,e.getLocalizedMessage()); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } + /** + * Responds to the GET request for a {@linkplain Need need} for the given id + * + * @param id The id used to locate the {@link Need need} + * + * @return ResponseEntity with {@link Need need} object and HTTP status of OK if + * found<br> + * ResponseEntity with HTTP status of NOT_FOUND if not found<br> + * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise + */ @GetMapping("/{id}") - public Need getNeed(@PathVariable int id) { - return cupboard.getNeed(id); + public ResponseEntity<Need> getNeed(@PathVariable int id) { + LOG.log(Level.INFO, "GET /need/{0}", id); + + try { + Need need = cupboard.getNeed(id); + if (need != null) { + return new ResponseEntity<>(need, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + } catch (IOException e) { + LOG.log(Level.SEVERE, e.getLocalizedMessage()); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + /** + * Updates a Need with the provided one + * + * @param need The need to update + * @return OK response and the need if it was successful, or INTERNAL_SERVER_ERROR if there was an issue + */ + @PutMapping("") - public void updateNeed(@RequestBody Need need) { - cupboard.updateNeed(need); + public ResponseEntity<Need> updateNeed(@RequestBody Need need) { + try { + need = cupboard.updateNeed(need); + return new ResponseEntity<>(need, HttpStatus.OK); + } catch (IOException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } @DeleteMapping("/{id}") - public void deleteNeed(@PathVariable int id) { - cupboard.removeNeed(id); + public ResponseEntity<Need> deleteNeed(@PathVariable int id) { + try { + if (cupboard.getNeed(id) != null) { + cupboard.removeNeed(id); + return new ResponseEntity<>(HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } catch (IOException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java index a626561..0ce015c 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java @@ -23,8 +23,8 @@ public class Cupboard { return dao.findNeeds(name); } - public void updateNeed(Need need) throws IOException { - dao.updateNeed(need); + public Need updateNeed(Need need) throws IOException { + return dao.updateNeed(need); } public void removeNeed(int id) throws IOException { |