diff options
| author | Gunther6070 <haydenhartman10@yahoo.com> | 2025-04-02 16:56:27 -0400 | 
|---|---|---|
| committer | Gunther6070 <haydenhartman10@yahoo.com> | 2025-04-02 16:56:27 -0400 | 
| commit | c561e9a5b36bafbb5e6f277b04f112941c2231a5 (patch) | |
| tree | 7b7b8de827b12f32c87d5529e822d31abddcb59c /ufund-api/src/main/java/com/ufund/api/ufundapi | |
| parent | bc23c624a760b33bb67b3c0df5fd3d8f39f36e77 (diff) | |
| download | JellySolutions-c561e9a5b36bafbb5e6f277b04f112941c2231a5.tar.gz JellySolutions-c561e9a5b36bafbb5e6f277b04f112941c2231a5.tar.bz2 JellySolutions-c561e9a5b36bafbb5e6f277b04f112941c2231a5.zip  | |
Implemented new endpoint to get total number of users
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi')
4 files changed, 53 insertions, 0 deletions
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..653a925 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("/userCount") +    public ResponseEntity<Object> getUserCount(@RequestHeader("jelly-api-key") String key) { +        LOG.log(Level.INFO, "GET /userAmount"); + +        try { +            authService.keyHasAccessToCupboard(key); +            int count = 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  | 
