diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-02-16 13:26:43 -0500 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-02-16 13:26:43 -0500 |
commit | a4aa226955df2284a298b6ae35ee72d4996978a3 (patch) | |
tree | aad3d8d420402298a112dc3c4952599f4aba6d4d /ufund-api/src/main/java/com/ufund/api/ufundapi/persistence | |
parent | b7c662afadf531eae34c5044c50c7b5c5330345b (diff) | |
download | JellySolutions-a4aa226955df2284a298b6ae35ee72d4996978a3.tar.gz JellySolutions-a4aa226955df2284a298b6ae35ee72d4996978a3.tar.bz2 JellySolutions-a4aa226955df2284a298b6ae35ee72d4996978a3.zip |
Implement FileDAO
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.java | 12 | ||||
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java | 142 |
2 files changed, 135 insertions, 19 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 4b016fa..1435410 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 @@ -22,9 +22,9 @@ public interface CupboardDAO { /** * Finds all {@linkplain Need needs} whose name contains the given text * - * @param containsText The text to match against + * @param targetName The text to match against * - * @return An array of {@link Need needs} whose nemes contains the given text, may be empty + * @return An array of {@link Need needs} whose names contains the given text, may be empty * * @throws IOException if an issue with underlying storage */ @@ -33,7 +33,7 @@ public interface CupboardDAO { /** * Retrieves a {@linkplain Need need} with the given name * - * @param name The name of the {@link Need need} to get + * @param id The ID of the {@link Need need} to get * * @return a {@link Need need} object with the matching name * <br> @@ -50,7 +50,7 @@ public interface CupboardDAO { * <br> * The id of the need object is automatically incremented. * - * @return new {@link Need need} if successful, false otherwise + * @return new {@link Need need} if successful, null otherwise * * @throws IOException if an issue with underlying storage */ @@ -59,7 +59,7 @@ public interface CupboardDAO { /** * Updates and saves a {@linkplain Need need} * - * @param {@link Need need} object to be updated and saved + * @param need {@link Need need} object to be updated and saved * * @return updated {@link Need need} if successful, null if * {@link Need need} could not be found @@ -75,7 +75,7 @@ public interface CupboardDAO { * * @return true if the {@link Need need} was deleted * <br> - * false if need with the given id does not exist + * false if the need with the given id does not exist * * @throws IOException if underlying storage cannot be accessed */ 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 f2c70f9..fd42e17 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 @@ -1,29 +1,145 @@ package com.ufund.api.ufundapi.persistence; +import com.fasterxml.jackson.databind.ObjectMapper; import com.ufund.api.ufundapi.model.Need; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; -import java.util.ArrayList; +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.TreeMap; -public class CupboardFileDao { - - private ArrayList<Need> needs; - private final String filePath; +@Component +public class CupboardFileDao implements CupboardDAO{ - public CupboardFileDao(String filepath) { - this.filePath = filepath; + 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 { + 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++; } - public ArrayList<Need> getNeeds() { - return needs; + /** + * Return an array of the needs + * + * @return An array of all the needs + */ + private Need[] getNeedsArray() { + return needs.values().toArray(Need[]::new); } - public String getFilePath() { - return filePath; + /** + * 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().contains(search)) + .toArray(Need[]::new); } - public void addNeed(Need need) { - needs.add(need); + /** + * 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 |