aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2025-02-16 13:26:43 -0500
committersowgro <tpoke.ferrari@gmail.com>2025-02-16 13:26:43 -0500
commita4aa226955df2284a298b6ae35ee72d4996978a3 (patch)
treeaad3d8d420402298a112dc3c4952599f4aba6d4d /ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java
parentb7c662afadf531eae34c5044c50c7b5c5330345b (diff)
downloadJellySolutions-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/CupboardFileDao.java')
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java142
1 files changed, 129 insertions, 13 deletions
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