diff options
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi')
3 files changed, 250 insertions, 0 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java new file mode 100644 index 0000000..12bbddc --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java @@ -0,0 +1,20 @@ +package com.ufund.api.ufundapi.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class User { + +    @JsonProperty("name") private String name; + +    /** +     * Create a new user +     * +     * @param name    The name of the user +     */ +    public User(String name) { +        this.name = name; +    } + +     +     +} 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 new file mode 100644 index 0000000..df797b9 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java @@ -0,0 +1,84 @@ +package com.ufund.api.ufundapi.persistence; + +import java.io.IOException; + +import com.ufund.api.ufundapi.model.Need; + +/** + * Defines the interface for Need object persistence + *  + * @author Team 2B Jelly Solutions + */ +public interface UserDAO { +    /** +     * Retrieves all {@linkplain Need needs} +     *  +     * @return An array of {@link Need need} objects, may be empty +     *  +     * @throws IOException if an issue with underlying storage +     */ +    Need[] getUsers() throws IOException; + +    /** +     * Finds all {@linkplain Need needs} whose name contains the given text +     *  +     * @param targetName The text to match against +     *  +     * @return An array of {@link Need needs} whose names contains the given text, +     *         may be empty +     *  +     * @throws IOException if an issue with underlying storage +     */ +    Need[] findUsers(String targetName) throws IOException; + +    /** +     * Retrieves a {@linkplain Need need} with the given name +     *  +     * @param id The ID of the {@link Need need} to get +     *  +     * @return a {@link Need need} object with the matching name +     *         <br> +     *         null if no {@link Need need} with a matching name is found +     *  +     * @throws IOException if an issue with underlying storage +     */ +    Need getUser(int id) throws IOException; + +    /** +     * Creates and saves a {@linkplain Need need} +     *  +     * @param need {@linkplain Need need} object to be created and saved +     *             <br> +     *             The id of the need object is automatically incremented. +     * +     * @return new {@link Need need} if successful, null otherwise +     *  +     * @throws IOException if an issue with underlying storage +     */ +    Need createNeed(Need need) throws IOException; + +    /** +     * Updates and saves a {@linkplain Need need} +     *  +     * @param need {@link Need need} object to be updated and saved +     *  +     * @return updated {@link Need need} if successful, null if +     *         {@link Need need} could not be found +     *  +     * @throws IOException if underlying storage cannot be accessed +     */ +    Need updateNeed(Need need) throws IOException; + +    /** +     * Deletes a {@linkplain Need need} with the given id +     *  +     * @param id The id of the {@link Need need} +     *  +     * @return true if the {@link Need need} was deleted +     *         <br> +     *         false if the need with the given id does not exist +     *  +     * @throws IOException if underlying storage cannot be accessed +     */ +    boolean deleteNeed(int id) throws IOException; +} 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 new file mode 100644 index 0000000..87b8103 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -0,0 +1,146 @@ +package com.ufund.api.ufundapi.persistence; + +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.TreeMap; + +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; + +@Component +public class UserFileDAO implements UserDAO { + +    private final Map<Integer, Need> needs; // 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; +    } + +    /** +     * Load changes from the json file +     * +     * @throws IOException Any IO issue with the file +     */ +    private void load() throws IOException { +        needs.clear(); +        nextId = 0; + +        Need[] needsArray = objectMapper.readValue(new File(filename), Need[].class); + +        for (Need need : needsArray) { +            needs.put(need.getId(), need); +            if (need.getId() > nextId()) { +                nextId = need.getId(); +            } +        } +        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); +    } + +    /** +     * Saves the needs to json +     * +     * @return True if the save was successful, false otherwise +     * @throws IOException If there was an IO issue saving the file +     */ +    private boolean save() throws IOException { +        Need[] needArray = getNeedsArray(); + +        objectMapper.writeValue(new File(filename), needArray); +        return true; +    } + +    @Override +    public Need[] getNeeds() { +        synchronized (needs) { +            return getNeedsArray(); +        } +    } + +    @Override +    public Need[] findNeeds(String targetName) { +        synchronized (needs) { +            return getNeedsArray(targetName); +        } +    } + +    @Override +    public Need getNeed(int id) { +        synchronized (needs) { +            return needs.getOrDefault(id, null); +        } +    } + +    @Override +    public Need createNeed(Need need) throws IOException { +        synchronized (needs) { +            Need newNeed = new Need(need); +            newNeed.setID(nextId()); +            needs.put(newNeed.getId(), newNeed); +            save(); +            return newNeed; +        } +    } + +    @Override +    public Need updateNeed(Need need) throws IOException { +        synchronized (needs) { +            if (needs.containsKey(need.getId())) { +                needs.put(need.getId(), need); +                save(); +                return need; +            } else { +                return null; +            } + +        } +    } + +    @Override +    public boolean deleteNeed(int id) throws IOException { +        synchronized (needs) { +            if (needs.containsKey(id)) { +                needs.remove(id); +                return save(); +            } else { +                return false; +            } +        } +    } +}
\ No newline at end of file  | 
