diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-03-06 21:41:39 -0500 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-03-06 21:41:39 -0500 |
commit | bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3 (patch) | |
tree | 7df43079fa8d3e5c7b34c5ab13389018c1216078 /ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java | |
parent | eb4edcc7e7e4f9a6a59bed6d3952486f179fc445 (diff) | |
download | JellySolutions-bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3.tar.gz JellySolutions-bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3.tar.bz2 JellySolutions-bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3.zip |
Push current changes (working on documentation and tests)
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.java | 126 |
1 files changed, 0 insertions, 126 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 84ea693..0000000 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java +++ /dev/null @@ -1,126 +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); - } - - /** - * 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 getNeed(int id) { - synchronized (needs) { - return needs.getOrDefault(id, null); - } - } - - @Override - public Need addNeed(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 |