From c02c47efcb00782feb1461534923023a711d4f15 Mon Sep 17 00:00:00 2001
From: sowgro <tpoke.ferrari@gmail.com>
Date: Sun, 2 Mar 2025 11:22:48 -0500
Subject: First attempt at an authentication system.

---
 .../api/ufundapi/persistence/UserAuthFIleDAO.java  | 62 ++++++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java

(limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java')

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..67918cc
--- /dev/null
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java
@@ -0,0 +1,62 @@
+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();
+    }
+
+    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);
+        }
+    }
+
+    private void save() throws IOException {
+        objectMapper.writeValue(new File(filename), userAuthMap.values());
+    }
+
+    public UserAuth[] getAuthKeys() {
+        synchronized (userAuthMap) {
+            return userAuthMap.values().toArray(UserAuth[]::new);
+        }
+    }
+
+    @Override
+    public UserAuth getUserAuth(String key) {
+        synchronized (userAuthMap) {
+            return userAuthMap.get(key);
+        }
+    }
+
+    @Override
+    public boolean addUserAuth(UserAuth userAuth) throws IOException {
+        synchronized (userAuthMap) {
+            userAuthMap.put(userAuth.getKey(), userAuth);
+            save();
+            return true;
+        }
+    }
+}
-- 
cgit v1.2.3


From bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3 Mon Sep 17 00:00:00 2001
From: sowgro <tpoke.ferrari@gmail.com>
Date: Thu, 6 Mar 2025 21:41:39 -0500
Subject: Push current changes (working on documentation and tests)

---
 .../api/ufundapi/persistence/UserAuthFIleDAO.java  | 27 +++++++++++++++-------
 1 file changed, 19 insertions(+), 8 deletions(-)

(limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java')

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();
     }
 }
-- 
cgit v1.2.3


From a3fbcd713ae9a6b3f38dcc42a5c4c2f369a5d6f5 Mon Sep 17 00:00:00 2001
From: sowgro <tpoke.ferrari@gmail.com>
Date: Thu, 6 Mar 2025 22:53:36 -0500
Subject: more javadocs and cleanup

---
 .../main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

(limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java')

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 4494939..1fc1e92 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
@@ -59,15 +59,15 @@ public class UserAuthFIleDAO implements UserAuthDAO {
     public void addUserAuth(UserAuth userAuth) throws IOException {
         synchronized (userAuthMap) {
             userAuthMap.put(userAuth.getKey(), userAuth);
+            save();
         }
-        save();
     }
 
     @Override
     public void removeUserAuth(String key) throws IOException {
         synchronized (userAuthMap) {
             userAuthMap.remove(key);
+            save();
         }
-        save();
     }
 }
-- 
cgit v1.2.3