aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java
diff options
context:
space:
mode:
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.java23
1 files changed, 22 insertions, 1 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
index d09afb4..15d1fad 100644
--- 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
@@ -3,6 +3,7 @@ package com.ufund.api.ufundapi.service;
import java.io.IOException;
import java.util.Arrays;
+import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import com.ufund.api.ufundapi.DuplicateKeyException;
@@ -13,8 +14,10 @@ import com.ufund.api.ufundapi.persistence.CupboardDAO;
public class CupboardService {
private final CupboardDAO cupboardDAO;
+ final AuthService authService;
- public CupboardService(CupboardDAO cupboardDAO) {
+ public CupboardService(@Lazy AuthService authService, CupboardDAO cupboardDAO) {
+ this.authService = authService;
this.cupboardDAO = cupboardDAO;
}
@@ -97,6 +100,23 @@ public class CupboardService {
}
/**
+ * Checks out a need with the desired amount
+ *
+ * @param id The ID of the need to update
+ * @param checkoutAmount The amount to update the need by
+ * @throws IOException If there is an error reading the file
+ * @throws IllegalAccessException If the user has insufficient permission
+ */
+ public void checkoutNeed(int id, double checkoutAmount, String key) throws IOException, IllegalAccessException {
+ if (checkoutAmount <= 0) {
+ throw new IllegalArgumentException("Amount must be greater than 0");
+ }
+ authService.keyIsValid(key);
+ Need need = cupboardDAO.getNeed(id);
+ need.incrementCurrent(checkoutAmount);
+ }
+
+ /**
* Delete a need from the cupboard
*
* @param id the ID of the need
@@ -104,6 +124,7 @@ public class CupboardService {
* @throws IOException Thrown on any problem removing the need
*/
public boolean deleteNeed(int id) throws IOException {
+
return cupboardDAO.deleteNeed(id);
}
}