From 380b0ca3f3ebd1564d2e961a67e0fc8a8dac80f2 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Wed, 26 Feb 2025 13:22:33 -0500 Subject: Created UserController class to handle http requests --- .../api/ufundapi/controller/UserController.java | 170 +++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java new file mode 100644 index 0000000..223963d --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java @@ -0,0 +1,170 @@ +package com.ufund.api.ufundapi.controller; + +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.ufund.api.ufundapi.model.Need; +import com.ufund.api.ufundapi.persistence.UserDAO; + +@RestController +@RequestMapping("cupboard") +public class UserController { + private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); + private final UserDAO UserDAO; + + /** + * Create a cupboard controller to receive REST signals + * + * @param UserDAO The Data Access Object + */ + public UserController(UserDAO userDAO) { + this.UserDAO = userDAO; + } + + /** + * 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 ResponseEntity createUser(@RequestBody Need need) { + try { + if (need.getMaxGoal() <= 0) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + if (need.getMaxGoal() < need.getCurrent()) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + UserDAO.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
+ * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise + */ + @GetMapping("") + public ResponseEntity getNeeds() { + LOG.info("GET /needs"); + + try { + Need[] needs = UserDAO.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
+ * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise + *

+ */ + @GetMapping("/") + public ResponseEntity searchNeeds(@RequestParam String name) { + LOG.info("GET /need/?name="+name); + + try { + Need[] needArray = UserDAO.findNeeds(name); + return new ResponseEntity<>(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
+ * ResponseEntity with HTTP status of NOT_FOUND if not found
+ * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise + */ + @GetMapping("/{id}") + public ResponseEntity getNeed(@PathVariable int id) { + LOG.log(Level.INFO, "GET /need/{0}", id); + + try { + Need need = UserDAO.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 ResponseEntity updateNeed(@RequestBody Need need) { + try { + need = UserDAO.updateNeed(need); + return new ResponseEntity<>(need, HttpStatus.OK); + } catch (IOException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + /** + * Deletes a single need from the cupboard using the Need's id + * + * @param id The need's ID + * @return OK if the need was deleted, NOT_FOUND if the need was not found, or INTERNAL_SERVER_ERROR if an error occurred + */ + @DeleteMapping("/{id}") + public ResponseEntity deleteNeed(@PathVariable int id) { + try { + if (UserDAO.getNeed(id) != null) { + UserDAO.deleteNeed(id); + return new ResponseEntity<>(HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } catch (IOException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + +} -- cgit v1.2.3 From 0d1c11aa2738a622fc2ee6ecb23aef214c9520db Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Wed, 26 Feb 2025 13:33:03 -0500 Subject: Implemented createUser in the controller and modified logic in the UserFileDAO to check for conflict --- .../ufund/api/ufundapi/controller/UserController.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java index 223963d..bf3f7a1 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java @@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import com.ufund.api.ufundapi.model.Need; +import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.persistence.UserDAO; @RestController @@ -41,16 +41,14 @@ public class UserController { * @return OK response and the need if it was successful, INTERNAL_SERVER_ERROR otherwise */ @PostMapping("") - public ResponseEntity createUser(@RequestBody Need need) { + public ResponseEntity createUser(@RequestBody User user) { try { - if (need.getMaxGoal() <= 0) { - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } - if (need.getMaxGoal() < need.getCurrent()) { - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + if (UserDAO.createUser(user) != null) { + return new ResponseEntity<>(user, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.CONFLICT); } - UserDAO.createNeed(need); - return new ResponseEntity<>(need, HttpStatus.OK); + } catch (IOException ex) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } -- cgit v1.2.3 From 03799f252ad2fb207e256fe7eff7ad0600131b7d Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Wed, 26 Feb 2025 15:30:04 -0500 Subject: Updated javadocs and implemented additional CRUD operations --- .../api/ufundapi/controller/UserController.java | 94 +++++++++------------- 1 file changed, 36 insertions(+), 58 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java index bf3f7a1..ae75179 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java @@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.ufund.api.ufundapi.model.User; @@ -35,10 +34,10 @@ public class UserController { } /** - * Creates a Need with the provided object + * Creates a User with the provided object * - * @param need The need to create - * @return OK response and the need if it was successful, INTERNAL_SERVER_ERROR otherwise + * @param user The user to create + * @return OK response and the user if it was successful, INTERNAL_SERVER_ERROR otherwise */ @PostMapping("") public ResponseEntity createUser(@RequestBody User user) { @@ -55,68 +54,44 @@ public class UserController { } /** - * Responds to the GET request for all {@linkplain Need needs} + * Responds to the GET request for all {@linkplain User user} * - * @return ResponseEntity with array of {@link Need needs} objects (may be empty) + * @return ResponseEntity with array of {@link User user} objects (may be empty) * and * HTTP status of OK
* ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise */ @GetMapping("") - public ResponseEntity getNeeds() { - LOG.info("GET /needs"); + public ResponseEntity getUseers() { + LOG.info("GET /users"); try { - Need[] needs = UserDAO.getNeeds(); - return new ResponseEntity<>(needs, HttpStatus.OK); + User[] users = UserDAO.getUsers(); + return new ResponseEntity<>(users, 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
- * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise - *

- */ - @GetMapping("/") - public ResponseEntity searchNeeds(@RequestParam String name) { - LOG.info("GET /need/?name="+name); - - try { - Need[] needArray = UserDAO.findNeeds(name); - return new ResponseEntity<>(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 + * Responds to the GET request for a {@linkplain User user} for the given id * - * @param id The id used to locate the {@link Need need} + * @param id The id used to locate the {@link User user} * - * @return ResponseEntity with {@link Need need} object and HTTP status of OK if + * @return ResponseEntity with {@link User user} object and HTTP status of OK if * found
* ResponseEntity with HTTP status of NOT_FOUND if not found
* ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise */ - @GetMapping("/{id}") - public ResponseEntity getNeed(@PathVariable int id) { - LOG.log(Level.INFO, "GET /need/{0}", id); + @GetMapping("/{name}") + public ResponseEntity getUser(@PathVariable String name) { + LOG.log(Level.INFO, "GET /user/{0}", name); try { - Need need = UserDAO.getNeed(id); - if (need != null) { - return new ResponseEntity<>(need, HttpStatus.OK); + User user = UserDAO.getUser(name); + if (user != null) { + return new ResponseEntity<>(user, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } @@ -129,33 +104,36 @@ public class UserController { } /** - * 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 + * Updates a User with the provided one + * + * @param user The user to update + * @return OK response and the user if it was successful, or INTERNAL_SERVER_ERROR if there was an issue */ - @PutMapping("") - public ResponseEntity updateNeed(@RequestBody Need need) { + public ResponseEntity updateUser(@RequestBody User user, String string) { try { - need = UserDAO.updateNeed(need); - return new ResponseEntity<>(need, HttpStatus.OK); + user = UserDAO.updateUser(user, string); + if (user != null) { + return new ResponseEntity<>(user, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } /** - * Deletes a single need from the cupboard using the Need's id + * Deletes a user with the desired name * - * @param id The need's ID - * @return OK if the need was deleted, NOT_FOUND if the need was not found, or INTERNAL_SERVER_ERROR if an error occurred + * @param name The name of the user + * @return OK if the user was deleted, NOT_FOUND if the user was not found, or INTERNAL_SERVER_ERROR if an error occurred */ - @DeleteMapping("/{id}") - public ResponseEntity deleteNeed(@PathVariable int id) { + @DeleteMapping("/{name}") + public ResponseEntity deleteUser(@PathVariable String name) { try { - if (UserDAO.getNeed(id) != null) { - UserDAO.deleteNeed(id); + if (UserDAO.deleteUser(name)) { return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); -- cgit v1.2.3 From 72d614074a3cd578322931af647c6f0a65a23dfd Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Wed, 26 Feb 2025 16:00:35 -0500 Subject: Tested user http calls and fixed update user --- .../api/ufundapi/controller/UserController.java | 27 ++++++++++++---------- 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java index ae75179..1af865d 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java @@ -19,13 +19,13 @@ import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.persistence.UserDAO; @RestController -@RequestMapping("cupboard") +@RequestMapping("users") public class UserController { private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); private final UserDAO UserDAO; /** - * Create a cupboard controller to receive REST signals + * Create a user controller to receive REST signals * * @param UserDAO The Data Access Object */ @@ -37,7 +37,8 @@ public class UserController { * Creates a User with the provided object * * @param user The user to create - * @return OK response and the user if it was successful, INTERNAL_SERVER_ERROR otherwise + * @return OK response and the user if it was successful, INTERNAL_SERVER_ERROR + * otherwise */ @PostMapping("") public ResponseEntity createUser(@RequestBody User user) { @@ -47,7 +48,7 @@ public class UserController { } else { return new ResponseEntity<>(HttpStatus.CONFLICT); } - + } catch (IOException ex) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } @@ -107,18 +108,19 @@ public class UserController { * Updates a User with the provided one * * @param user The user to update - * @return OK response and the user if it was successful, or INTERNAL_SERVER_ERROR if there was an issue + * @return OK response and the user if it was successful, or + * INTERNAL_SERVER_ERROR if there was an issue */ - @PutMapping("") - public ResponseEntity updateUser(@RequestBody User user, String string) { + @PutMapping("/{name}") + public ResponseEntity updateUser(@RequestBody User user, @PathVariable String name) { try { - user = UserDAO.updateUser(user, string); + user = UserDAO.updateUser(user, name); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - + } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } @@ -128,8 +130,9 @@ public class UserController { * Deletes a user with the desired name * * @param name The name of the user - * @return OK if the user was deleted, NOT_FOUND if the user was not found, or INTERNAL_SERVER_ERROR if an error occurred - */ + * @return OK if the user was deleted, NOT_FOUND if the user was not found, or + * INTERNAL_SERVER_ERROR if an error occurred + */ @DeleteMapping("/{name}") public ResponseEntity deleteUser(@PathVariable String name) { try { @@ -137,7 +140,7 @@ public class UserController { return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } + } } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } -- cgit v1.2.3 From 98b66c0ccc3e555879f8d68bd61ea573184e4b3c Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Wed, 26 Feb 2025 17:00:54 -0500 Subject: Removed getUsers method --- .../api/ufundapi/controller/UserController.java | 21 --------------------- 1 file changed, 21 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java index 1af865d..a40c46f 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java @@ -54,27 +54,6 @@ public class UserController { } } - /** - * Responds to the GET request for all {@linkplain User user} - * - * @return ResponseEntity with array of {@link User user} objects (may be empty) - * and - * HTTP status of OK
- * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise - */ - @GetMapping("") - public ResponseEntity getUseers() { - LOG.info("GET /users"); - - try { - User[] users = UserDAO.getUsers(); - return new ResponseEntity<>(users, 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 User user} for the given id * -- cgit v1.2.3