aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/controller
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2025-04-04 22:58:26 -0400
committersowgro <tpoke.ferrari@gmail.com>2025-04-04 22:58:26 -0400
commitf70e86470f7083cda1949ff97556d9e39578ce1d (patch)
tree5cc8a3e1089409e10fd92896625605a7f8f6897b /ufund-api/src/main/java/com/ufund/api/ufundapi/controller
parentedfc9a8450d140ba1650b38e4b707000710b2faa (diff)
parent5a5d31896d79a736bce33b7d1aa7b3168ba308a9 (diff)
downloadJellySolutions-f70e86470f7083cda1949ff97556d9e39578ce1d.tar.gz
JellySolutions-f70e86470f7083cda1949ff97556d9e39578ce1d.tar.bz2
JellySolutions-f70e86470f7083cda1949ff97556d9e39578ce1d.zip
Merge branch 'main' into need-list-abstraction
# Conflicts: # ufund-ui/src/app/components/cupboard/cupboard.component.ts # ufund-ui/src/app/components/funding-basket/funding-basket.component.html # ufund-ui/src/app/components/funding-basket/funding-basket.component.ts # ufund-ui/src/app/components/need-list/need-list.component.html # ufund-ui/src/app/components/need-list/need-list.component.ts # ufund-ui/src/app/components/need-page/need-page.component.html
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller')
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java21
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java26
2 files changed, 42 insertions, 5 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java
index 12fb0a9..075878a 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java
@@ -1,6 +1,7 @@
package com.ufund.api.ufundapi.controller;
import java.io.IOException;
+import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -189,12 +190,22 @@ public class CupboardController {
* @return OK if successful, other statuses if failure
*/
@PutMapping("/checkout")
- public ResponseEntity<Object> checkoutNeeds(@RequestBody Map<String, Integer> data, @RequestHeader("jelly-api-key") String key) {
- int needID = data.get("needID");
- int checkoutAmount = data.get("amount");
- LOG.log(Level.INFO, "PUT /need/checkout body={0}", data);
+ public ResponseEntity<Object> checkoutNeeds(@RequestBody List<Map<String, Integer>> data, @RequestHeader("jelly-api-key") String key) {
+ LOG.log(Level.INFO, "PUT /cupboard/checkout body={0}", data);
try {
- cupboardService.checkoutNeed(needID, checkoutAmount, key);
+ authService.keyIsValid(key);
+
+ for (Map<String, Integer> map : data) {
+ int needID = map.get("needID");
+ if (cupboardService.getNeed(needID) == null) {
+ return new ResponseEntity<>("One or more need is invalid, please refresh.", HttpStatus.BAD_REQUEST);
+ }
+ }
+ for (Map<String, Integer> map : data) {
+ int needID = map.get("needID");
+ int checkoutAmount = map.get("quantity");
+ cupboardService.checkoutNeed(needID, checkoutAmount, key);
+ }
return new ResponseEntity<>(HttpStatus.OK);
} catch (IllegalArgumentException ex) {
LOG.log(Level.WARNING, ex.getLocalizedMessage());
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
index a34e891..6953276 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
@@ -95,6 +95,32 @@ public class UserController {
}
/**
+ * Responds to the GET request with the total number of users
+ *
+ * @param key The authentication key of the user
+ * @return ResponseEntity with amount and HTTP status of OK<br>
+ * ResponseEntity with HTTP status of UNAUTHORIZED if user is not aa manager<br>
+ * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise
+ */
+ @GetMapping("/count")
+ public ResponseEntity<Object> getUserCount(@RequestHeader("jelly-api-key") String key) {
+ LOG.log(Level.INFO, "GET /userAmount");
+
+ try {
+ authService.keyHasAccessToCupboard(key);
+ String count = String.valueOf(userService.getUserCount());
+ return new ResponseEntity<>(count, HttpStatus.OK);
+ } catch (IllegalAccessException ex) {
+ LOG.log(Level.WARNING, ex.getLocalizedMessage());
+ return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED);
+ } catch (IOException ex) {
+ LOG.log(Level.SEVERE, ex.getLocalizedMessage());
+ return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+
+ }
+
+ /**
* Updates a User with the provided one
*
* @param user The user to update