aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence
diff options
context:
space:
mode:
authorGunther6070 <haydenhartman10@yahoo.com>2025-03-17 23:12:49 -0400
committerGunther6070 <haydenhartman10@yahoo.com>2025-03-17 23:12:49 -0400
commit5b6c20479fbb6ed0cabbbc88b42280c5a7dbd22c (patch)
tree16f93b3ce3b40de1e550f3824f60ca3aac632265 /ufund-api/src/main/java/com/ufund/api/ufundapi/persistence
parent7a5c5073e9e410b3ccc3ab7902a0d6f558277c7d (diff)
parent2b847078b7af4ade35461b8af52352bc88994be3 (diff)
downloadJellySolutions-5b6c20479fbb6ed0cabbbc88b42280c5a7dbd22c.tar.gz
JellySolutions-5b6c20479fbb6ed0cabbbc88b42280c5a7dbd22c.tar.bz2
JellySolutions-5b6c20479fbb6ed0cabbbc88b42280c5a7dbd22c.zip
Merge branch 'main' into funding_basket
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.java21
-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)27
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java32
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java73
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java21
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java88
6 files changed, 143 insertions, 119 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 1435410..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
@@ -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
*
@@ -14,23 +14,10 @@ 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;
/**
- * 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
*
* @param id The ID of the {@link Need need} to get
@@ -38,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;
@@ -54,7 +39,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..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
@@ -11,18 +11,18 @@ 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<>();
- load(); // load the heroes from the file
+ load();
}
private synchronized static int nextId() {
@@ -61,18 +61,6 @@ public class CupboardFileDao implements CupboardDAO {
}
/**
- * 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
*
* @return True if the save was successful, false otherwise
@@ -93,13 +81,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) {
return needs.getOrDefault(id, null);
@@ -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/persistence/UserAuthDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java
new file mode 100644
index 0000000..355aae4
--- /dev/null
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthDAO.java
@@ -0,0 +1,32 @@
+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) throws IOException;
+
+ /**
+ * Add a user authentication profile
+ *
+ * @param userAuth The user auth profile to add
+ * @throws IOException Thrown on any file writing error
+ */
+ 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
new file mode 100644
index 0000000..1fc1e92
--- /dev/null
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java
@@ -0,0 +1,73 @@
+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<String, UserAuth> 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();
+ }
+
+ /**
+ * 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();
+
+ UserAuth[] userAuthKeysArray = objectMapper.readValue(new File(filename), UserAuth[].class);
+
+ for (UserAuth userAuth : userAuthKeysArray) {
+ userAuthMap.put(userAuth.getKey(), userAuth);
+ }
+ }
+
+ /**
+ * 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());
+ }
+
+ @Override
+ public UserAuth getUserAuth(String key) {
+ synchronized (userAuthMap) {
+ return userAuthMap.get(key);
+ }
+ }
+
+ @Override
+ public void addUserAuth(UserAuth userAuth) throws IOException {
+ synchronized (userAuthMap) {
+ userAuthMap.put(userAuth.getKey(), userAuth);
+ save();
+ }
+ }
+
+ @Override
+ public void removeUserAuth(String key) throws IOException {
+ synchronized (userAuthMap) {
+ userAuthMap.remove(key);
+ save();
+ }
+ }
+}
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..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
@@ -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
* <br>
- * 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,25 +44,24 @@ 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}
*
- * @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
*
- * @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
* <br>
@@ -70,5 +69,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..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;