diff options
Diffstat (limited to 'ufund-api/src/main')
| -rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | 126 | 
1 files changed, 56 insertions, 70 deletions
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 87b8103..c42d211 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 @@ -9,27 +9,20 @@ import org.springframework.beans.factory.annotation.Value;  import org.springframework.stereotype.Component;  import com.fasterxml.jackson.databind.ObjectMapper; -import com.ufund.api.ufundapi.model.Need; +import com.ufund.api.ufundapi.model.User;  @Component  public class UserFileDAO implements UserDAO { -    private final Map<Integer, Need> needs; // cache +    private final Map<String, User> users; // cache      private final ObjectMapper objectMapper; -    private static int nextId;      private final String filename;      public UserFileDAO(@Value("${users.file}") String filename, ObjectMapper objectMapper) throws IOException {          this.filename = filename;          this.objectMapper = objectMapper; -        needs = new TreeMap<>(); -        load(); // load the heroes from the file -    } - -    private synchronized static int nextId() { -        int id = nextId; -        nextId++; -        return id; +        users = new TreeMap<>(); +        load(); // load the users from the file      }      /** @@ -38,39 +31,13 @@ public class UserFileDAO implements UserDAO {       * @throws IOException Any IO issue with the file       */      private void load() throws IOException { -        needs.clear(); -        nextId = 0; +        users.clear(); -        Need[] needsArray = objectMapper.readValue(new File(filename), Need[].class); +        User[] usersArray = objectMapper.readValue(new File(filename), User[].class); -        for (Need need : needsArray) { -            needs.put(need.getId(), need); -            if (need.getId() > nextId()) { -                nextId = need.getId(); -            } +        for (User user : usersArray) { +            users.put(user.getName(), user);          } -        nextId++; -    } - -    /** -     * Return an array of the needs -     * -     * @return An array of all the needs -     */ -    private Need[] getNeedsArray() { -        return needs.values().toArray(Need[]::new); -    } - -    /** -     * Returns an array of needs filtered by a search -     * -     * @param search The search substring -     * @return The requested array -     */ -    private Need[] getNeedsArray(String search) { -        return needs.values().stream() -                .filter(i -> i.getName().toLowerCase().contains(search.toLowerCase())) -                .toArray(Need[]::new);      }      /** @@ -80,67 +47,86 @@ public class UserFileDAO implements UserDAO {       * @throws IOException If there was an IO issue saving the file       */      private boolean save() throws IOException { -        Need[] needArray = getNeedsArray(); +        User[] userArray = getUserArray(); -        objectMapper.writeValue(new File(filename), needArray); +        objectMapper.writeValue(new File(filename), userArray);          return true;      } -    @Override -    public Need[] getNeeds() { -        synchronized (needs) { -            return getNeedsArray(); -        } +    /** +     * Return an array of the needs +     * +     * @return An array of all the needs +     */ +    private User[] getUserArray() { +        return users.values().toArray(User[]::new);      } +          @Override -    public Need[] findNeeds(String targetName) { -        synchronized (needs) { -            return getNeedsArray(targetName); +    public User[] getUsers() throws IOException { +        synchronized (users) { +            return getUserArray();          }      } +    /** +     * Return the user with the String name name or null otherwise +     *  +     * @param name Name of desired user +     *  +     * @return Desired user, null otherwise +     * @throws IOException If there was an IO issue saving the file +     */      @Override -    public Need getNeed(int id) { -        synchronized (needs) { -            return needs.getOrDefault(id, null); +    public User getUser(String name) throws IOException { +        synchronized (users) { +            return users.getOrDefault(name, null);          }      } +    /** +     * Return the user with the String name name or null otherwise +     *  +     * @param name Name of desired user +     *  +     * @return Desired user, null otherwise +     * @throws IOException If there was an IO issue saving the file +     */      @Override -    public Need createNeed(Need need) throws IOException { -        synchronized (needs) { -            Need newNeed = new Need(need); -            newNeed.setID(nextId()); -            needs.put(newNeed.getId(), newNeed); +    public User createUser(User user) throws IOException { +        synchronized (users) { +            User newUser = new User(user); +            users.put(newUser.getName(), newUser);              save(); -            return newNeed; +            return newUser;          }      }      @Override -    public Need updateNeed(Need need) throws IOException { -        synchronized (needs) { -            if (needs.containsKey(need.getId())) { -                needs.put(need.getId(), need); +    public User updateUser(User user) throws IOException { +        synchronized (users) { +            if (users.containsKey(user.getName())) { +                users.put(user.getName(), user);                  save(); -                return need; +                return user;              } else {                  return null;              } -          }      }      @Override -    public boolean deleteNeed(int id) throws IOException { -        synchronized (needs) { -            if (needs.containsKey(id)) { -                needs.remove(id); +    public boolean deleteUser(String name) throws IOException { +        synchronized (users) { +            if (users.containsKey(name)) { +                users.remove(name);                  return save();              } else {                  return false;              }          }      } + +      }
\ No newline at end of file  | 
