aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com
diff options
context:
space:
mode:
authorGunther6070 <haydenhartman10@yahoo.com>2025-02-26 12:37:07 -0500
committerGunther6070 <haydenhartman10@yahoo.com>2025-02-26 12:37:07 -0500
commita52c511db3743a9be027672b353a16651b37a2bb (patch)
treebc332a01f62b8309051d7ea86293d852a6f87dde /ufund-api/src/main/java/com
parentd775671c878a15a138d16efd70138e3a4fbec7d7 (diff)
downloadJellySolutions-a52c511db3743a9be027672b353a16651b37a2bb.tar.gz
JellySolutions-a52c511db3743a9be027672b353a16651b37a2bb.tar.bz2
JellySolutions-a52c511db3743a9be027672b353a16651b37a2bb.zip
Implemented CRUD operations and helper methods
Diffstat (limited to 'ufund-api/src/main/java/com')
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java126
1 files changed, 56 insertions, 70 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 87b8103..c42d211 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
@@ -9,27 +9,20 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.ufund.api.ufundapi.model.Need;
+import com.ufund.api.ufundapi.model.User;
@Component
public class UserFileDAO implements UserDAO {
- private final Map<Integer, Need> needs; // cache
+ private final Map<String, User> users; // cache
private final ObjectMapper objectMapper;
- private static int nextId;
private final String filename;
public UserFileDAO(@Value("${users.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;
+ users = new TreeMap<>();
+ load(); // load the users from the file
}
/**
@@ -38,39 +31,13 @@ public class UserFileDAO implements UserDAO {
* @throws IOException Any IO issue with the file
*/
private void load() throws IOException {
- needs.clear();
- nextId = 0;
+ users.clear();
- Need[] needsArray = objectMapper.readValue(new File(filename), Need[].class);
+ User[] usersArray = objectMapper.readValue(new File(filename), User[].class);
- for (Need need : needsArray) {
- needs.put(need.getId(), need);
- if (need.getId() > nextId()) {
- nextId = need.getId();
- }
+ for (User user : usersArray) {
+ users.put(user.getName(), user);
}
- nextId++;
- }
-
- /**
- * Return an array of the needs
- *
- * @return An array of all the needs
- */
- private Need[] getNeedsArray() {
- 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);
}
/**
@@ -80,67 +47,86 @@ public class UserFileDAO implements UserDAO {
* @throws IOException If there was an IO issue saving the file
*/
private boolean save() throws IOException {
- Need[] needArray = getNeedsArray();
+ User[] userArray = getUserArray();
- objectMapper.writeValue(new File(filename), needArray);
+ objectMapper.writeValue(new File(filename), userArray);
return true;
}
- @Override
- public Need[] getNeeds() {
- synchronized (needs) {
- return getNeedsArray();
- }
+ /**
+ * Return an array of the needs
+ *
+ * @return An array of all the needs
+ */
+ private User[] getUserArray() {
+ return users.values().toArray(User[]::new);
}
+
@Override
- public Need[] findNeeds(String targetName) {
- synchronized (needs) {
- return getNeedsArray(targetName);
+ public User[] getUsers() throws IOException {
+ synchronized (users) {
+ return getUserArray();
}
}
+ /**
+ * 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 Need getNeed(int id) {
- synchronized (needs) {
- return needs.getOrDefault(id, null);
+ public User getUser(String name) throws IOException {
+ synchronized (users) {
+ return users.getOrDefault(name, null);
}
}
+ /**
+ * 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 Need createNeed(Need need) throws IOException {
- synchronized (needs) {
- Need newNeed = new Need(need);
- newNeed.setID(nextId());
- needs.put(newNeed.getId(), newNeed);
+ public User createUser(User user) throws IOException {
+ synchronized (users) {
+ User newUser = new User(user);
+ users.put(newUser.getName(), newUser);
save();
- return newNeed;
+ return newUser;
}
}
@Override
- public Need updateNeed(Need need) throws IOException {
- synchronized (needs) {
- if (needs.containsKey(need.getId())) {
- needs.put(need.getId(), need);
+ public User updateUser(User user) throws IOException {
+ synchronized (users) {
+ if (users.containsKey(user.getName())) {
+ users.put(user.getName(), user);
save();
- return need;
+ return user;
} else {
return null;
}
-
}
}
@Override
- public boolean deleteNeed(int id) throws IOException {
- synchronized (needs) {
- if (needs.containsKey(id)) {
- needs.remove(id);
+ public boolean deleteUser(String name) throws IOException {
+ synchronized (users) {
+ if (users.containsKey(name)) {
+ users.remove(name);
return save();
} else {
return false;
}
}
}
+
+
} \ No newline at end of file