aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java2
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java24
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardDAO.java12
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDao.java142
4 files changed, 159 insertions, 21 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java
index de1695a..a626561 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Cupboard.java
@@ -34,7 +34,7 @@ public class Cupboard {
public void removeNeed(String name) throws IOException {
for (Need need : getNeeds()) {
if (need.getName().equals(name)) {
- dao.deleteNeed(need.getID());
+ dao.deleteNeed(need.getId());
return;
}
}
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java
index df65032..9187796 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java
@@ -14,6 +14,14 @@ public class Need {
private double maxGoal;
private double current;
+ /**
+ * Create a new need
+ *
+ * @param name The name of the need
+ * @param id The unique ID of the need
+ * @param maxGoal The maximum goal for this need
+ * @param type The type of need (monetary, physical)
+ */
public Need(String name, int id, double maxGoal, GoalType type) {
this.id = id;
this.name = name;
@@ -21,11 +29,25 @@ public class Need {
this.type = type;
}
+ /**
+ * Create a deep copy of another need
+ *
+ * @param other The need to copy from
+ */
+ public Need(Need other) {
+ this.name = other.name;
+ this.id = other.id;
+ this.filterAttributes = other.filterAttributes;
+ this.type = other.type;
+ this.maxGoal = other.maxGoal;
+ this.current = other.current;
+ }
+
public String getName() {
return name;
}
- public int getID() {
+ public int getId() {
return id;
}
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