From c02c47efcb00782feb1461534923023a711d4f15 Mon Sep 17 00:00:00 2001 From: sowgro Date: Sun, 2 Mar 2025 11:22:48 -0500 Subject: First attempt at an authentication system. --- .../api/ufundapi/controller/AuthController.java | 54 ++++++++++++++++++ .../api/ufundapi/controller/UserController.java | 64 ++++++++++++++-------- .../java/com/ufund/api/ufundapi/model/User.java | 39 +++++++------ .../com/ufund/api/ufundapi/model/UserAuth.java | 43 +++++++++++++++ .../api/ufundapi/persistence/UserAuthDAO.java | 23 ++++++++ .../api/ufundapi/persistence/UserAuthFIleDAO.java | 62 +++++++++++++++++++++ .../ufund/api/ufundapi/persistence/UserDAO.java | 16 +++--- .../api/ufundapi/persistence/UserFileDAO.java | 29 ++++------ 8 files changed, 263 insertions(+), 67 deletions(-) create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/model/UserAuth.java create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java new file mode 100644 index 0000000..aa27e3f --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java @@ -0,0 +1,54 @@ +package com.ufund.api.ufundapi.controller; + +import com.ufund.api.ufundapi.model.UserAuth; +import com.ufund.api.ufundapi.persistence.UserAuthDAO; +import com.ufund.api.ufundapi.persistence.UserDAO; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.io.IOException; +import java.util.Map; + +@RestController +@RequestMapping("auth") +public class AuthController { + private final UserDAO userDAO; + private final UserAuthDAO userAuthDAO; + + public AuthController(UserDAO userDAO, UserAuthDAO userAuthDAO) { + this.userDAO = userDAO; + this.userAuthDAO = userAuthDAO; + } + + /** + * Attempts to log in as a user + * @param params A map/json object in the format {username: string, password: string} + * @return An api key if the auth was successful, null otherwise + */ + @PostMapping("") + public ResponseEntity login(@RequestBody Map params) { + String username = params.get("username"); + String password = params.get("password"); + try { + var usr = userDAO.getUser(username); + if (usr == null || !usr.verifyPassword(password)) { + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } + var userAuth = UserAuth.generate(username); + userAuthDAO.addUserAuth(userAuth); + return new ResponseEntity<>(userAuth.getKey(), HttpStatus.OK); + } catch (IOException ex) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + /** + * TODO + * @return + */ + @DeleteMapping("") + public ResponseEntity logout() { + return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); + } +} 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 4e5f156..aa9598d 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 @@ -1,19 +1,14 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +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.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 org.springframework.web.bind.annotation.*; import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.persistence.UserDAO; @@ -21,30 +16,34 @@ import com.ufund.api.ufundapi.persistence.UserDAO; @RestController @RequestMapping("users") public class UserController { - private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); + private static final Logger LOG = Logger.getLogger(UserController.class.getName()); private final UserDAO UserDAO; + private final UserAuthDAO userAuthDAO; /** * Create a user controller to receive REST signals * * @param userDAO The Data Access Object */ - public UserController(UserDAO userDAO) { + public UserController(UserDAO userDAO, UserAuthDAO userAuthDAO) { this.UserDAO = userDAO; + this.userAuthDAO = userAuthDAO; } /** * 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 createUser(@RequestBody User user) { + public ResponseEntity createUser(@RequestBody Map params) { + String username = params.get("username"); + String password = params.get("password"); + try { - if (UserDAO.createUser(user) != null) { - return new ResponseEntity<>(user, HttpStatus.CREATED); + if (UserDAO.addUser(User.create(username, password)) != null) { + return new ResponseEntity<>(true, HttpStatus.CREATED); } else { return new ResponseEntity<>(HttpStatus.CONFLICT); } @@ -62,14 +61,19 @@ public class UserController { * ResponseEntity with HTTP status of NOT_FOUND if not found
* ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise */ - @GetMapping("/{name}") - public ResponseEntity getUser(@PathVariable String name) { - LOG.log(Level.INFO, "GET /user/{0}", name); + @GetMapping("/{username}") + public ResponseEntity 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(name); + User user = UserDAO.getUser(username); if (user != null) { - return new ResponseEntity<>(user, HttpStatus.OK); + return new ResponseEntity<>(user.withoutPasswordHash(), HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } @@ -89,7 +93,13 @@ public class UserController { * INTERNAL_SERVER_ERROR if there was an issue */ @PutMapping("/{name}") - public ResponseEntity updateUser(@RequestBody User user, @PathVariable String name) { + public ResponseEntity 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); + } + try { user = UserDAO.updateUser(user, name); if (user != null) { @@ -106,14 +116,20 @@ public class UserController { /** * Deletes a user with the desired name * - * @param name The name of the user + * @param username 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 deleteUser(@PathVariable String name) { + @DeleteMapping("/{username}") + public ResponseEntity 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); + } + try { - if (UserDAO.deleteUser(name)) { + if (UserDAO.deleteUser(username)) { return new ResponseEntity<>(HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java index 59f4c46..1e182a6 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java @@ -7,8 +7,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; public class User { - @JsonProperty("name") - private final String name; + @JsonProperty("username") + private final String username; @JsonProperty("passwordHash") private int passwordHash; @JsonProperty("basket") @@ -17,36 +17,35 @@ public class User { /** * Create a new user * - * @param name The name of the user + * @param username The name of the user */ - public User(String name) { - this.name = name; + public User(String username) { + this.username = username; basket = new ArrayList<>(); } /** * Create a new user * - * @param name The name of the user + * @param username The name of the user * @param basket A basket to copy from */ - public User(@JsonProperty("name") String name, @JsonProperty("basket") List basket) { - this.name = name; + public User(@JsonProperty("username") String username, @JsonProperty("passwordHash") int passwordHash, @JsonProperty("basket") List basket) { + this.username = username; this.basket = basket; + this.passwordHash = passwordHash; } - /** - * Create a deep copy of another user - * - * @param other The user to copy from - */ - public User(User other) { - this.name = other.name; - this.basket = other.basket; + public static User create(String username, String password) { + return new User( + username, + password.hashCode(), + new ArrayList<>() + ); } - public String getName() { - return name; + public String getUsername() { + return username; } public boolean verifyPassword(String password) { @@ -65,4 +64,8 @@ public class User { basket.remove(need); } + public User withoutPasswordHash() { + return new User(this.username, 0, this.basket); + } + } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/UserAuth.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/UserAuth.java new file mode 100644 index 0000000..1c11a28 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/UserAuth.java @@ -0,0 +1,43 @@ +package com.ufund.api.ufundapi.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.time.LocalDateTime; +import java.util.UUID; + +public class UserAuth { + @JsonProperty("key") String key; + @JsonProperty("username") String username; + @JsonProperty("expiration") LocalDateTime expiration; + + public UserAuth(@JsonProperty("key") String key, @JsonProperty("username") String username, @JsonProperty("expiration") LocalDateTime expiration) { + this.key = key; + this.expiration = expiration; + this.username = username; + } + + /** + * Generate a new user authentication profile + * @param username the username the key will belong to + * @return The new user authentication profile + */ + public static UserAuth generate(String username) { + return new UserAuth( + UUID.randomUUID().toString(), + username, + LocalDateTime.now().plusDays(30) + ); + } + + public String getKey() { + return key; + } + + public String getUsername() { + return username; + } + + public LocalDateTime getExpiration() { + return expiration; + } +} diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java new file mode 100644 index 0000000..45515b8 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java @@ -0,0 +1,23 @@ +package com.ufund.api.ufundapi.persistence; + +import com.ufund.api.ufundapi.model.UserAuth; + +import java.io.IOException; + +public interface UserAuthDAO { + + /** + * Get a user authentication profile + * @param key The auth key + * @return The authentication profile or null if there was none + */ + UserAuth getUserAuth(String key); + + /** + * Add a user authentication profile + * @param userAuth The user auth profile to add + * @return True if it was successful + * @throws IOException On any file writing error + */ + boolean addUserAuth(UserAuth userAuth) throws IOException; +} diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java new file mode 100644 index 0000000..67918cc --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java @@ -0,0 +1,62 @@ +package com.ufund.api.ufundapi.persistence; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.ufund.api.ufundapi.model.UserAuth; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +@Component +public class UserAuthFIleDAO implements UserAuthDAO { + + private final Map userAuthMap; + private final ObjectMapper objectMapper; + private final String filename; + + public UserAuthFIleDAO(ObjectMapper objectMapper, @Value("${authKeys.file}") String filename) throws IOException { + this.userAuthMap = new HashMap<>(); + this.objectMapper = objectMapper; + this.filename = filename; + load(); + } + + private void load() throws IOException { + userAuthMap.clear(); + + UserAuth[] userAuthKeysArray = objectMapper.readValue(new File(filename), UserAuth[].class); + + for (UserAuth userAuth : userAuthKeysArray) { + userAuthMap.put(userAuth.getKey(), userAuth); + } + } + + private void save() throws IOException { + objectMapper.writeValue(new File(filename), userAuthMap.values()); + } + + public UserAuth[] getAuthKeys() { + synchronized (userAuthMap) { + return userAuthMap.values().toArray(UserAuth[]::new); + } + } + + @Override + public UserAuth getUserAuth(String key) { + synchronized (userAuthMap) { + return userAuthMap.get(key); + } + } + + @Override + public boolean addUserAuth(UserAuth userAuth) throws IOException { + synchronized (userAuthMap) { + userAuthMap.put(userAuth.getKey(), userAuth); + save(); + return true; + } + } +} diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java index d456abc..6558ce2 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java @@ -21,17 +21,17 @@ public interface UserDAO { User[] getUsers() throws IOException; /** - * Retrieves a {@linkplain User user} with the given name + * Retrieves a {@linkplain User user} with the given username * - * @param id The ID of the {@link User user} to get + * @param username The ID of the {@link User user} to get * - * @return a {@link User user} object with the matching name + * @return a {@link User user} object with the matching username *
- * null if no {@link User user} with a matching name is found + * null if no {@link User user} with a matching username is found * * @throws IOException if an issue with underlying storage */ - User getUser(String name) throws IOException; + User getUser(String username) throws IOException; /** * Creates and saves a {@linkplain User user} @@ -44,7 +44,7 @@ public interface UserDAO { * * @throws IOException if an issue with underlying storage */ - User createUser(User user) throws IOException; + User addUser(User user) throws IOException; /** * Updates and saves a {@linkplain User user} @@ -62,7 +62,7 @@ public interface UserDAO { /** * Deletes a {@linkplain User user} with the given id * - * @param id The id of the {@link User user} + * @param username The id of the {@link User user} * * @return true if the {@link User user} was deleted *
@@ -70,5 +70,5 @@ public interface UserDAO { * * @throws IOException if underlying storage cannot be accessed */ - boolean deleteUser(String name) throws IOException; + boolean deleteUser(String username) throws IOException; } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java index 18eec18..54ce74a 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -36,7 +36,7 @@ public class UserFileDAO implements UserDAO { User[] usersArray = objectMapper.readValue(new File(filename), User[].class); for (User user : usersArray) { - users.put(user.getName(), user); + users.put(user.getUsername(), user); } } @@ -72,15 +72,15 @@ public class UserFileDAO implements UserDAO { /** * Return the user with the String name name or null otherwise * - * @param name Name of desired user + * @param username Name of desired user * * @return Desired user, null otherwise * @throws IOException If there was an IO issue saving the file */ @Override - public User getUser(String name) throws IOException { + public User getUser(String username) throws IOException { synchronized (users) { - return users.getOrDefault(name, null); + return users.getOrDefault(username, null); } } @@ -93,16 +93,11 @@ public class UserFileDAO implements UserDAO { * @throws IOException If there was an IO issue saving the file */ @Override - public User createUser(User user) throws IOException { + public User addUser(User user) throws IOException { synchronized (users) { - if (getUser(user.getName()) == null) { - User newUser = new User(user); - users.put(newUser.getName(), newUser); - save(); - return newUser; - } else { - return null; - } + var res = users.putIfAbsent(user.getUsername(), user); + save(); + return res; } } @@ -131,16 +126,16 @@ public class UserFileDAO implements UserDAO { /** * Delete a user matching the name * - * @param name The name of the user + * @param username The name of the user * * @return True if deleted, false otherwise * @throws IOException If there was an IO issue saving the file */ @Override - public boolean deleteUser(String name) throws IOException { + public boolean deleteUser(String username) throws IOException { synchronized (users) { - if (users.containsKey(name)) { - users.remove(name); + if (users.containsKey(username)) { + users.remove(username); return save(); } else { return false; -- cgit v1.2.3 From 4cfacd63b1552bf6ea33e28f3f66e11b75e5756a Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 12:45:35 -0500 Subject: Created Cupboard Service and refactored the controller and DAO to add the service as an inbetween with logic --- .../ufundapi/controller/CupboardController.java | 55 ++++++++------ .../java/com/ufund/api/ufundapi/model/Need.java | 13 ++++ .../api/ufundapi/persistence/CupboardDAO.java | 17 +---- .../api/ufundapi/persistence/CupboardFileDao.java | 21 +----- .../api/ufundapi/service/CupboardService.java | 83 ++++++++++++++++++++++ 5 files changed, 133 insertions(+), 56 deletions(-) create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java (limited to 'ufund-api/src/main/java') 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 4b2a04d..6b0bb71 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 @@ -1,6 +1,7 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -17,21 +18,23 @@ 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.CupboardDAO; +import com.ufund.api.ufundapi.model.Need.GoalType; +import com.ufund.api.ufundapi.service.CupboardService; +import com.ufund.api.ufundapi.service.CupboardService.DuplicateKeyException; @RestController @RequestMapping("cupboard") public class CupboardController { private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); - private final CupboardDAO cupboardDAO; + private final CupboardService cupboardService; /** * Create a cupboard controller to receive REST signals * - * @param cupboardDAO The Data Access Object + * @param cupboardService The Data Access Object */ - public CupboardController(CupboardDAO cupboardDAO) { - this.cupboardDAO = cupboardDAO; + public CupboardController(CupboardService cupboardService) { + this.cupboardService = cupboardService; } /** @@ -41,16 +44,20 @@ public class CupboardController { * @return OK response and the need if it was successful, INTERNAL_SERVER_ERROR otherwise */ @PostMapping("") - public ResponseEntity createNeed(@RequestBody Need need) { + public ResponseEntity createNeed(@RequestBody Map params) { + String name = params.get("name"); + int maxGoal = Integer.parseInt(params.get("maxGoal")); + Need.GoalType goalType = GoalType.valueOf(params.get("maxGoal")); + try { - if (need.getMaxGoal() <= 0) { - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } - if (need.getMaxGoal() < need.getCurrent()) { - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } - cupboardDAO.createNeed(need); + + Need need = cupboardService.createNeed(name, maxGoal, goalType); return new ResponseEntity<>(need, HttpStatus.OK); + + } catch (DuplicateKeyException ex) { + return new ResponseEntity<>(HttpStatus.CONFLICT); + } catch (IllegalArgumentException ex) { + return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); } catch (IOException ex) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } @@ -69,7 +76,7 @@ public class CupboardController { LOG.info("GET /needs"); try { - Need[] needs = cupboardDAO.getNeeds(); + Need[] needs = cupboardService.getNeeds(); return new ResponseEntity<>(needs, HttpStatus.OK); } catch (IOException e) { LOG.log(Level.SEVERE, e.getLocalizedMessage()); @@ -93,8 +100,8 @@ public class CupboardController { LOG.info("GET /need/?name="+name); try { - Need[] needArray = cupboardDAO.findNeeds(name); - return new ResponseEntity<>(needArray, HttpStatus.OK); + Need[] needs = cupboardService.searchNeeds(name); + return new ResponseEntity<>(needs, HttpStatus.OK); } catch (IOException e) { LOG.log(Level.SEVERE,e.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); @@ -116,7 +123,7 @@ public class CupboardController { LOG.log(Level.INFO, "GET /need/{0}", id); try { - Need need = cupboardDAO.getNeed(id); + Need need = cupboardService.getNeed(id); if (need != null) { return new ResponseEntity<>(need, HttpStatus.OK); } else { @@ -140,8 +147,12 @@ public class CupboardController { @PutMapping("") public ResponseEntity updateNeed(@RequestBody Need need) { try { - need = cupboardDAO.updateNeed(need); - return new ResponseEntity<>(need, HttpStatus.OK); + Need updatedNeed = cupboardService.updateNeed(need); + if (updatedNeed != null) { + return new ResponseEntity<>(need, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } @@ -156,9 +167,9 @@ public class CupboardController { @DeleteMapping("/{id}") public ResponseEntity deleteNeed(@PathVariable int id) { try { - if (cupboardDAO.getNeed(id) != null) { - cupboardDAO.deleteNeed(id); - return new ResponseEntity<>(HttpStatus.OK); + Need need = cupboardService.getNeed(id); + if (cupboardService.deleteNeed(id)) { + return new ResponseEntity<>(need, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java index 2611357..9ca097a 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java @@ -31,6 +31,19 @@ public class Need { this.type = type; } + /** + * Create a new need + * + * @param name The name of the need + * @param maxGoal The maximum goal for this need + * @param type The type of need (monetary, physical) + */ + public Need(String name, GoalType type, double maxGoal) { + this.name = name; + this.type = type; + this.maxGoal = maxGoal; + } + /** * Create a deep copy of another need * diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java index 1435410..6baf3e4 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java @@ -1,9 +1,9 @@ package com.ufund.api.ufundapi.persistence; -import com.ufund.api.ufundapi.model.Need; - import java.io.IOException; +import com.ufund.api.ufundapi.model.Need; + /** * Defines the interface for Need object persistence * @@ -19,17 +19,6 @@ public interface CupboardDAO { */ Need[] getNeeds() throws IOException; - /** - * Finds all {@linkplain Need needs} whose name contains the given text - * - * @param targetName The text to match against - * - * @return An array of {@link Need needs} whose names contains the given text, may be empty - * - * @throws IOException if an issue with underlying storage - */ - Need[] findNeeds(String targetName) throws IOException; - /** * Retrieves a {@linkplain Need need} with the given name * @@ -54,7 +43,7 @@ public interface CupboardDAO { * * @throws IOException if an issue with underlying storage */ - Need createNeed(Need need) throws IOException; + Need addNeed(Need need) throws IOException; /** * Updates and saves a {@linkplain Need need} diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java index 81ee7c0..84ea693 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java @@ -60,18 +60,6 @@ public class CupboardFileDao implements CupboardDAO { return needs.values().toArray(Need[]::new); } - /** - * Returns an array of needs filtered by a search - * - * @param search The search substring - * @return The requested array - */ - private Need[] getNeedsArray(String search) { - return needs.values().stream() - .filter(i -> i.getName().toLowerCase().contains(search.toLowerCase())) - .toArray(Need[]::new); - } - /** * Saves the needs to json * @@ -92,13 +80,6 @@ public class CupboardFileDao implements CupboardDAO { } } - @Override - public Need[] findNeeds(String targetName) { - synchronized (needs) { - return getNeedsArray(targetName); - } - } - @Override public Need getNeed(int id) { synchronized (needs) { @@ -107,7 +88,7 @@ public class CupboardFileDao implements CupboardDAO { } @Override - public Need createNeed(Need need) throws IOException { + public Need addNeed(Need need) throws IOException { synchronized (needs) { Need newNeed = new Need(need); newNeed.setID(nextId()); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java new file mode 100644 index 0000000..860a2a8 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -0,0 +1,83 @@ +package com.ufund.api.ufundapi.service; + +import java.io.IOException; +import java.util.Arrays; + +import com.ufund.api.ufundapi.model.Need; +import com.ufund.api.ufundapi.persistence.CupboardDAO; + +public class CupboardService { + + private final CupboardDAO cupboardDAO; + + public class DuplicateKeyException extends Exception { + + public DuplicateKeyException(String message) { + super(message); + } + + } + + public CupboardService(CupboardDAO cupboardDAO) { + this.cupboardDAO = cupboardDAO; + } + + public Need createNeed(String name, int maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { + + Need need = new Need(name, goalType, maxGoal); + + if (need.getMaxGoal() <= 0) { + throw new IllegalArgumentException("Max Goal must be greater than zero"); + } else { + for (Need searchNeed : cupboardDAO.getNeeds()) { + if (need.getName().equalsIgnoreCase(searchNeed.getName())) { + throw new DuplicateKeyException("Duplicate names are not allowed"); + } + } + return cupboardDAO.addNeed(need); + } + + } + + public Need[] getNeeds() throws IOException { + return cupboardDAO.getNeeds(); + } + + /** + * Returns an array of needs filtered by a search + * + * @param search The search substring + * @return The requested array + * @throws IOException + */ + public Need[] searchNeeds(String search) throws IOException { + return Arrays.stream(cupboardDAO.getNeeds()) + .filter(i -> i.getName().toLowerCase().contains(search.toLowerCase())) + .toArray(Need[]::new); + } + + /** + * @param id + * @return + * @throws IOException + */ + public Need getNeed(int id) throws IOException { + return cupboardDAO.getNeed(id); + } + + /** + * + * @param need + * @return + * @throws IOException + */ + public Need updateNeed(Need need) throws IOException { + return cupboardDAO.updateNeed(need); + } + + public boolean deleteNeed(int id) throws IOException { + return cupboardDAO.deleteNeed(id); + } + + +} -- cgit v1.2.3 From 7cfa986e9c46f16c08fb490f3af9717a20488a1f Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 12:45:48 -0500 Subject: Created Auth service class --- .../src/main/java/com/ufund/api/ufundapi/service/AuthService.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java new file mode 100644 index 0000000..caf1edd --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -0,0 +1,5 @@ +package com.ufund.api.ufundapi.service; + +public class AuthService { + +} -- cgit v1.2.3 From d2539df788d97e23dedd06cf42eca92c4aa08112 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 12:45:53 -0500 Subject: Created user service class --- .../src/main/java/com/ufund/api/ufundapi/service/UserService.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java new file mode 100644 index 0000000..994512d --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java @@ -0,0 +1,5 @@ +package com.ufund.api.ufundapi.service; + +public class UserService { + +} -- cgit v1.2.3 From e9d5addc7a0b65c426803171471ca5a042b73c93 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 17:24:15 -0500 Subject: Migrated auth controller methods to auth service --- .../api/ufundapi/controller/AuthController.java | 24 +++++++------- .../ufund/api/ufundapi/service/AuthService.java | 38 +++++++++++++++++++++- 2 files changed, 49 insertions(+), 13 deletions(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java index aa27e3f..b9c8ed3 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java @@ -3,22 +3,25 @@ package com.ufund.api.ufundapi.controller; import com.ufund.api.ufundapi.model.UserAuth; import com.ufund.api.ufundapi.persistence.UserAuthDAO; import com.ufund.api.ufundapi.persistence.UserDAO; +import com.ufund.api.ufundapi.service.AuthService; +import com.ufund.api.ufundapi.service.UserService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.util.Map; @RestController @RequestMapping("auth") public class AuthController { - private final UserDAO userDAO; - private final UserAuthDAO userAuthDAO; + private final UserService userService; + private final AuthService authService; - public AuthController(UserDAO userDAO, UserAuthDAO userAuthDAO) { - this.userDAO = userDAO; - this.userAuthDAO = userAuthDAO; + public AuthController(UserService userService, AuthService authService) { + this.userService = userService; + this.authService = authService; } /** @@ -31,15 +34,12 @@ public class AuthController { String username = params.get("username"); String password = params.get("password"); try { - var usr = userDAO.getUser(username); - if (usr == null || !usr.verifyPassword(password)) { - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); - } - var userAuth = UserAuth.generate(username); - userAuthDAO.addUserAuth(userAuth); - return new ResponseEntity<>(userAuth.getKey(), HttpStatus.OK); + String key = authService.login(username, password); + return new ResponseEntity<>(key, HttpStatus.OK); } catch (IOException ex) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } catch (IllegalAccessException e) { + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index caf1edd..2e644ee 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -1,5 +1,41 @@ package com.ufund.api.ufundapi.service; +import com.ufund.api.ufundapi.model.UserAuth; +import com.ufund.api.ufundapi.persistence.UserAuthDAO; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component public class AuthService { - + + private final UserAuthDAO userAuthDAO; + private final UserService userService; + + public AuthService(UserAuthDAO userAuthDAO, UserService userService) { + this.userAuthDAO = userAuthDAO; + this.userService = userService; + } + + public UserAuth getUserAuth(String key) { + return userAuthDAO.getUserAuth(key); + } + + public void authenticate(String username, String key) throws IllegalAccessException { + var userAuth = getUserAuth(key); + if (userAuth == null || !userAuth.getUsername().equals(username)) { + throw new IllegalAccessException("Unauthorized"); + } + } + + public String login(String username, String password) throws IllegalAccessException, IOException { + var usr = userService.getUser(username); + if (usr == null || !usr.verifyPassword(password)) { + throw new IllegalAccessException("Unauthorized"); + } + var userAuth = UserAuth.generate(username); + userAuthDAO.addUserAuth(userAuth); + return userAuth.getKey(); + } + } -- cgit v1.2.3 From 42c61d799bb5828949d71dfce6b83dccd3514768 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 17:24:47 -0500 Subject: Migrated user controller methods to user service. Also changed some return types. --- .../api/ufundapi/controller/UserController.java | 65 ++++++++++------------ .../api/ufundapi/persistence/UserFileDAO.java | 5 ++ .../ufund/api/ufundapi/service/UserService.java | 38 +++++++++++++ 3 files changed, 73 insertions(+), 35 deletions(-) (limited to 'ufund-api/src/main/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 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 createUser(@RequestBody Map params) { + public ResponseEntity createUser(@RequestBody Map 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 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 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 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 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 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); } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java index 54ce74a..4f43f8c 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -97,6 +97,11 @@ public class UserFileDAO implements UserDAO { synchronized (users) { var res = users.putIfAbsent(user.getUsername(), user); save(); + if (res == null) { + return user; + } else { + + } return res; } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java index 994512d..c23bf89 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java @@ -1,5 +1,43 @@ package com.ufund.api.ufundapi.service; +import java.io.IOException; + +import com.ufund.api.ufundapi.model.User; +import com.ufund.api.ufundapi.persistence.UserAuthDAO; +import com.ufund.api.ufundapi.persistence.UserDAO; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; + +@Component public class UserService { + + private final UserDAO userDAO; + + /** + * Create a user controller to receive REST signals + * + * @param userDao The Data Access Object + */ + public UserService(UserDAO userDao, AuthService authService) { + this.userDAO = userDao; + } + + public User createUser(String username, String password) throws IOException { + User user = User.create(username, password); + return userDAO.addUser(user); + } + + public User getUser(String username) throws IOException, IllegalAccessException { + return userDAO.getUser(username); + } + + public User updateUser(User user, String name) throws IllegalAccessException, IOException { + return userDAO.updateUser(user, name); + } + + public Boolean deleteUser(String username) throws IllegalAccessException, IOException { + return userDAO.deleteUser(username); + } } -- cgit v1.2.3 From 1fe3905e9d4354657d22e9dbc1a244108ab55a83 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 17:27:15 -0500 Subject: Removed unused imports and fixed other warnings --- .../java/com/ufund/api/ufundapi/controller/AuthController.java | 8 +------- .../main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | 2 -- .../src/main/java/com/ufund/api/ufundapi/service/UserService.java | 3 --- 3 files changed, 1 insertion(+), 12 deletions(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java index b9c8ed3..1a545f6 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java @@ -1,26 +1,20 @@ package com.ufund.api.ufundapi.controller; -import com.ufund.api.ufundapi.model.UserAuth; -import com.ufund.api.ufundapi.persistence.UserAuthDAO; -import com.ufund.api.ufundapi.persistence.UserDAO; import com.ufund.api.ufundapi.service.AuthService; import com.ufund.api.ufundapi.service.UserService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.util.Map; @RestController @RequestMapping("auth") public class AuthController { - private final UserService userService; private final AuthService authService; - public AuthController(UserService userService, AuthService authService) { - this.userService = userService; + public AuthController(AuthService authService) { this.authService = authService; } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java index 4f43f8c..dca812b 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -99,8 +99,6 @@ public class UserFileDAO implements UserDAO { save(); if (res == null) { return user; - } else { - } return res; } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java index c23bf89..a545029 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java @@ -3,10 +3,7 @@ package com.ufund.api.ufundapi.service; import java.io.IOException; import com.ufund.api.ufundapi.model.User; -import com.ufund.api.ufundapi.persistence.UserAuthDAO; import com.ufund.api.ufundapi.persistence.UserDAO; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; @Component -- cgit v1.2.3 From bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3 Mon Sep 17 00:00:00 2001 From: sowgro Date: Thu, 6 Mar 2025 21:41:39 -0500 Subject: Push current changes (working on documentation and tests) --- .../api/ufundapi/controller/AuthController.java | 25 ++-- .../ufundapi/controller/CupboardController.java | 15 +-- .../api/ufundapi/persistence/CupboardDAO.java | 4 - .../api/ufundapi/persistence/CupboardFileDAO.java | 126 +++++++++++++++++++++ .../api/ufundapi/persistence/CupboardFileDao.java | 126 --------------------- .../api/ufundapi/persistence/UserAuthDAO.java | 17 ++- .../api/ufundapi/persistence/UserAuthFIleDAO.java | 27 +++-- .../api/ufundapi/persistence/UserFileDAO.java | 23 +--- .../ufund/api/ufundapi/service/AuthService.java | 32 +++++- .../api/ufundapi/service/CupboardService.java | 25 ++-- .../ufund/api/ufundapi/service/UserService.java | 2 +- 11 files changed, 231 insertions(+), 191 deletions(-) create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java delete mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java index 1a545f6..b0390ae 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java @@ -20,8 +20,10 @@ public class AuthController { /** * Attempts to log in as a user - * @param params A map/json object in the format {username: string, password: string} - * @return An api key if the auth was successful, null otherwise + * + * @param params A json object in the format {username: string, password: string} + * @return An api key and status OK if the authentication was successful, + * Status UNAUTHORIZED if the authentication failed and INTERNAL SERVER ERROR otherwise. */ @PostMapping("") public ResponseEntity login(@RequestBody Map params) { @@ -30,19 +32,26 @@ public class AuthController { try { String key = authService.login(username, password); return new ResponseEntity<>(key, HttpStatus.OK); - } catch (IOException ex) { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } catch (IllegalAccessException e) { return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } catch (IOException ex) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } /** - * TODO - * @return + * Logs out the current user + * + * @param key The API sent by the client in the header + * @return OK if the user was successfully logged out, INTERNAL_SERVER_ERROR otherwise. */ @DeleteMapping("") - public ResponseEntity logout() { - return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); + public ResponseEntity logout(@RequestHeader("jelly-api-key") String key) { + try { + authService.logout(key); + return new ResponseEntity<>(HttpStatus.OK); + } catch (IOException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } } 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 6b0bb71..dfcb8a3 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 @@ -40,8 +40,11 @@ public class CupboardController { /** * 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 + * @param params The need to create + * @return OK response and the need if it was successful, + * CONFLICT if another need with the same name exists + * UNPROCESSABLE_ENTITY if the need contains bad data + * INTERNAL_SERVER_ERROR otherwise */ @PostMapping("") public ResponseEntity createNeed(@RequestBody Map params) { @@ -50,10 +53,8 @@ public class CupboardController { Need.GoalType goalType = GoalType.valueOf(params.get("maxGoal")); try { - Need need = cupboardService.createNeed(name, maxGoal, goalType); return new ResponseEntity<>(need, HttpStatus.OK); - } catch (DuplicateKeyException ex) { return new ResponseEntity<>(HttpStatus.CONFLICT); } catch (IllegalArgumentException ex) { @@ -113,10 +114,8 @@ public class CupboardController { * * @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
+ * @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) { @@ -129,7 +128,6 @@ public class CupboardController { } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - } catch (IOException e) { LOG.log(Level.SEVERE, e.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); @@ -143,7 +141,6 @@ public class CupboardController { * @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 { diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java index 6baf3e4..c8285a0 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java @@ -14,8 +14,6 @@ public interface CupboardDAO { * Retrieves all {@linkplain Need needs} * * @return An array of {@link Need need} objects, may be empty - * - * @throws IOException if an issue with underlying storage */ Need[] getNeeds() throws IOException; @@ -27,8 +25,6 @@ public interface CupboardDAO { * @return a {@link Need need} object with the matching name *
* null if no {@link Need need} with a matching name is found - * - * @throws IOException if an issue with underlying storage */ Need getNeed(int id) throws IOException; diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java new file mode 100644 index 0000000..c4aaca3 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java @@ -0,0 +1,126 @@ +package com.ufund.api.ufundapi.persistence; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.ufund.api.ufundapi.model.Need; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.TreeMap; + +@Component +public class CupboardFileDAO implements CupboardDAO { + + private final Map needs; // cache + private final ObjectMapper objectMapper; + private static int nextId; + private final String filename; + + public CupboardFileDAO(@Value("${cupboard.file}") String filename, ObjectMapper objectMapper) throws IOException { + this.filename = filename; + this.objectMapper = objectMapper; + needs = new TreeMap<>(); + load(); // load the heroes from the file + } + + private synchronized static int nextId() { + int id = nextId; + nextId++; + return id; + } + + /** + * Load changes from the json file + * + * @throws IOException Any IO issue with the file + */ + private void load() throws IOException { + needs.clear(); + nextId = 0; + + Need[] needsArray = objectMapper.readValue(new File(filename), Need[].class); + + for (Need need : needsArray) { + needs.put(need.getId(), need); + if (need.getId() > nextId()) { + nextId = need.getId(); + } + } + nextId++; + } + + /** + * Return an array of the needs + * + * @return An array of all the needs + */ + private Need[] getNeedsArray() { + return needs.values().toArray(Need[]::new); + } + + /** + * Saves the needs to json + * + * @return True if the save was successful, false otherwise + * @throws IOException If there was an IO issue saving the file + */ + private boolean save() throws IOException { + Need[] needArray = getNeedsArray(); + + objectMapper.writeValue(new File(filename), needArray); + return true; + } + + @Override + public Need[] getNeeds() { + synchronized (needs) { + return getNeedsArray(); + } + } + + @Override + public Need getNeed(int id) { + synchronized (needs) { + return needs.getOrDefault(id, null); + } + } + + @Override + public Need addNeed(Need need) throws IOException { + synchronized (needs) { + Need newNeed = new Need(need); + newNeed.setID(nextId()); + needs.put(newNeed.getId(), newNeed); + save(); + return newNeed; + } + } + + @Override + public Need updateNeed(Need need) throws IOException { + synchronized (needs) { + if (needs.containsKey(need.getId())) { + needs.put(need.getId(), need); + save(); + return need; + } else { + return null; + } + + } + } + + @Override + public boolean deleteNeed(int id) throws IOException { + synchronized (needs) { + if (needs.containsKey(id)) { + needs.remove(id); + return save(); + } else { + return false; + } + } + } +} \ No newline at end of file diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java deleted file mode 100644 index 84ea693..0000000 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.ufund.api.ufundapi.persistence; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.ufund.api.ufundapi.model.Need; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -import java.io.File; -import java.io.IOException; -import java.util.Map; -import java.util.TreeMap; - -@Component -public class CupboardFileDao implements CupboardDAO { - - private final Map needs; // cache - private final ObjectMapper objectMapper; - private static int nextId; - private final String filename; - - public CupboardFileDao(@Value("${cupboard.file}") String filename, ObjectMapper objectMapper) throws IOException { - this.filename = filename; - this.objectMapper = objectMapper; - needs = new TreeMap<>(); - load(); // load the heroes from the file - } - - private synchronized static int nextId() { - int id = nextId; - nextId++; - return id; - } - - /** - * Load changes from the json file - * - * @throws IOException Any IO issue with the file - */ - private void load() throws IOException { - needs.clear(); - nextId = 0; - - Need[] needsArray = objectMapper.readValue(new File(filename), Need[].class); - - for (Need need : needsArray) { - needs.put(need.getId(), need); - if (need.getId() > nextId()) { - nextId = need.getId(); - } - } - nextId++; - } - - /** - * Return an array of the needs - * - * @return An array of all the needs - */ - private Need[] getNeedsArray() { - return needs.values().toArray(Need[]::new); - } - - /** - * Saves the needs to json - * - * @return True if the save was successful, false otherwise - * @throws IOException If there was an IO issue saving the file - */ - private boolean save() throws IOException { - Need[] needArray = getNeedsArray(); - - objectMapper.writeValue(new File(filename), needArray); - return true; - } - - @Override - public Need[] getNeeds() { - synchronized (needs) { - return getNeedsArray(); - } - } - - @Override - public Need getNeed(int id) { - synchronized (needs) { - return needs.getOrDefault(id, null); - } - } - - @Override - public Need addNeed(Need need) throws IOException { - synchronized (needs) { - Need newNeed = new Need(need); - newNeed.setID(nextId()); - needs.put(newNeed.getId(), newNeed); - save(); - return newNeed; - } - } - - @Override - public Need updateNeed(Need need) throws IOException { - synchronized (needs) { - if (needs.containsKey(need.getId())) { - needs.put(need.getId(), need); - save(); - return need; - } else { - return null; - } - - } - } - - @Override - public boolean deleteNeed(int id) throws IOException { - synchronized (needs) { - if (needs.containsKey(id)) { - needs.remove(id); - return save(); - } else { - return false; - } - } - } -} \ No newline at end of file diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java index 45515b8..355aae4 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java @@ -8,16 +8,25 @@ public interface UserAuthDAO { /** * Get a user authentication profile + * * @param key The auth key * @return The authentication profile or null if there was none */ - UserAuth getUserAuth(String key); + UserAuth getUserAuth(String key) throws IOException; /** * Add a user authentication profile + * * @param userAuth The user auth profile to add - * @return True if it was successful - * @throws IOException On any file writing error + * @throws IOException Thrown on any file writing error */ - boolean addUserAuth(UserAuth userAuth) throws IOException; + void addUserAuth(UserAuth userAuth) throws IOException; + + /** + * Remove a user authentication profile + * + * @param key The key of the user auth profile to remove + * @throws IOException Thrown on any file writing error + */ + void removeUserAuth(String key) throws IOException; } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java index 67918cc..4494939 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java @@ -24,6 +24,11 @@ public class UserAuthFIleDAO implements UserAuthDAO { load(); } + /** + * Loads the data from the file into the map + * + * @throws IOException Thrown if there was an issue reading the file + */ private void load() throws IOException { userAuthMap.clear(); @@ -34,29 +39,35 @@ public class UserAuthFIleDAO implements UserAuthDAO { } } + /** + * Saves the data from the map into the json file + * + * @throws IOException Thrown on any problem writing the file + */ private void save() throws IOException { objectMapper.writeValue(new File(filename), userAuthMap.values()); } - public UserAuth[] getAuthKeys() { + @Override + public UserAuth getUserAuth(String key) { synchronized (userAuthMap) { - return userAuthMap.values().toArray(UserAuth[]::new); + return userAuthMap.get(key); } } @Override - public UserAuth getUserAuth(String key) { + public void addUserAuth(UserAuth userAuth) throws IOException { synchronized (userAuthMap) { - return userAuthMap.get(key); + userAuthMap.put(userAuth.getKey(), userAuth); } + save(); } @Override - public boolean addUserAuth(UserAuth userAuth) throws IOException { + public void removeUserAuth(String key) throws IOException { synchronized (userAuthMap) { - userAuthMap.put(userAuth.getKey(), userAuth); - save(); - return true; + userAuthMap.remove(key); } + save(); } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java index dca812b..1ef3032 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -2,6 +2,7 @@ package com.ufund.api.ufundapi.persistence; import java.io.File; import java.io.IOException; +import java.util.HashMap; import java.util.Map; import java.util.TreeMap; @@ -21,7 +22,7 @@ public class UserFileDAO implements UserDAO { public UserFileDAO(@Value("${users.file}") String filename, ObjectMapper objectMapper) throws IOException { this.filename = filename; this.objectMapper = objectMapper; - users = new TreeMap<>(); + users = new HashMap<>(); load(); // load the users from the file } @@ -47,25 +48,14 @@ public class UserFileDAO implements UserDAO { * @throws IOException If there was an IO issue saving the file */ private boolean save() throws IOException { - User[] userArray = getUserArray(); - - objectMapper.writeValue(new File(filename), userArray); + objectMapper.writeValue(new File(filename), users.values()); return true; } - /** - * Return an array of the needs - * - * @return An array of all the needs - */ - private User[] getUserArray() { - return users.values().toArray(User[]::new); - } - @Override - public User[] getUsers() throws IOException { + public User[] getUsers() { synchronized (users) { - return getUserArray(); + return users.values().toArray(User[]::new); } } @@ -75,10 +65,9 @@ public class UserFileDAO implements UserDAO { * @param username Name of desired user * * @return Desired user, null otherwise - * @throws IOException If there was an IO issue saving the file */ @Override - public User getUser(String username) throws IOException { + public User getUser(String username) { synchronized (users) { return users.getOrDefault(username, null); } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index 2e644ee..ac86ff1 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -17,17 +17,29 @@ public class AuthService { this.userService = userService; } - public UserAuth getUserAuth(String key) { - return userAuthDAO.getUserAuth(key); - } - + /** + * Check if the provided key has access to the provided user. + * + * @param username The username of the user trying to be accessed. + * @param key The api key obtained by the client from logging in. + * @throws IllegalAccessException Thrown if access was denied to the user. + */ public void authenticate(String username, String key) throws IllegalAccessException { - var userAuth = getUserAuth(key); + var userAuth = userAuthDAO.getUserAuth(key); if (userAuth == null || !userAuth.getUsername().equals(username)) { throw new IllegalAccessException("Unauthorized"); } } + /** + * Attempt to log in with the provided credentials + * + * @param username The username of the user + * @param password The password of the user + * @return An API key if the authentication was successful. + * @throws IllegalAccessException Thrown if the username or password was incorrect + * @throws IOException If there was an issue saving the authentication + */ public String login(String username, String password) throws IllegalAccessException, IOException { var usr = userService.getUser(username); if (usr == null || !usr.verifyPassword(password)) { @@ -38,4 +50,14 @@ public class AuthService { return userAuth.getKey(); } + /** + * Logs out the current user + * + * @param key The API key to of the client + * @throws IOException Thrown if there was an error saving the authentication + */ + public void logout(String key) throws IOException { + userAuthDAO.removeUserAuth(key); + } + } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 860a2a8..6052e4f 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -5,17 +5,17 @@ import java.util.Arrays; import com.ufund.api.ufundapi.model.Need; import com.ufund.api.ufundapi.persistence.CupboardDAO; +import org.springframework.stereotype.Component; +@Component public class CupboardService { private final CupboardDAO cupboardDAO; public class DuplicateKeyException extends Exception { - public DuplicateKeyException(String message) { super(message); } - } public CupboardService(CupboardDAO cupboardDAO) { @@ -57,27 +57,34 @@ public class CupboardService { } /** - * @param id - * @return - * @throws IOException + * Gets a need with the specified ID + * + * @param id the ID of the need + * @return The resulting Need or null if the need was not found */ public Need getNeed(int id) throws IOException { return cupboardDAO.getNeed(id); } /** - * + * Modify a need + * * @param need * @return - * @throws IOException + * @throws IOException Thrown if there was an issue saving the changes */ public Need updateNeed(Need need) throws IOException { return cupboardDAO.updateNeed(need); } + /** + * Delete a need from the cupboard + * + * @param id the ID of the need + * @return True if the need was deleted + * @throws IOException Thrown on any problem removing the need + */ public boolean deleteNeed(int id) throws IOException { return cupboardDAO.deleteNeed(id); } - - } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java index a545029..6af3897 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java @@ -16,7 +16,7 @@ public class UserService { * * @param userDao The Data Access Object */ - public UserService(UserDAO userDao, AuthService authService) { + public UserService(UserDAO userDao) { this.userDAO = userDao; } -- cgit v1.2.3 From 953db99263178bcf122a415b50765fa283a8f42e Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 21:49:18 -0500 Subject: Removed unused import --- .../src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | 1 - 1 file changed, 1 deletion(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java index 1ef3032..9b206c8 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -4,7 +4,6 @@ import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.TreeMap; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -- cgit v1.2.3 From 7cb123c21bef247a2216545bc18245136f2ddf78 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 21:49:56 -0500 Subject: Added IOException throw to authenticate --- .../java/com/ufund/api/ufundapi/service/AuthService.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index ac86ff1..7e54cfb 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -1,10 +1,11 @@ package com.ufund.api.ufundapi.service; -import com.ufund.api.ufundapi.model.UserAuth; -import com.ufund.api.ufundapi.persistence.UserAuthDAO; +import java.io.IOException; + import org.springframework.stereotype.Component; -import java.io.IOException; +import com.ufund.api.ufundapi.model.UserAuth; +import com.ufund.api.ufundapi.persistence.UserAuthDAO; @Component public class AuthService { @@ -23,8 +24,9 @@ public class AuthService { * @param username The username of the user trying to be accessed. * @param key The api key obtained by the client from logging in. * @throws IllegalAccessException Thrown if access was denied to the user. - */ - public void authenticate(String username, String key) throws IllegalAccessException { + * @throws IOException + */ + public void authenticate(String username, String key) throws IllegalAccessException, IOException { var userAuth = userAuthDAO.getUserAuth(key); if (userAuth == null || !userAuth.getUsername().equals(username)) { throw new IllegalAccessException("Unauthorized"); -- cgit v1.2.3 From a3fbcd713ae9a6b3f38dcc42a5c4c2f369a5d6f5 Mon Sep 17 00:00:00 2001 From: sowgro Date: Thu, 6 Mar 2025 22:53:36 -0500 Subject: more javadocs and cleanup --- .../ufund/api/ufundapi/DuplicateKeyException.java | 7 ++++ .../ufundapi/controller/CupboardController.java | 4 +- .../api/ufundapi/controller/UserController.java | 11 ++--- .../api/ufundapi/persistence/UserAuthFIleDAO.java | 4 +- .../api/ufundapi/persistence/UserFileDAO.java | 32 --------------- .../ufund/api/ufundapi/service/AuthService.java | 12 +++--- .../api/ufundapi/service/CupboardService.java | 27 +++++++++---- .../ufund/api/ufundapi/service/UserService.java | 47 +++++++++++++++++----- 8 files changed, 76 insertions(+), 68 deletions(-) create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/DuplicateKeyException.java (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/DuplicateKeyException.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/DuplicateKeyException.java new file mode 100644 index 0000000..69ce6c0 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/DuplicateKeyException.java @@ -0,0 +1,7 @@ +package com.ufund.api.ufundapi; + +public class DuplicateKeyException extends Exception { + public DuplicateKeyException(String message) { + super(message); + } +} 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 dfcb8a3..15a741a 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 @@ -20,7 +20,7 @@ import org.springframework.web.bind.annotation.RestController; import com.ufund.api.ufundapi.model.Need; import com.ufund.api.ufundapi.model.Need.GoalType; import com.ufund.api.ufundapi.service.CupboardService; -import com.ufund.api.ufundapi.service.CupboardService.DuplicateKeyException; +import com.ufund.api.ufundapi.DuplicateKeyException; @RestController @RequestMapping("cupboard") @@ -50,7 +50,7 @@ public class CupboardController { public ResponseEntity createNeed(@RequestBody Map params) { String name = params.get("name"); int maxGoal = Integer.parseInt(params.get("maxGoal")); - Need.GoalType goalType = GoalType.valueOf(params.get("maxGoal")); + Need.GoalType goalType = GoalType.valueOf(params.get("goalType")); try { Need need = cupboardService.createNeed(name, maxGoal, goalType); 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 02526af..21cd1b3 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,6 +5,7 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import com.ufund.api.ufundapi.DuplicateKeyException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -20,12 +21,6 @@ public class UserController { private final UserService userService; private final AuthService authService; - /** - * Creates a UserController - * - * @param userService - * @param authService - */ public UserController(UserService userService, AuthService authService) { this.userService = userService; this.authService = authService; @@ -49,7 +44,8 @@ public class UserController { } else { return new ResponseEntity<>(HttpStatus.CONFLICT); } - + } catch (DuplicateKeyException ex) { + return new ResponseEntity<>(HttpStatus.CONFLICT); } catch (IOException ex) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } @@ -93,7 +89,6 @@ public class UserController { */ @PutMapping("/{username}") public ResponseEntity updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { - try { authService.authenticate(username, key); user = userService.updateUser(user, username); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java index 4494939..1fc1e92 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java @@ -59,15 +59,15 @@ public class UserAuthFIleDAO implements UserAuthDAO { public void addUserAuth(UserAuth userAuth) throws IOException { synchronized (userAuthMap) { userAuthMap.put(userAuth.getKey(), userAuth); + save(); } - save(); } @Override public void removeUserAuth(String key) throws IOException { synchronized (userAuthMap) { userAuthMap.remove(key); + save(); } - save(); } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java index 9b206c8..97bc378 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -58,13 +58,6 @@ public class UserFileDAO implements UserDAO { } } - /** - * Return the user with the String name name or null otherwise - * - * @param username Name of desired user - * - * @return Desired user, null otherwise - */ @Override public User getUser(String username) { synchronized (users) { @@ -72,14 +65,6 @@ public class UserFileDAO implements UserDAO { } } - /** - * Create a User user - * - * @param user User to create - * - * @return Desired created user - * @throws IOException If there was an IO issue saving the file - */ @Override public User addUser(User user) throws IOException { synchronized (users) { @@ -92,15 +77,6 @@ public class UserFileDAO implements UserDAO { } } - /** - * Update a user that matches the supplied name - * - * @param name The name of the user - * @param newUser New user data - * - * @return Desired user, null otherwise - * @throws IOException If there was an IO issue saving the file - */ @Override public User updateUser(User newUser, String name) throws IOException { synchronized (users) { @@ -114,14 +90,6 @@ public class UserFileDAO implements UserDAO { } } - /** - * Delete a user matching the name - * - * @param username The name of the user - * - * @return True if deleted, false otherwise - * @throws IOException If there was an IO issue saving the file - */ @Override public boolean deleteUser(String username) throws IOException { synchronized (users) { diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index 7e54cfb..591d891 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -1,11 +1,10 @@ package com.ufund.api.ufundapi.service; -import java.io.IOException; - -import org.springframework.stereotype.Component; - import com.ufund.api.ufundapi.model.UserAuth; import com.ufund.api.ufundapi.persistence.UserAuthDAO; +import org.springframework.stereotype.Component; + +import java.io.IOException; @Component public class AuthService { @@ -24,9 +23,8 @@ public class AuthService { * @param username The username of the user trying to be accessed. * @param key The api key obtained by the client from logging in. * @throws IllegalAccessException Thrown if access was denied to the user. - * @throws IOException - */ - public void authenticate(String username, String key) throws IllegalAccessException, IOException { + */ + public void authenticate(String username, String key) throws IllegalAccessException, IOException { var userAuth = userAuthDAO.getUserAuth(key); if (userAuth == null || !userAuth.getUsername().equals(username)) { throw new IllegalAccessException("Unauthorized"); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 6052e4f..15f8442 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -6,22 +6,27 @@ import java.util.Arrays; import com.ufund.api.ufundapi.model.Need; import com.ufund.api.ufundapi.persistence.CupboardDAO; import org.springframework.stereotype.Component; +import com.ufund.api.ufundapi.DuplicateKeyException; @Component public class CupboardService { private final CupboardDAO cupboardDAO; - public class DuplicateKeyException extends Exception { - public DuplicateKeyException(String message) { - super(message); - } - } - public CupboardService(CupboardDAO cupboardDAO) { this.cupboardDAO = cupboardDAO; } + /** + * Creates a new Need + * + * @param name The name of the need to create + * @param maxGoal The max goal of the new need + * @param goalType The goal type of the new need + * @return The need that was created + * @throws IOException Thrown if there was any issue saving the data + * @throws DuplicateKeyException If there already exists a need with the same name + */ public Need createNeed(String name, int maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { Need need = new Need(name, goalType, maxGoal); @@ -39,6 +44,12 @@ public class CupboardService { } + /** + * Get all the needs in the cupboard + * + * @return An array containing all needs + * @throws IOException Thrown if there was any issue saving the data + */ public Need[] getNeeds() throws IOException { return cupboardDAO.getNeeds(); } @@ -48,7 +59,7 @@ public class CupboardService { * * @param search The search substring * @return The requested array - * @throws IOException + * @throws IOException Thrown if there was any issue saving the data */ public Need[] searchNeeds(String search) throws IOException { return Arrays.stream(cupboardDAO.getNeeds()) @@ -68,7 +79,7 @@ public class CupboardService { /** * Modify a need - * + * // TODO * @param need * @return * @throws IOException Thrown if there was an issue saving the changes diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java index 6af3897..776d09a 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java @@ -2,6 +2,7 @@ package com.ufund.api.ufundapi.service; import java.io.IOException; +import com.ufund.api.ufundapi.DuplicateKeyException; import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.persistence.UserDAO; import org.springframework.stereotype.Component; @@ -11,29 +12,57 @@ public class UserService { private final UserDAO userDAO; - /** - * Create a user controller to receive REST signals - * - * @param userDao The Data Access Object - */ public UserService(UserDAO userDao) { this.userDAO = userDao; } - public User createUser(String username, String password) throws IOException { + /** + * Creates a new user + * + * @param username The username of the user + * @param password The password of the user + * @return The created user object + * @throws IOException Thrown on any problem saving the file + */ + public User createUser(String username, String password) throws IOException, DuplicateKeyException { + if (userDAO.getUser(username) != null) { + throw new DuplicateKeyException("A user with this name already exists"); + } User user = User.create(username, password); return userDAO.addUser(user); } - public User getUser(String username) throws IOException, IllegalAccessException { + /** + * Gets a user with the given username + * + * @param username The username of the user + * @return The user object with that username + * @throws IOException If there was any problem saving the file + */ + public User getUser(String username) throws IOException { return userDAO.getUser(username); } - public User updateUser(User user, String name) throws IllegalAccessException, IOException { + /** + * Updates a user + * // TODO + * @param user + * @param name + * @return + * @throws IOException Thrown if there was any issue saving the data + */ + public User updateUser(User user, String name) throws IOException { return userDAO.updateUser(user, name); } - public Boolean deleteUser(String username) throws IllegalAccessException, IOException { + /** + * Deletes a user + * + * @param username The username of the user to delete + * @return True if the user was deleted + * @throws IOException Thrown if there was any issue saving the data + */ + public boolean deleteUser(String username) throws IOException { return userDAO.deleteUser(username); } -- cgit v1.2.3 From 34903015992ac0cd7719b662af3ceb54a801351c Mon Sep 17 00:00:00 2001 From: sowgro Date: Fri, 7 Mar 2025 00:02:56 -0500 Subject: Finish update methods --- .../ufund/api/ufundapi/controller/CupboardController.java | 9 ++++++--- .../com/ufund/api/ufundapi/controller/UserController.java | 4 +++- .../java/com/ufund/api/ufundapi/persistence/UserDAO.java | 5 ++--- .../com/ufund/api/ufundapi/persistence/UserFileDAO.java | 8 ++++---- .../com/ufund/api/ufundapi/service/CupboardService.java | 14 +++++++++----- .../java/com/ufund/api/ufundapi/service/UserService.java | 15 +++++++++------ 6 files changed, 33 insertions(+), 22 deletions(-) (limited to 'ufund-api/src/main/java') 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 15a741a..7773028 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 @@ -1,6 +1,7 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +import java.security.InvalidParameterException; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -141,15 +142,17 @@ public class CupboardController { * @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) { + @PutMapping("/{id}") + public ResponseEntity updateNeed(@RequestBody Need need, @PathVariable int id) { try { - Need updatedNeed = cupboardService.updateNeed(need); + Need updatedNeed = cupboardService.updateNeed(need, id); if (updatedNeed != null) { return new ResponseEntity<>(need, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } + } catch (InvalidParameterException ex) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } 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 21cd1b3..0bb3fcf 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 @@ -1,6 +1,7 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +import java.security.InvalidParameterException; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -97,7 +98,8 @@ public class UserController { } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - + } catch (InvalidParameterException ex) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } catch (IllegalAccessException e) { diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java index 6558ce2..29d46cf 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java @@ -49,15 +49,14 @@ public interface UserDAO { /** * Updates and saves a {@linkplain User user} * - * @param newUser {@link User user} object to be updated and saved - * @param name {@link String name} name of object to be updated + * @param user {@link User user} object to be updated and saved * * @return updated {@link User user} if successful, null if * {@link User user} could not be found * * @throws IOException if underlying storage cannot be accessed */ - User updateUser(User newUser, String name) throws IOException; + User updateUser(User user) throws IOException; /** * Deletes a {@linkplain User user} with the given id diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java index 97bc378..f17f8f2 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -78,12 +78,12 @@ public class UserFileDAO implements UserDAO { } @Override - public User updateUser(User newUser, String name) throws IOException { + public User updateUser(User user) throws IOException { synchronized (users) { - if (users.containsKey(name)) { - users.put(name, newUser); + if (users.containsKey(user.getUsername())) { + users.put(user.getUsername(), user); save(); - return newUser; + return user; } else { return null; } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 15f8442..c8609ab 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -78,13 +78,17 @@ public class CupboardService { } /** - * Modify a need - * // TODO - * @param need - * @return + * Updates a need + * + * @param id The ID of the need to update + * @param need The need object to set (note: the ID is ignored) + * @return The updated need object * @throws IOException Thrown if there was an issue saving the changes */ - public Need updateNeed(Need need) throws IOException { + public Need updateNeed(Need need, int id) throws IOException { + if (need.getId() != id) { + throw new IllegalArgumentException("ID in URL and body must match"); + } return cupboardDAO.updateNeed(need); } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java index 776d09a..935ee72 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java @@ -45,14 +45,17 @@ public class UserService { /** * Updates a user - * // TODO - * @param user - * @param name - * @return + * + * @param user The ID of the user to update + * @param username The user object to set (note: the ID is ignored) + * @return The updated user object * @throws IOException Thrown if there was any issue saving the data */ - public User updateUser(User user, String name) throws IOException { - return userDAO.updateUser(user, name); + public User updateUser(User user, String username) throws IOException { + if (!user.getUsername().equals(username)) { + throw new IllegalArgumentException("ID in URL and body must match"); + } + return userDAO.updateUser(user); } /** -- cgit v1.2.3 From 183d4b047f69c1f6daed8e6ee8eb257a52d97e32 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 13 Mar 2025 16:54:21 -0400 Subject: Updated imports --- .../com/ufund/api/ufundapi/controller/AuthController.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java index b0390ae..b46d4ee 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java @@ -1,13 +1,18 @@ package com.ufund.api.ufundapi.controller; -import com.ufund.api.ufundapi.service.AuthService; -import com.ufund.api.ufundapi.service.UserService; +import java.io.IOException; +import java.util.Map; + import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; -import java.io.IOException; -import java.util.Map; +import com.ufund.api.ufundapi.service.AuthService; @RestController @RequestMapping("auth") -- cgit v1.2.3 From bae0f05fb971b7ec99f4279743e602a418553e45 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 13 Mar 2025 17:44:26 -0400 Subject: Updated docstrings --- .../api/ufundapi/controller/UserController.java | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'ufund-api/src/main/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 0bb3fcf..795ca13 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 @@ -6,11 +6,19 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import com.ufund.api.ufundapi.DuplicateKeyException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +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.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import com.ufund.api.ufundapi.DuplicateKeyException; import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.service.AuthService; import com.ufund.api.ufundapi.service.UserService; @@ -29,7 +37,7 @@ public class UserController { /** * Creates a User with the provided object - * + * @param params A map consisting of the parameters for a user * @return OK response and the user if it was successful, INTERNAL_SERVER_ERROR * otherwise */ @@ -55,6 +63,8 @@ public class UserController { /** * Responds to the GET request for a {@linkplain User user} for the given id * + * @param username The name of the user + * @param key The authentication key of the user * @return ResponseEntity with {@link User user} object and HTTP status of OK if * found
* ResponseEntity with HTTP status of NOT_FOUND if not found
@@ -84,7 +94,9 @@ public class UserController { /** * Updates a User with the provided one * - * @param user The user to update + * @param user The user to update + * @param username The name of the user + * @param key The authentication key of the user * @return OK response and the user if it was successful, or * INTERNAL_SERVER_ERROR if there was an issue */ @@ -111,6 +123,7 @@ public class UserController { * Deletes a user with the desired name * * @param username The name of the user + * @param key The authentication key 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 */ -- cgit v1.2.3 From 4caaeec30f8732658dbe9ad053253d5cb483efca Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 13 Mar 2025 20:38:41 -0400 Subject: Updated tests --- .../src/main/java/com/ufund/api/ufundapi/controller/UserController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ufund-api/src/main/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 795ca13..adf17a1 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 @@ -48,7 +48,7 @@ public class UserController { try { User user = userService.createUser(username, password); - if (user == null) { + if (user != null) { return new ResponseEntity<>(user, HttpStatus.CREATED); } else { return new ResponseEntity<>(HttpStatus.CONFLICT); -- cgit v1.2.3 From 4ac7711c4d9dd3275ae4037f843347e4fbcb1f2a Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sat, 15 Mar 2025 23:18:54 -0400 Subject: Added additional check to createNeed method --- .../com/ufund/api/ufundapi/service/CupboardService.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index c8609ab..6dd120c 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -3,10 +3,11 @@ package com.ufund.api.ufundapi.service; import java.io.IOException; import java.util.Arrays; -import com.ufund.api.ufundapi.model.Need; -import com.ufund.api.ufundapi.persistence.CupboardDAO; import org.springframework.stereotype.Component; + import com.ufund.api.ufundapi.DuplicateKeyException; +import com.ufund.api.ufundapi.model.Need; +import com.ufund.api.ufundapi.persistence.CupboardDAO; @Component public class CupboardService { @@ -27,16 +28,18 @@ public class CupboardService { * @throws IOException Thrown if there was any issue saving the data * @throws DuplicateKeyException If there already exists a need with the same name */ - public Need createNeed(String name, int maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { + public Need createNeed(String name, double maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { Need need = new Need(name, goalType, maxGoal); if (need.getMaxGoal() <= 0) { throw new IllegalArgumentException("Max Goal must be greater than zero"); } else { - for (Need searchNeed : cupboardDAO.getNeeds()) { - if (need.getName().equalsIgnoreCase(searchNeed.getName())) { - throw new DuplicateKeyException("Duplicate names are not allowed"); + if (cupboardDAO.getNeeds().length > 0) { + for (Need searchNeed : cupboardDAO.getNeeds()) { + if (need.getName().equalsIgnoreCase(searchNeed.getName())) { + throw new DuplicateKeyException("Duplicate names are not allowed"); + } } } return cupboardDAO.addNeed(need); -- cgit v1.2.3 From a3150b8a8e17c8a71f617745bb8588b397a75f47 Mon Sep 17 00:00:00 2001 From: sowgro Date: Sat, 15 Mar 2025 23:52:58 -0400 Subject: fix testCreateNeed() --- .../api/ufundapi/service/CupboardService.java | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 6dd120c..78f8f85 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -29,22 +29,20 @@ public class CupboardService { * @throws DuplicateKeyException If there already exists a need with the same name */ public Need createNeed(String name, double maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { - - Need need = new Need(name, goalType, maxGoal); - if (need.getMaxGoal() <= 0) { + if (maxGoal <= 0) { throw new IllegalArgumentException("Max Goal must be greater than zero"); - } else { - if (cupboardDAO.getNeeds().length > 0) { - for (Need searchNeed : cupboardDAO.getNeeds()) { - if (need.getName().equalsIgnoreCase(searchNeed.getName())) { - throw new DuplicateKeyException("Duplicate names are not allowed"); - } - } + } + + for (Need searchNeed : cupboardDAO.getNeeds()) { + if (searchNeed.getName().equalsIgnoreCase(name)) { + throw new DuplicateKeyException("Duplicate names are not allowed"); } - return cupboardDAO.addNeed(need); } - + + Need need = new Need(name, goalType, maxGoal); + return cupboardDAO.addNeed(need); + } /** -- cgit v1.2.3 From 251f30c402700169213ed4560a7797a785a50e78 Mon Sep 17 00:00:00 2001 From: sowgro Date: Mon, 17 Mar 2025 16:08:11 -0400 Subject: Refactoring --- .../java/com/ufund/api/ufundapi/model/User.java | 35 +++++++++++----------- .../api/ufundapi/persistence/CupboardFileDAO.java | 2 +- .../ufund/api/ufundapi/service/AuthService.java | 13 ++++++-- 3 files changed, 28 insertions(+), 22 deletions(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java index 1e182a6..61293b9 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java @@ -7,40 +7,35 @@ import com.fasterxml.jackson.annotation.JsonProperty; public class User { - @JsonProperty("username") - private final String username; - @JsonProperty("passwordHash") - private int passwordHash; - @JsonProperty("basket") - private final List basket; - - /** - * Create a new user - * - * @param username The name of the user - */ - public User(String username) { - this.username = username; - basket = new ArrayList<>(); + public enum UserType { + HELPER, + MANAGER } + @JsonProperty("username") private final String username; + @JsonProperty("passwordHash") private int passwordHash; + @JsonProperty("basket") private final List basket; + @JsonProperty("type") private final UserType type; + /** * Create a new user * * @param username The name of the user * @param basket A basket to copy from */ - public User(@JsonProperty("username") String username, @JsonProperty("passwordHash") int passwordHash, @JsonProperty("basket") List basket) { + public User(@JsonProperty("username") String username, @JsonProperty("passwordHash") int passwordHash, @JsonProperty("basket") List basket, @JsonProperty("type") UserType userType) { this.username = username; this.basket = basket; this.passwordHash = passwordHash; + this.type = userType; } public static User create(String username, String password) { return new User( username, password.hashCode(), - new ArrayList<>() + new ArrayList<>(), + UserType.HELPER ); } @@ -65,7 +60,11 @@ public class User { } public User withoutPasswordHash() { - return new User(this.username, 0, this.basket); + return new User(this.username, 0, this.basket, this.type); + } + + public UserType getType() { + return type; } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java index c4aaca3..521acae 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java @@ -22,7 +22,7 @@ public class CupboardFileDAO implements CupboardDAO { this.filename = filename; this.objectMapper = objectMapper; needs = new TreeMap<>(); - load(); // load the heroes from the file + load(); } private synchronized static int nextId() { diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index 591d891..5a1a492 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -1,5 +1,6 @@ package com.ufund.api.ufundapi.service; +import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.model.UserAuth; import com.ufund.api.ufundapi.persistence.UserAuthDAO; import org.springframework.stereotype.Component; @@ -20,13 +21,19 @@ public class AuthService { /** * Check if the provided key has access to the provided user. * - * @param username The username of the user trying to be accessed. + * @param targetUsername The targetUsername of the user trying to be accessed. * @param key The api key obtained by the client from logging in. * @throws IllegalAccessException Thrown if access was denied to the user. */ - public void authenticate(String username, String key) throws IllegalAccessException, IOException { + public void authenticate(String targetUsername, String key) throws IllegalAccessException, IOException { var userAuth = userAuthDAO.getUserAuth(key); - if (userAuth == null || !userAuth.getUsername().equals(username)) { + if (userAuth == null) { + throw new IllegalAccessException("Unauthenticated"); + } + + var username = userAuth.getUsername(); + var userType = userService.getUser(username).getType(); + if (!username.equals(targetUsername) && userType != User.UserType.MANAGER) { throw new IllegalAccessException("Unauthorized"); } } -- cgit v1.2.3 From 8a5f74d67551ac295c37be2ef8dd41b780a73b16 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 17 Mar 2025 18:08:13 -0400 Subject: Refactored user to instead hold a list of ID's instead of needs --- .../src/main/java/com/ufund/api/ufundapi/model/User.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java index 61293b9..f08f9f0 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java @@ -14,7 +14,7 @@ public class User { @JsonProperty("username") private final String username; @JsonProperty("passwordHash") private int passwordHash; - @JsonProperty("basket") private final List basket; + @JsonProperty("basket") private final List basket; @JsonProperty("type") private final UserType type; /** @@ -23,7 +23,7 @@ public class User { * @param username The name of the user * @param basket A basket to copy from */ - public User(@JsonProperty("username") String username, @JsonProperty("passwordHash") int passwordHash, @JsonProperty("basket") List basket, @JsonProperty("type") UserType userType) { + public User(@JsonProperty("username") String username, @JsonProperty("passwordHash") int passwordHash, @JsonProperty("basket") List basket, @JsonProperty("type") UserType userType) { this.username = username; this.basket = basket; this.passwordHash = passwordHash; @@ -48,15 +48,15 @@ public class User { } public void addToBasket(Need need) { - basket.add(need); + basket.add(need.getId()); } - public Need[] getBasketNeeds() { - return basket.toArray(Need[]::new); + public Integer[] getBasketNeeds() { + return basket.toArray(Integer[]::new); } public void removeBasketNeed(Need need) { - basket.remove(need); + basket.remove(need.getId()); } public User withoutPasswordHash() { -- cgit v1.2.3 From 674b158932394d3cad8bce8dedca49b1efdfd453 Mon Sep 17 00:00:00 2001 From: sowgro Date: Mon, 17 Mar 2025 21:17:06 -0400 Subject: Attempt at fixing connection to front end --- .../api/ufundapi/controller/CupboardController.java | 13 ++++++++----- .../com/ufund/api/ufundapi/service/AuthService.java | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) (limited to 'ufund-api/src/main/java') 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 7773028..4bad4b9 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 @@ -48,10 +48,11 @@ public class CupboardController { * INTERNAL_SERVER_ERROR otherwise */ @PostMapping("") - public ResponseEntity createNeed(@RequestBody Map params) { - String name = params.get("name"); - int maxGoal = Integer.parseInt(params.get("maxGoal")); - Need.GoalType goalType = GoalType.valueOf(params.get("goalType")); + public ResponseEntity createNeed(@RequestBody Map params) { + System.out.println(params); + String name = (String) params.get("name"); + int maxGoal = (int) params.get("maxGoal"); + Need.GoalType goalType = GoalType.valueOf((String) params.get("goalType")); try { Need need = cupboardService.createNeed(name, maxGoal, goalType); @@ -152,8 +153,10 @@ public class CupboardController { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (InvalidParameterException ex) { + ex.printStackTrace(); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } catch (IOException e) { + } catch (IOException ex) { + ex.printStackTrace(); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index 5a1a492..c847cac 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -26,16 +26,16 @@ public class AuthService { * @throws IllegalAccessException Thrown if access was denied to the user. */ public void authenticate(String targetUsername, String key) throws IllegalAccessException, IOException { - var userAuth = userAuthDAO.getUserAuth(key); - if (userAuth == null) { - throw new IllegalAccessException("Unauthenticated"); - } - - var username = userAuth.getUsername(); - var userType = userService.getUser(username).getType(); - if (!username.equals(targetUsername) && userType != User.UserType.MANAGER) { - throw new IllegalAccessException("Unauthorized"); - } +// var userAuth = userAuthDAO.getUserAuth(key); +// if (userAuth == null) { +// throw new IllegalAccessException("Unauthenticated"); +// } +// +// var username = userAuth.getUsername(); +// var userType = userService.getUser(username).getType(); +// if (!username.equals(targetUsername) && userType != User.UserType.MANAGER) { +// throw new IllegalAccessException("Unauthorized"); +// } } /** -- cgit v1.2.3 From 54876363de44791ba65b6c43b795f8d0c3548ecc Mon Sep 17 00:00:00 2001 From: sowgro Date: Mon, 17 Mar 2025 21:45:31 -0400 Subject: Fix tests --- .../com/ufund/api/ufundapi/controller/CupboardController.java | 2 +- .../src/main/java/com/ufund/api/ufundapi/service/AuthService.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'ufund-api/src/main/java') 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 4bad4b9..bffc9ec 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 @@ -52,7 +52,7 @@ public class CupboardController { System.out.println(params); String name = (String) params.get("name"); int maxGoal = (int) params.get("maxGoal"); - Need.GoalType goalType = GoalType.valueOf((String) params.get("goalType")); + Need.GoalType goalType = GoalType.valueOf((String) params.get("type")); try { Need need = cupboardService.createNeed(name, maxGoal, goalType); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index c847cac..87a16a6 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -26,10 +26,10 @@ public class AuthService { * @throws IllegalAccessException Thrown if access was denied to the user. */ public void authenticate(String targetUsername, String key) throws IllegalAccessException, IOException { -// var userAuth = userAuthDAO.getUserAuth(key); -// if (userAuth == null) { -// throw new IllegalAccessException("Unauthenticated"); -// } + var userAuth = userAuthDAO.getUserAuth(key); + if (userAuth == null) { + throw new IllegalAccessException("Unauthenticated"); + } // // var username = userAuth.getUsername(); // var userType = userService.getUser(username).getType(); -- cgit v1.2.3 From d4c0487021b75d94cbb76dcb5c97c344468ba9e5 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 17 Mar 2025 22:20:02 -0400 Subject: Added check to update to check for less than 1 values --- .../src/main/java/com/ufund/api/ufundapi/service/CupboardService.java | 3 +++ 1 file changed, 3 insertions(+) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 78f8f85..2398745 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -90,6 +90,9 @@ public class CupboardService { if (need.getId() != id) { throw new IllegalArgumentException("ID in URL and body must match"); } + if (need.getMaxGoal() <= 0) { + throw new IllegalArgumentException("Goal must be greater than 0"); + } return cupboardDAO.updateNeed(need); } -- cgit v1.2.3 From e04bfc401fdbcdb892bd6d08f56004b139d69078 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 17 Mar 2025 22:45:39 -0400 Subject: Changed catch to use illegal argument exception --- .../java/com/ufund/api/ufundapi/controller/CupboardController.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'ufund-api/src/main/java') 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 bffc9ec..9592490 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 @@ -1,7 +1,6 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; -import java.security.InvalidParameterException; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -152,7 +151,7 @@ public class CupboardController { } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - } catch (InvalidParameterException ex) { + } catch (IllegalArgumentException ex) { ex.printStackTrace(); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IOException ex) { -- cgit v1.2.3 From b5797b53eddf5a52ea9bbd8f3c638edd678407ab Mon Sep 17 00:00:00 2001 From: Akash Keshav <112591754+domesticchores@users.noreply.github.com> Date: Mon, 17 Mar 2025 22:56:19 -0400 Subject: please work, i backmerged and everything. -ak --- ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java index 9ca097a..c0e9214 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java @@ -14,7 +14,7 @@ public class Need { @JsonProperty("filterAttributes") private String[] filterAttributes; @JsonProperty("type") final private GoalType type; @JsonProperty("maxGoal") private double maxGoal; - @JsonProperty("Current") private double current; + @JsonProperty("current") private double current; /** * Create a new need -- cgit v1.2.3