aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java
blob: 6052e4f98ee13a911aefd660265f73003e07ed58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.ufund.api.ufundapi.service;

import java.io.IOException;
import java.util.Arrays;

import com.ufund.api.ufundapi.model.Need;
import com.ufund.api.ufundapi.persistence.CupboardDAO;
import org.springframework.stereotype.Component;

@Component
public class CupboardService {

    private final CupboardDAO cupboardDAO;

    public class DuplicateKeyException extends Exception {
        public DuplicateKeyException(String message) {
            super(message);
        }
    }

    public CupboardService(CupboardDAO cupboardDAO) {
        this.cupboardDAO = cupboardDAO;
    }

    public Need createNeed(String name, int maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException {
        
        Need need = new Need(name, goalType, maxGoal);

        if (need.getMaxGoal() <= 0) {
            throw new IllegalArgumentException("Max Goal must be greater than zero");
        } else {
            for (Need searchNeed : cupboardDAO.getNeeds()) {
                if (need.getName().equalsIgnoreCase(searchNeed.getName())) {
                    throw new DuplicateKeyException("Duplicate names are not allowed");
                }
            }
            return cupboardDAO.addNeed(need);
        }
        
    }

    public Need[] getNeeds() throws IOException {
        return cupboardDAO.getNeeds();
    }

    /**
     * Returns an array of needs filtered by a search
     *
     * @param search The search substring
     * @return The requested array
     * @throws IOException 
     */
    public Need[] searchNeeds(String search) throws IOException {
        return Arrays.stream(cupboardDAO.getNeeds())
                .filter(i -> i.getName().toLowerCase().contains(search.toLowerCase()))
                .toArray(Need[]::new);
    }

    /**
     * Gets a need with the specified ID
     *
     * @param id the ID of the need
     * @return The resulting Need or null if the need was not found
     */
    public Need getNeed(int id) throws IOException {
        return cupboardDAO.getNeed(id);
    }

    /**
     * Modify a need
     *
     * @param need
     * @return
     * @throws IOException Thrown if there was an issue saving the changes
     */
    public Need updateNeed(Need need) throws IOException {
        return cupboardDAO.updateNeed(need);
    }

    /**
     * Delete a need from the cupboard
     *
     * @param id the ID of the need
     * @return True if the need was deleted
     * @throws IOException Thrown on any problem removing the need
     */
    public boolean deleteNeed(int id) throws IOException {
        return cupboardDAO.deleteNeed(id);
    }
}