diff options
Diffstat (limited to 'ufund-api/src/main')
| -rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java | 21 | 
1 files changed, 16 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());  | 
