diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-03-02 11:22:48 -0500 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-03-02 11:22:48 -0500 |
commit | c02c47efcb00782feb1461534923023a711d4f15 (patch) | |
tree | 8c59e17bc6039d76d0b9522e2535a49a33b3d340 /ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | |
parent | 8e93fe31c81c4c36e66c48e7efcdbfedb1877385 (diff) | |
download | JellySolutions-c02c47efcb00782feb1461534923023a711d4f15.tar.gz JellySolutions-c02c47efcb00782feb1461534923023a711d4f15.tar.bz2 JellySolutions-c02c47efcb00782feb1461534923023a711d4f15.zip |
First attempt at an authentication system.
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java')
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | 29 |
1 files changed, 12 insertions, 17 deletions
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; |