aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/controller
diff options
context:
space:
mode:
authorGunther6070 <haydenhartman10@yahoo.com>2025-03-06 17:24:47 -0500
committerGunther6070 <haydenhartman10@yahoo.com>2025-03-06 17:24:47 -0500
commit42c61d799bb5828949d71dfce6b83dccd3514768 (patch)
tree50c0cf6576f9053f354fe0f70a19789a81fb38b7 /ufund-api/src/main/java/com/ufund/api/ufundapi/controller
parente9d5addc7a0b65c426803171471ca5a042b73c93 (diff)
downloadJellySolutions-42c61d799bb5828949d71dfce6b83dccd3514768.tar.gz
JellySolutions-42c61d799bb5828949d71dfce6b83dccd3514768.tar.bz2
JellySolutions-42c61d799bb5828949d71dfce6b83dccd3514768.zip
Migrated user controller methods to user service. Also changed some return types.
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.java65
1 files changed, 30 insertions, 35 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
index aa9598d..02526af 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
@@ -5,29 +5,30 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
-import com.ufund.api.ufundapi.persistence.UserAuthDAO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.ufund.api.ufundapi.model.User;
-import com.ufund.api.ufundapi.persistence.UserDAO;
+import com.ufund.api.ufundapi.service.AuthService;
+import com.ufund.api.ufundapi.service.UserService;
@RestController
@RequestMapping("users")
public class UserController {
private static final Logger LOG = Logger.getLogger(UserController.class.getName());
- private final UserDAO UserDAO;
- private final UserAuthDAO userAuthDAO;
+ private final UserService userService;
+ private final AuthService authService;
/**
- * Create a user controller to receive REST signals
- *
- * @param userDAO The Data Access Object
+ * Creates a UserController
+ *
+ * @param userService
+ * @param authService
*/
- public UserController(UserDAO userDAO, UserAuthDAO userAuthDAO) {
- this.UserDAO = userDAO;
- this.userAuthDAO = userAuthDAO;
+ public UserController(UserService userService, AuthService authService) {
+ this.userService = userService;
+ this.authService = authService;
}
/**
@@ -37,13 +38,14 @@ public class UserController {
* otherwise
*/
@PostMapping("")
- public ResponseEntity<Boolean> createUser(@RequestBody Map<String, String> params) {
+ public ResponseEntity<User> createUser(@RequestBody Map<String, String> params) {
String username = params.get("username");
String password = params.get("password");
try {
- if (UserDAO.addUser(User.create(username, password)) != null) {
- return new ResponseEntity<>(true, HttpStatus.CREATED);
+ User user = userService.createUser(username, password);
+ if (user == null) {
+ return new ResponseEntity<>(user, HttpStatus.CREATED);
} else {
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
@@ -65,19 +67,16 @@ public class UserController {
public ResponseEntity<User> getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) {
LOG.log(Level.INFO, "GET /user/{0}", username);
- var userAuth = userAuthDAO.getUserAuth(key);
- if (userAuth == null || !userAuth.getUsername().equals(username)) {
- return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
- }
-
try {
- User user = UserDAO.getUser(username);
+ authService.authenticate(username, key);
+ User user = userService.getUser(username);
if (user != null) {
return new ResponseEntity<>(user.withoutPasswordHash(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
-
+ } catch (IllegalAccessException ex) {
+ return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
} catch (IOException e) {
LOG.log(Level.SEVERE, e.getLocalizedMessage());
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
@@ -92,16 +91,12 @@ public class UserController {
* @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, @RequestHeader("jelly-api-key") String key) {
-
- var userAuth = userAuthDAO.getUserAuth(key);
- if (userAuth == null || !userAuth.getUsername().equals(user.getUsername())) {
- return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
- }
+ @PutMapping("/{username}")
+ public ResponseEntity<User> updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) {
try {
- user = UserDAO.updateUser(user, name);
+ authService.authenticate(username, key);
+ user = userService.updateUser(user, username);
if (user != null) {
return new ResponseEntity<>(user, HttpStatus.OK);
} else {
@@ -110,6 +105,8 @@ public class UserController {
} catch (IOException e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+ } catch (IllegalAccessException e) {
+ return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}
@@ -121,21 +118,19 @@ public class UserController {
* INTERNAL_SERVER_ERROR if an error occurred
*/
@DeleteMapping("/{username}")
- public ResponseEntity<User> deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) {
-
- var userAuth = userAuthDAO.getUserAuth(key);
- if (userAuth == null || !userAuth.getUsername().equals(username)) {
- return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
- }
+ public ResponseEntity<Boolean> deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) {
try {
- if (UserDAO.deleteUser(username)) {
+ authService.authenticate(username, key);
+ if (userService.deleteUser(username)) {
return new ResponseEntity<>(HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (IOException e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+ } catch (IllegalAccessException e) {
+ return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}