diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-03-25 00:03:45 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-03-25 00:03:45 -0400 |
commit | c15aa3daab0cf9a640945d4e634d1327fb55d2db (patch) | |
tree | 33b4331ce63890104dff5aa97152ac3f87386492 /ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java | |
parent | a8175ba69669fddadfbe143e11972cc21821ed5f (diff) | |
download | JellySolutions-c15aa3daab0cf9a640945d4e634d1327fb55d2db.tar.gz JellySolutions-c15aa3daab0cf9a640945d4e634d1327fb55d2db.tar.bz2 JellySolutions-c15aa3daab0cf9a640945d4e634d1327fb55d2db.zip |
Greatly improve logging and other backend clean up
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java')
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java | 57 |
1 files changed, 33 insertions, 24 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 664b53b..8db8901 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 @@ -23,6 +23,8 @@ import com.ufund.api.ufundapi.model.Need; import com.ufund.api.ufundapi.model.Need.GoalType; import com.ufund.api.ufundapi.service.CupboardService; +import static java.util.List.of; + @RestController @RequestMapping("cupboard") public class CupboardController { @@ -49,7 +51,8 @@ public class CupboardController { */ @PostMapping("") public ResponseEntity<Need> createNeed(@RequestBody Map<String, Object> params) { - System.out.println(params); + LOG.log(Level.INFO, "POST /cupboard body: {0}", params); + String name = (String) params.get("name"); double maxGoal = (double) params.get("maxGoal"); Need.GoalType goalType = GoalType.valueOf((String) params.get("type")); @@ -58,10 +61,13 @@ public class CupboardController { Need need = cupboardService.createNeed(name, maxGoal, goalType); return new ResponseEntity<>(need, HttpStatus.OK); } catch (DuplicateKeyException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.CONFLICT); } catch (IllegalArgumentException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -76,7 +82,7 @@ public class CupboardController { */ @GetMapping("") public ResponseEntity<Need[]> getNeeds() { - LOG.info("GET /needs"); + LOG.info("GET /cupboard"); try { Need[] needs = cupboardService.getNeeds(); @@ -88,19 +94,21 @@ public class CupboardController { } /** - * 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> - */ + * 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} + * + * @deprecated Searching should now be done client side in the future + * + * @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 ResponseEntity<Need[]> searchNeeds(@RequestParam String name) { - LOG.info("GET /need/?name="+name); + LOG.info("GET /cupboard/?name="+name); try { Need[] needs = cupboardService.searchNeeds(name); @@ -121,7 +129,7 @@ public class CupboardController { */ @GetMapping("/{id}") public ResponseEntity<Need> getNeed(@PathVariable int id) { - LOG.log(Level.INFO, "GET /need/{0}", id); + LOG.log(Level.INFO, "GET /cupboard/{0}", id); try { Need need = cupboardService.getNeed(id); @@ -145,7 +153,7 @@ public class CupboardController { */ @PutMapping("/{id}") public ResponseEntity<Need> updateNeed(@RequestBody Need need, @PathVariable int id) { - LOG.log(Level.INFO, "Updating need: " + need); + LOG.log(Level.INFO, "PUT /cupboard/{0} body: {1}", of(id, need)); try { Need updatedNeed = cupboardService.updateNeed(need, id); if (updatedNeed != null) { @@ -154,10 +162,10 @@ public class CupboardController { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (IllegalArgumentException ex) { - ex.printStackTrace(); + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IOException ex) { - ex.printStackTrace(); + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -168,24 +176,23 @@ public class CupboardController { * @param data JSON object with paramters needID and amount * @param key Key used to authenticate user * @return OK if successful, other statuses if failure - * @throws IllegalAccessException */ @PutMapping("/checkout") - public ResponseEntity<Object> checkoutNeeds(@RequestBody Map<String, Integer> data, @RequestHeader("jelly-api-key") String key) throws IllegalAccessException { + public ResponseEntity<Object> checkoutNeeds(@RequestBody Map<String, Integer> data, @RequestHeader("jelly-api-key") String key) { int needID = data.get("needID"); int checkoutAmount = data.get("amount"); - LOG.log(Level.INFO, "Checking out need with ID: " + needID + " by " + checkoutAmount); + LOG.log(Level.INFO, "PUT /need/checkout body: {0}", data); try { cupboardService.checkoutNeed(needID, checkoutAmount, key); return new ResponseEntity<>(HttpStatus.OK); } catch (IllegalArgumentException ex) { - ex.printStackTrace(); + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IllegalAccessException ex) { - ex.printStackTrace(); + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } catch (IOException ex) { - ex.printStackTrace(); + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -198,6 +205,7 @@ public class CupboardController { */ @DeleteMapping("/{id}") public ResponseEntity<Need> deleteNeed(@PathVariable int id) { + LOG.log(Level.INFO, "DELETE /cupboard/{0}", id); try { Need need = cupboardService.getNeed(id); if (cupboardService.deleteNeed(id)) { @@ -205,7 +213,8 @@ public class CupboardController { } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - } catch (IOException e) { + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } |