aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java
diff options
context:
space:
mode:
authorGunther6070 <haydenhartman10@yahoo.com>2025-03-17 23:12:49 -0400
committerGunther6070 <haydenhartman10@yahoo.com>2025-03-17 23:12:49 -0400
commit5b6c20479fbb6ed0cabbbc88b42280c5a7dbd22c (patch)
tree16f93b3ce3b40de1e550f3824f60ca3aac632265 /ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java
parent7a5c5073e9e410b3ccc3ab7902a0d6f558277c7d (diff)
parent2b847078b7af4ade35461b8af52352bc88994be3 (diff)
downloadJellySolutions-5b6c20479fbb6ed0cabbbc88b42280c5a7dbd22c.tar.gz
JellySolutions-5b6c20479fbb6ed0cabbbc88b42280c5a7dbd22c.tar.bz2
JellySolutions-5b6c20479fbb6ed0cabbbc88b42280c5a7dbd22c.zip
Merge branch 'main' into funding_basket
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.java145
1 files changed, 0 insertions, 145 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
deleted file mode 100644
index 81ee7c0..0000000
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java
+++ /dev/null
@@ -1,145 +0,0 @@
-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.io.File;
-import java.io.IOException;
-import java.util.Map;
-import java.util.TreeMap;
-
-@Component
-public class CupboardFileDao implements CupboardDAO {
-
- 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++;
- }
-
- /**
- * 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