diff options
| author | Tyler Ferrari <69283684+Sowgro@users.noreply.github.com> | 2025-03-27 18:50:33 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-27 18:50:33 -0400 | 
| commit | ddbd1cc688aa98fb275ad72a750fbaaf53e6c0ae (patch) | |
| tree | 0a0f9669fb0f7cf2f2816b798269e50a8b26f125 /ufund-api/src/main/java/com/ufund/api/ufundapi/controller | |
| parent | 35d7c971ed47718d4dc5738edb09d62cd780dac4 (diff) | |
| parent | 4f5e9e9ecda282a98af5d70bd6cf0540973c7314 (diff) | |
| download | JellySolutions-ddbd1cc688aa98fb275ad72a750fbaaf53e6c0ae.tar.gz JellySolutions-ddbd1cc688aa98fb275ad72a750fbaaf53e6c0ae.tar.bz2 JellySolutions-ddbd1cc688aa98fb275ad72a750fbaaf53e6c0ae.zip  | |
Merge pull request #17 from RIT-SWEN-261-02/api-cleanup
Merge api-cleanup into main
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/controller')
3 files changed, 118 insertions, 43 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java index b46d4ee..aa99a90 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java @@ -2,6 +2,8 @@ package com.ufund.api.ufundapi.controller;  import java.io.IOException;  import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger;  import org.springframework.http.HttpStatus;  import org.springframework.http.ResponseEntity; @@ -17,6 +19,7 @@ import com.ufund.api.ufundapi.service.AuthService;  @RestController  @RequestMapping("auth")  public class AuthController { +    private static final Logger LOG = Logger.getLogger(AuthController.class.getName());      private final AuthService authService;      public AuthController(AuthService authService) { @@ -32,14 +35,17 @@ public class AuthController {       */      @PostMapping("")      public ResponseEntity<String> login(@RequestBody Map<String, String> params) { +        LOG.log(Level.INFO, "POST /auth body={0}", params);          String username = params.get("username");          String password = params.get("password");          try {              String key = authService.login(username, password);              return new ResponseEntity<>(key, HttpStatus.OK); -        } catch (IllegalAccessException e) { +        } catch (IllegalAccessException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);          } catch (IOException ex) { +            LOG.log(Level.SEVERE, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          }      } @@ -52,10 +58,12 @@ public class AuthController {       */      @DeleteMapping("")      public ResponseEntity<Object> logout(@RequestHeader("jelly-api-key") String key) { +        LOG.log(Level.INFO, "DELETE /auth key={0}", key);          try {              authService.logout(key);              return new ResponseEntity<>(HttpStatus.OK); -        } catch (IOException e) { +        } catch (IOException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          }      } 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 36ae341..55ee457 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 @@ -5,6 +5,7 @@ import java.util.Map;  import java.util.logging.Level;  import java.util.logging.Logger; +import com.ufund.api.ufundapi.service.AuthService;  import org.springframework.http.HttpStatus;  import org.springframework.http.ResponseEntity;  import org.springframework.web.bind.annotation.DeleteMapping; @@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.PathVariable;  import org.springframework.web.bind.annotation.PostMapping;  import org.springframework.web.bind.annotation.PutMapping;  import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.bind.annotation.RequestParam;  import org.springframework.web.bind.annotation.RestController; @@ -27,14 +29,16 @@ import com.ufund.api.ufundapi.service.CupboardService;  public class CupboardController {      private static final Logger LOG = Logger.getLogger(CupboardController.class.getName());      private final CupboardService cupboardService; +    private final AuthService authService;      /**       * Create a cupboard controller to receive REST signals       *       * @param cupboardService The Data Access Object       */ -    public CupboardController(CupboardService cupboardService) { +    public CupboardController(CupboardService cupboardService, AuthService authService) {          this.cupboardService = cupboardService; +        this.authService = authService;      }      /** @@ -47,20 +51,28 @@ public class CupboardController {       *         INTERNAL_SERVER_ERROR otherwise       */      @PostMapping("") -    public ResponseEntity<Need> createNeed(@RequestBody Map<String, Object> params) { -        System.out.println(params); +    public ResponseEntity<Need> createNeed(@RequestBody Map<String, Object> params, @RequestHeader("jelly-api-key") String key) { +        LOG.log(Level.INFO, "POST /cupboard body={0}", params); +          String name = (String) params.get("name"); -        double maxGoal = (double) params.get("maxGoal"); +        double maxGoal = ((Number) params.get("maxGoal")).doubleValue();          Need.GoalType goalType = GoalType.valueOf((String) params.get("type"));          try { +            authService.keyHasAccessToCupboard(key);              Need need = cupboardService.createNeed(name, maxGoal, goalType);              return new ResponseEntity<>(need, HttpStatus.OK);          } catch (DuplicateKeyException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.CONFLICT);          } catch (IllegalArgumentException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.BAD_REQUEST); +        } catch (IllegalAccessException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage()); +            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);          } catch (IOException ex) { +            LOG.log(Level.SEVERE, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          }      } @@ -75,7 +87,7 @@ public class CupboardController {       */      @GetMapping("")      public ResponseEntity<Need[]> getNeeds() { -        LOG.info("GET /needs"); +        LOG.info("GET /cupboard");          try {              Need[] needs = cupboardService.getNeeds(); @@ -87,19 +99,21 @@ public class CupboardController {      }       /** -     * Responds to the GET request for all {@linkplain Need need} whose name contains -     * the text in name -     *  -     * @param name The name parameter which contains the text used to find the {@link Need need} -     *  -     * @return ResponseEntity with array of {@link Need need} objects (may be empty) and -     * HTTP status of OK<br> -     * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise -     * <p> -     */ +      * Responds to the GET request for all {@linkplain Need need} whose name contains +      * the text in name +      * +      * @param name The name parameter which contains the text used to find the {@link Need need} +      * +      * @deprecated Searching should now be done client side in the future +      * +      * @return ResponseEntity with array of {@link Need need} objects (may be empty) and +      * HTTP status of OK<br> +      * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise +      * <p> +      */      @GetMapping("/")      public ResponseEntity<Need[]> searchNeeds(@RequestParam String name) { -        LOG.info("GET /need/?name="+name); +        LOG.info("GET /cupboard/?name="+name);          try {              Need[] needs = cupboardService.searchNeeds(name); @@ -120,7 +134,7 @@ public class CupboardController {       */      @GetMapping("/{id}")      public ResponseEntity<Need> getNeed(@PathVariable int id) { -        LOG.log(Level.INFO, "GET /need/{0}", id); +        LOG.log(Level.INFO, "GET /cupboard/{0}", id);          try {              Need need = cupboardService.getNeed(id); @@ -143,9 +157,10 @@ public class CupboardController {       * @return OK response and the need if it was successful, or INTERNAL_SERVER_ERROR if there was an issue       */      @PutMapping("/{id}") -    public ResponseEntity<Need> updateNeed(@RequestBody Need need, @PathVariable int id) { -        LOG.log(Level.INFO, "Updating need: " + need); +    public ResponseEntity<Need> updateNeed(@RequestBody Need need, @PathVariable int id, @RequestHeader("jelly-api-key") String key) { +        LOG.log(Level.INFO, "PUT /cupboard/{0} body={1}", of(id, need));          try { +            authService.keyHasAccessToCupboard(key);              Need updatedNeed = cupboardService.updateNeed(need, id);              if (updatedNeed != null) {                  return new ResponseEntity<>(need, HttpStatus.OK); @@ -153,10 +168,40 @@ public class CupboardController {                  return new ResponseEntity<>(HttpStatus.NOT_FOUND);              }          } catch (IllegalArgumentException ex) { -            ex.printStackTrace(); +            LOG.log(Level.WARNING, ex.getLocalizedMessage()); +            return new ResponseEntity<>(HttpStatus.BAD_REQUEST); +        } catch (IllegalAccessException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage()); +            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); +        } catch (IOException ex) { +            LOG.log(Level.SEVERE, ex.getLocalizedMessage()); +            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); +        } +    } + +    /** +     * Checks out a need by checkoutAmount +     *  +     * @param data JSON object with paramters needID and amount +     * @param key  Key used to authenticate user +     * @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); +        try { +            cupboardService.checkoutNeed(needID, checkoutAmount, key); +            return new ResponseEntity<>(HttpStatus.OK); +        } catch (IllegalArgumentException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.BAD_REQUEST); +        } catch (IllegalAccessException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage()); +            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);          } catch (IOException ex) { -            ex.printStackTrace(); +            LOG.log(Level.SEVERE, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          }      } @@ -168,17 +213,27 @@ public class CupboardController {       * @return OK if the need was deleted, NOT_FOUND if the need was not found, or INTERNAL_SERVER_ERROR if an error occurred      */      @DeleteMapping("/{id}") -    public ResponseEntity<Need> deleteNeed(@PathVariable int id) { +    public ResponseEntity<Need> deleteNeed(@PathVariable int id, @RequestHeader("jelly-api-key") String key) { +        LOG.log(Level.INFO, "DELETE /cupboard/{0}", id);          try { +            authService.keyHasAccessToCupboard(key);              Need need = cupboardService.getNeed(id);              if (cupboardService.deleteNeed(id)) {                  return new ResponseEntity<>(need, HttpStatus.OK);              } else {                  return new ResponseEntity<>(HttpStatus.NOT_FOUND); -            }  -        } catch (IOException e) { +            } +        } catch (IllegalAccessException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage()); +            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); +        } catch (IOException ex) { +            LOG.log(Level.SEVERE, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          }      } +    private Object[] of(Object ...params) { +        return params; +    } +  } 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 dfaad3a..33d2e4f 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 @@ -1,7 +1,6 @@  package com.ufund.api.ufundapi.controller;  import java.io.IOException; -import java.security.InvalidParameterException;  import java.util.Map;  import java.util.logging.Level;  import java.util.logging.Logger; @@ -43,6 +42,7 @@ public class UserController {       */      @PostMapping("")      public ResponseEntity<User> createUser(@RequestBody Map<String, String> params) { +        LOG.log(Level.INFO, "POST /users body={0}", params);          String username = params.get("username");          String password = params.get("password"); @@ -54,8 +54,10 @@ public class UserController {                  return new ResponseEntity<>(HttpStatus.CONFLICT);              }          } catch (DuplicateKeyException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.CONFLICT);          } catch (IOException ex) { +            LOG.log(Level.SEVERE, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          }      } @@ -72,10 +74,10 @@ public class UserController {       */      @GetMapping("/{username}")      public ResponseEntity<User> getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { -        LOG.log(Level.INFO, "GET /user/{0}", username); +        LOG.log(Level.INFO, "GET /user/{0} key={1}", of(username, key));          try { -            authService.authenticate(username, key); +            authService.keyHasAccessToUser(username, key);              User user = userService.getUser(username);              if (user != null) {                  return new ResponseEntity<>(user.withoutPasswordHash(), HttpStatus.OK); @@ -83,9 +85,10 @@ public class UserController {                  return new ResponseEntity<>(HttpStatus.NOT_FOUND);              }          } catch (IllegalAccessException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); -        } catch (IOException e) { -            LOG.log(Level.SEVERE, e.getLocalizedMessage()); +        } catch (IOException ex) { +            LOG.log(Level.SEVERE, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          } @@ -102,23 +105,25 @@ public class UserController {       */      @PutMapping("/{username}")      public ResponseEntity<User> updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { -        LOG.log(Level.INFO,"PUT: " + user + " " + username + " " + key.toString()); +        LOG.log(Level.INFO,"PUT /users/{0} body={1} key={2}", of(username, user, key));          try { -            //authService.authenticate(username, key); +            authService.keyHasAccessToUser(username, key);              user = userService.updateUser(user, username);              if (user != null) {                  return new ResponseEntity<>(user, HttpStatus.OK);              } else {                  return new ResponseEntity<>(HttpStatus.NOT_FOUND);              } -        } catch (InvalidParameterException ex) { +        } catch (IllegalArgumentException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.BAD_REQUEST); -        } catch (IOException e) { +        } catch (IllegalAccessException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage()); +            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); +        } catch (IOException ex) { +            LOG.log(Level.SEVERE, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); -        }  -        // catch (IllegalAccessException e) { -        //     return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); -        // } +        }      }      /** @@ -131,19 +136,26 @@ public class UserController {       */      @DeleteMapping("/{username}")      public ResponseEntity<Boolean> deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { +        LOG.log(Level.INFO, "DELETE /users/{0} id={1}", of(username, key));          try { -            authService.authenticate(username, key); +            authService.keyHasAccessToUser(username, key);              if (userService.deleteUser(username)) {                  return new ResponseEntity<>(HttpStatus.OK);              } else {                  return new ResponseEntity<>(HttpStatus.NOT_FOUND);              } -        } catch (IOException e) { -            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); -        } catch (IllegalAccessException e) { +        } catch (IllegalAccessException ex) { +            LOG.log(Level.WARNING, ex.getLocalizedMessage());              return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); +        } catch (IOException ex) { +            LOG.log(Level.SEVERE, ex.getLocalizedMessage()); +            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          }      } +    private Object[] of(Object ...params) { +        return params; +    } +  }  | 
