diff options
author | Tyler Ferrari <69283684+Sowgro@users.noreply.github.com> | 2025-02-26 17:20:11 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-26 17:20:11 -0500 |
commit | 9d832c1e96ca061b80b32abda472269bf7e0bbb7 (patch) | |
tree | 67ccc4c1f33b64d75c9cc7d57d05da3bc541bd26 /ufund-api/src/main/java/com/ufund/api/ufundapi/persistence | |
parent | 4c452d39214c42e2b0dc2a557b0c2ef1e3bcd9c0 (diff) | |
parent | da74d5c7e67b873dc8fdbfc672891eb63c0b8ea2 (diff) | |
download | JellySolutions-9d832c1e96ca061b80b32abda472269bf7e0bbb7.tar.gz JellySolutions-9d832c1e96ca061b80b32abda472269bf7e0bbb7.tar.bz2 JellySolutions-9d832c1e96ca061b80b32abda472269bf7e0bbb7.zip |
Merge pull request #7 from RIT-SWEN-261-02/user-functionality
Merge user-functionality into main
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/UserDAO.java | 74 | ||||
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | 151 |
2 files changed, 225 insertions, 0 deletions
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 new file mode 100644 index 0000000..d456abc --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java @@ -0,0 +1,74 @@ +package com.ufund.api.ufundapi.persistence; + +import java.io.IOException; + +import com.ufund.api.ufundapi.model.User; + + +/** + * Defines the interface for User object persistence + * + * @author Team 2B Jelly Solutions + */ +public interface UserDAO { + /** + * Retrieves all {@linkplain User users} + * + * @return An array of {@link User user} objects, may be empty + * + * @throws IOException if an issue with underlying storage + */ + User[] getUsers() throws IOException; + + /** + * Retrieves a {@linkplain User user} with the given name + * + * @param id The ID of the {@link User user} to get + * + * @return a {@link User user} object with the matching name + * <br> + * null if no {@link User user} with a matching name is found + * + * @throws IOException if an issue with underlying storage + */ + User getUser(String name) throws IOException; + + /** + * Creates and saves a {@linkplain User user} + * + * @param user {@linkplain User user} object to be created and saved + * <br> + * The id of the user object is automatically incremented. + * + * @return new {@link User user} if successful, null otherwise + * + * @throws IOException if an issue with underlying storage + */ + User createUser(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 + * + * @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; + + /** + * Deletes a {@linkplain User user} with the given id + * + * @param id The id of the {@link User user} + * + * @return true if the {@link User user} was deleted + * <br> + * false if the user with the given id does not exist + * + * @throws IOException if underlying storage cannot be accessed + */ + boolean deleteUser(String name) 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 new file mode 100644 index 0000000..18eec18 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -0,0 +1,151 @@ +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.User; + +@Component +public class UserFileDAO implements UserDAO { + + private final Map<String, User> users; // cache + private final ObjectMapper objectMapper; + private final String filename; + + public UserFileDAO(@Value("${users.file}") String filename, ObjectMapper objectMapper) throws IOException { + this.filename = filename; + this.objectMapper = objectMapper; + users = new TreeMap<>(); + load(); // load the users from the file + } + + /** + * Load changes from the json file + * + * @throws IOException Any IO issue with the file + */ + private void load() throws IOException { + users.clear(); + + User[] usersArray = objectMapper.readValue(new File(filename), User[].class); + + for (User user : usersArray) { + users.put(user.getName(), user); + } + } + + /** + * 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 { + User[] userArray = getUserArray(); + + objectMapper.writeValue(new File(filename), userArray); + 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 { + 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 User getUser(String name) throws IOException { + synchronized (users) { + return users.getOrDefault(name, 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 { + synchronized (users) { + if (getUser(user.getName()) == null) { + User newUser = new User(user); + users.put(newUser.getName(), newUser); + save(); + return newUser; + } else { + return null; + } + } + } + + /** + * 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 { + synchronized (users) { + if (users.containsKey(name)) { + users.put(name, newUser); + save(); + 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) { + if (users.containsKey(name)) { + users.remove(name); + return save(); + } else { + return false; + } + } + } + +}
\ No newline at end of file |