aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java
diff options
context:
space:
mode:
authorHayden Hartman <haydenhartman10@gmail.com>2025-03-15 23:59:47 -0400
committerGitHub <noreply@github.com>2025-03-15 23:59:47 -0400
commit9baaa0590fbc38c06d530786a1de804ee9edd7db (patch)
tree7c94dc98f9b1978f8ccf3c38bb3777237bf0788a /ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java
parente4e6ae9a3d142fc78f31ee19464ec5e54bfb516f (diff)
parenta3150b8a8e17c8a71f617745bb8588b397a75f47 (diff)
downloadJellySolutions-9baaa0590fbc38c06d530786a1de804ee9edd7db.tar.gz
JellySolutions-9baaa0590fbc38c06d530786a1de804ee9edd7db.tar.bz2
JellySolutions-9baaa0590fbc38c06d530786a1de804ee9edd7db.zip
Merge pull request #8 from RIT-SWEN-261-02/api-auth
First attempt at an authentication system.
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java')
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java
new file mode 100644
index 0000000..78f8f85
--- /dev/null
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java
@@ -0,0 +1,106 @@
+package com.ufund.api.ufundapi.service;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.springframework.stereotype.Component;
+
+import com.ufund.api.ufundapi.DuplicateKeyException;
+import com.ufund.api.ufundapi.model.Need;
+import com.ufund.api.ufundapi.persistence.CupboardDAO;
+
+@Component
+public class CupboardService {
+
+ private final CupboardDAO cupboardDAO;
+
+ public CupboardService(CupboardDAO cupboardDAO) {
+ this.cupboardDAO = cupboardDAO;
+ }
+
+ /**
+ * Creates a new Need
+ *
+ * @param name The name of the need to create
+ * @param maxGoal The max goal of the new need
+ * @param goalType The goal type of the new need
+ * @return The need that was created
+ * @throws IOException Thrown if there was any issue saving the data
+ * @throws DuplicateKeyException If there already exists a need with the same name
+ */
+ public Need createNeed(String name, double maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException {
+
+ if (maxGoal <= 0) {
+ throw new IllegalArgumentException("Max Goal must be greater than zero");
+ }
+
+ for (Need searchNeed : cupboardDAO.getNeeds()) {
+ if (searchNeed.getName().equalsIgnoreCase(name)) {
+ throw new DuplicateKeyException("Duplicate names are not allowed");
+ }
+ }
+
+ Need need = new Need(name, goalType, maxGoal);
+ return cupboardDAO.addNeed(need);
+
+ }
+
+ /**
+ * Get all the needs in the cupboard
+ *
+ * @return An array containing all needs
+ * @throws IOException Thrown if there was any issue saving the data
+ */
+ public Need[] getNeeds() throws IOException {
+ return cupboardDAO.getNeeds();
+ }
+
+ /**
+ * Returns an array of needs filtered by a search
+ *
+ * @param search The search substring
+ * @return The requested array
+ * @throws IOException Thrown if there was any issue saving the data
+ */
+ public Need[] searchNeeds(String search) throws IOException {
+ return Arrays.stream(cupboardDAO.getNeeds())
+ .filter(i -> i.getName().toLowerCase().contains(search.toLowerCase()))
+ .toArray(Need[]::new);
+ }
+
+ /**
+ * Gets a need with the specified ID
+ *
+ * @param id the ID of the need
+ * @return The resulting Need or null if the need was not found
+ */
+ public Need getNeed(int id) throws IOException {
+ return cupboardDAO.getNeed(id);
+ }
+
+ /**
+ * Updates a need
+ *
+ * @param id The ID of the need to update
+ * @param need The need object to set (note: the ID is ignored)
+ * @return The updated need object
+ * @throws IOException Thrown if there was an issue saving the changes
+ */
+ public Need updateNeed(Need need, int id) throws IOException {
+ if (need.getId() != id) {
+ throw new IllegalArgumentException("ID in URL and body must match");
+ }
+ return cupboardDAO.updateNeed(need);
+ }
+
+ /**
+ * Delete a need from the cupboard
+ *
+ * @param id the ID of the need
+ * @return True if the need was deleted
+ * @throws IOException Thrown on any problem removing the need
+ */
+ public boolean deleteNeed(int id) throws IOException {
+ return cupboardDAO.deleteNeed(id);
+ }
+}