diff options
author | Tyler Ferrari <69283684+Sowgro@users.noreply.github.com> | 2025-02-26 17:20:11 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-26 17:20:11 -0500 |
commit | 9d832c1e96ca061b80b32abda472269bf7e0bbb7 (patch) | |
tree | 67ccc4c1f33b64d75c9cc7d57d05da3bc541bd26 /ufund-api/src/main/java/com/ufund/api/ufundapi/controller | |
parent | 4c452d39214c42e2b0dc2a557b0c2ef1e3bcd9c0 (diff) | |
parent | da74d5c7e67b873dc8fdbfc672891eb63c0b8ea2 (diff) | |
download | JellySolutions-9d832c1e96ca061b80b32abda472269bf7e0bbb7.tar.gz JellySolutions-9d832c1e96ca061b80b32abda472269bf7e0bbb7.tar.bz2 JellySolutions-9d832c1e96ca061b80b32abda472269bf7e0bbb7.zip |
Merge pull request #7 from RIT-SWEN-261-02/user-functionality
Merge user-functionality into main
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller')
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java | 128 |
1 files changed, 128 insertions, 0 deletions
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..a40c46f --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java @@ -0,0 +1,128 @@ +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.RestController; + +import com.ufund.api.ufundapi.model.User; +import com.ufund.api.ufundapi.persistence.UserDAO; + +@RestController +@RequestMapping("users") +public class UserController { + private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); + private final UserDAO UserDAO; + + /** + * Create a user controller to receive REST signals + * + * @param UserDAO The Data Access Object + */ + public UserController(UserDAO userDAO) { + this.UserDAO = userDAO; + } + + /** + * 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 + */ + @PostMapping("") + public ResponseEntity<User> createUser(@RequestBody User user) { + try { + if (UserDAO.createUser(user) != null) { + return new ResponseEntity<>(user, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.CONFLICT); + } + + } catch (IOException ex) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + /** + * Responds to the GET request for a {@linkplain User user} for the given id + * + * @param id The id used to locate the {@link User user} + * + * @return ResponseEntity with {@link User user} 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("/{name}") + public ResponseEntity<User> getUser(@PathVariable String name) { + LOG.log(Level.INFO, "GET /user/{0}", name); + + try { + User user = UserDAO.getUser(name); + if (user != null) { + return new ResponseEntity<>(user, 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 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("/{name}") + public ResponseEntity<User> updateUser(@RequestBody User user, @PathVariable String name) { + try { + 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); + } + } + + /** + * 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 + */ + @DeleteMapping("/{name}") + public ResponseEntity<User> deleteUser(@PathVariable String name) { + try { + if (UserDAO.deleteUser(name)) { + return new ResponseEntity<>(HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } catch (IOException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + +} |