aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-api/src')
-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
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java10
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java7
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java10
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java48
6 files changed, 102 insertions, 20 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
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java
index 29d46cf..27ba0b9 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java
@@ -34,6 +34,16 @@ public interface UserDAO {
User getUser(String username) throws IOException;
/**
+ * Retrieves the total count of users
+ *
+ * @return a {@link int amount} number of users
+ * <br>
+ *
+ * @throws IOException if an issue with underlying storage
+ */
+ int getUserCount() throws IOException;
+
+ /**
* Creates and saves a {@linkplain User user}
*
* @param user {@linkplain User user} object to be created and saved
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
index ec94da8..7f1fadd 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
@@ -58,6 +58,13 @@ public class UserFileDAO implements UserDAO {
}
@Override
+ public int getUserCount() {
+ synchronized (users) {
+ return users.size();
+ }
+ }
+
+ @Override
public User getUser(String username) {
synchronized (users) {
return users.getOrDefault(username, null);
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java
index 6e27f50..8b34e68 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java
@@ -56,6 +56,16 @@ public class UserService {
}
/**
+ * Returns the total number of users
+ *
+ * @return The number of users
+ * @throws IOException If there was any problem saving the file
+ */
+ public int getUserCount() throws IOException {
+ return userDAO.getUserCount();
+ }
+
+ /**
* Updates a user
*
* @param user The ID of the user to update
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
index 8572ec6..7ea4455 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
@@ -1,6 +1,7 @@
package com.ufund.api.ufundapi.controller;
import java.io.IOException;
+import java.util.List;
import java.util.Map;
import static java.util.Map.entry;
@@ -418,12 +419,14 @@ public class CupboardControllerTest {
@Test
public void checkoutNeeds() throws IOException, IllegalAccessException {
+ when(mockCupboardService.getNeed(0)).thenReturn(new Need("name", "image", "location", 0, 10, GoalType.MONETARY, true, "a"));
doNothing().when(mockCupboardService).checkoutNeed(0, 20, key);
-
- Map<String, Integer> needMap = Map.ofEntries(
- entry("needID", 0),
- entry("amount", 20)
+ var needMap = List.of(
+ Map.ofEntries(
+ entry("needID", 0),
+ entry("quantity", 20)
+ )
);
var res = cupboardController.checkoutNeeds(needMap, key);
@@ -435,9 +438,15 @@ public class CupboardControllerTest {
public void checkoutNeedsBadRequest() throws IOException, IllegalAccessException {
doThrow(new IllegalArgumentException()).when(mockCupboardService).checkoutNeed(0, 20, key);
- Map<String, Integer> needMap = Map.ofEntries(
- entry("needID", 0),
- entry("amount", 20)
+ var needMap = List.of(
+ Map.ofEntries(
+ entry("needID", 0),
+ entry("quantity", 20)
+ ),
+ Map.ofEntries(
+ entry("needID", 2),
+ entry("quantity", 30)
+ )
);
var res = cupboardController.checkoutNeeds(needMap, key);
@@ -447,11 +456,17 @@ public class CupboardControllerTest {
@Test
public void checkoutNeedsUnauthorized() throws IOException, IllegalAccessException {
- doThrow(new IllegalAccessException()).when(mockCupboardService).checkoutNeed(0, 20, key);
-
- Map<String, Integer> needMap = Map.ofEntries(
- entry("needID", 0),
- entry("amount", 20)
+ doThrow(new IllegalAccessException()).when(mockAuthService).keyIsValid(key);
+
+ var needMap = List.of(
+ Map.ofEntries(
+ entry("needID", 0),
+ entry("quantity", 20)
+ ),
+ Map.ofEntries(
+ entry("needID", 2),
+ entry("quantity", 30)
+ )
);
var res = cupboardController.checkoutNeeds(needMap, key);
@@ -461,11 +476,14 @@ public class CupboardControllerTest {
@Test
public void checkoutNeedsInternalError() throws IOException, IllegalAccessException {
+ when(mockCupboardService.getNeed(0)).thenReturn(new Need("name", "image", "location", 0, 10, GoalType.MONETARY, true, "a"));
doThrow(new IOException()).when(mockCupboardService).checkoutNeed(0, 20, key);
- Map<String, Integer> needMap = Map.ofEntries(
- entry("needID", 0),
- entry("amount", 20)
+ var needMap = List.of(
+ Map.ofEntries(
+ entry("needID", 0),
+ entry("quantity", 20)
+ )
);
var res = cupboardController.checkoutNeeds(needMap, key);