diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-03-06 21:41:39 -0500 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-03-06 21:41:39 -0500 |
commit | bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3 (patch) | |
tree | 7df43079fa8d3e5c7b34c5ab13389018c1216078 /ufund-api/src/main/java/com/ufund/api/ufundapi/persistence | |
parent | eb4edcc7e7e4f9a6a59bed6d3952486f179fc445 (diff) | |
download | JellySolutions-bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3.tar.gz JellySolutions-bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3.tar.bz2 JellySolutions-bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3.zip |
Push current changes (working on documentation and tests)
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence')
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java | 4 | ||||
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java (renamed from ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java) | 4 | ||||
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java | 17 | ||||
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java | 27 | ||||
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | 23 |
5 files changed, 40 insertions, 35 deletions
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 * <br> * 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 index 84ea693..c4aaca3 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 @@ -11,14 +11,14 @@ import java.util.Map; import java.util.TreeMap; @Component -public class CupboardFileDao implements CupboardDAO { +public class CupboardFileDAO implements CupboardDAO { private final Map<Integer, Need> 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 { + public CupboardFileDAO(@Value("${cupboard.file}") String filename, ObjectMapper objectMapper) throws IOException { this.filename = filename; this.objectMapper = objectMapper; needs = new TreeMap<>(); 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); } |