diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-03-17 17:17:06 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-03-17 17:17:06 -0400 |
commit | baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754 (patch) | |
tree | e9213224b8f1b35b860f016a6a3d1318def8aae2 /ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | |
parent | bf33fa3ca9f29b1e75cc077ae2eaaf4f5725e4b3 (diff) | |
parent | d737551fba5617843f3014be6994490dd4328183 (diff) | |
download | JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.tar.gz JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.tar.bz2 JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.zip |
Merge remote-tracking branch 'origin/main' into cupboard-component
# Conflicts:
# ufund-api/data/cupboard.json
# ufund-ui/src/app/app.module.ts
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 | 88 |
1 files changed, 21 insertions, 67 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..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 @@ -2,8 +2,8 @@ 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; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -21,7 +21,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 } @@ -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); } } @@ -47,100 +47,54 @@ 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); } } - /** - * Return the user with the String name name or null otherwise - * - * @param name 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) { synchronized (users) { - return users.getOrDefault(name, null); + return users.getOrDefault(username, null); } } - /** - * 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 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(); + if (res == null) { + return user; } + return res; } } - /** - * 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 { + 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; } } } - /** - * Delete a user matching the name - * - * @param name 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; |