From c7ac2db02a39cf349f9434186996c93eebc17a3c Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sun, 23 Feb 2025 15:51:28 -0500 Subject: Created UserDAO and file as well as user class --- .../api/ufundapi/persistence/UserFileDAO.java | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java') 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 new file mode 100644 index 0000000..87b8103 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -0,0 +1,146 @@ +package com.ufund.api.ufundapi.persistence; + +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.TreeMap; + +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; + +@Component +public class UserFileDAO implements UserDAO { + + private final Map needs; // 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; + } + + /** + * Load changes from the json file + * + * @throws IOException Any IO issue with the file + */ + private void load() throws IOException { + needs.clear(); + nextId = 0; + + Need[] needsArray = objectMapper.readValue(new File(filename), Need[].class); + + for (Need need : needsArray) { + needs.put(need.getId(), need); + if (need.getId() > nextId()) { + nextId = need.getId(); + } + } + 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); + } + + /** + * Saves the needs to json + * + * @return True if the save was successful, false otherwise + * @throws IOException If there was an IO issue saving the file + */ + private boolean save() throws IOException { + Need[] needArray = getNeedsArray(); + + objectMapper.writeValue(new File(filename), needArray); + return true; + } + + @Override + public Need[] getNeeds() { + synchronized (needs) { + return getNeedsArray(); + } + } + + @Override + public Need[] findNeeds(String targetName) { + synchronized (needs) { + return getNeedsArray(targetName); + } + } + + @Override + public Need getNeed(int id) { + synchronized (needs) { + return needs.getOrDefault(id, null); + } + } + + @Override + public Need createNeed(Need need) throws IOException { + synchronized (needs) { + Need newNeed = new Need(need); + newNeed.setID(nextId()); + needs.put(newNeed.getId(), newNeed); + save(); + return newNeed; + } + } + + @Override + public Need updateNeed(Need need) throws IOException { + synchronized (needs) { + if (needs.containsKey(need.getId())) { + needs.put(need.getId(), need); + save(); + return need; + } else { + return null; + } + + } + } + + @Override + public boolean deleteNeed(int id) throws IOException { + synchronized (needs) { + if (needs.containsKey(id)) { + needs.remove(id); + return save(); + } else { + return false; + } + } + } +} \ No newline at end of file -- cgit v1.2.3 From a52c511db3743a9be027672b353a16651b37a2bb Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Wed, 26 Feb 2025 12:37:07 -0500 Subject: Implemented CRUD operations and helper methods --- .../api/ufundapi/persistence/UserFileDAO.java | 126 +++++++++------------ 1 file changed, 56 insertions(+), 70 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java') 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 needs; // cache + private final Map 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 -- cgit v1.2.3 From cbcd49f2b96dffe359b99b8791ccfb62cbd4717f Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Wed, 26 Feb 2025 13:18:18 -0500 Subject: Updated and added more javadocs. Also modified updateUser method to take a string name to use to find desired user to update. --- .../api/ufundapi/persistence/UserFileDAO.java | 33 ++++++++++++++++------ 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java') 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 c42d211..ebfa9f2 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 @@ -62,7 +62,6 @@ public class UserFileDAO implements UserDAO { return users.values().toArray(User[]::new); } - @Override public User[] getUsers() throws IOException { synchronized (users) { @@ -86,11 +85,11 @@ public class UserFileDAO implements UserDAO { } /** - * Return the user with the String name name or null otherwise + * Create a User user * - * @param name Name of desired user + * @param user User to create * - * @return Desired user, null otherwise + * @return Desired created user * @throws IOException If there was an IO issue saving the file */ @Override @@ -103,19 +102,36 @@ public class UserFileDAO implements UserDAO { } } + /** + * 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 user) throws IOException { + public User updateUser(User newUser, String name) throws IOException { synchronized (users) { - if (users.containsKey(user.getName())) { - users.put(user.getName(), user); + if (users.containsKey(name)) { + users.put(name, newUser); save(); - return user; + return newUser; } 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 { synchronized (users) { @@ -128,5 +144,4 @@ public class UserFileDAO implements UserDAO { } } - } \ No newline at end of file -- cgit v1.2.3 From 0d1c11aa2738a622fc2ee6ecb23aef214c9520db Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Wed, 26 Feb 2025 13:33:03 -0500 Subject: Implemented createUser in the controller and modified logic in the UserFileDAO to check for conflict --- .../java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java') 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 ebfa9f2..0f30824 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 @@ -95,10 +95,14 @@ public class UserFileDAO implements UserDAO { @Override public User createUser(User user) throws IOException { synchronized (users) { - User newUser = new User(user); - users.put(newUser.getName(), newUser); - save(); - return newUser; + if (getUser(user.getName()) != null) { + User newUser = new User(user); + users.put(newUser.getName(), newUser); + save(); + return newUser; + } else { + return null; + } } } -- cgit v1.2.3 From 72d614074a3cd578322931af647c6f0a65a23dfd Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Wed, 26 Feb 2025 16:00:35 -0500 Subject: Tested user http calls and fixed update user --- .../src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java') 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 0f30824..18eec18 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 @@ -95,7 +95,7 @@ public class UserFileDAO implements UserDAO { @Override public User createUser(User user) throws IOException { synchronized (users) { - if (getUser(user.getName()) != null) { + if (getUser(user.getName()) == null) { User newUser = new User(user); users.put(newUser.getName(), newUser); save(); @@ -131,7 +131,7 @@ public class UserFileDAO implements UserDAO { /** * Delete a user matching the name * - * @param name The name of the user + * @param name The name of the user * * @return True if deleted, false otherwise * @throws IOException If there was an IO issue saving the file -- cgit v1.2.3