Completed
Push — master ( d8c339...7072a0 )
by Kunal
03:14
created

savePreference(String,String,String)   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 1
rs 9.4285
1
package com.base.Services;
2
3
import com.base.Base;
4
import com.base.Exceptions.BaseHttpException;
5
import com.base.Exceptions.Http.NotFound;
6
import com.base.Exceptions.PreferenceNotFound;
7
import com.base.Http.Request.Request;
8
import com.base.Http.Response.Response;
9
import com.base.Models.Preference;
10
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.HashMap;
14
import java.util.List;
15
16
public class PreferenceService {
17
18
    /**
19
     * {@link Base}
20
     */
21
    private Base base;
22
23
    /**
24
     * Construct Instance of Base Class
25
     *
26
     * @param base
27
     */
28
    public PreferenceService(Base base) {
29
        this.base = base;
30
    }
31
32
33
    /**
34
     * Save New Preference of User
35
     *
36
     * @param value
37
     * @param category
38
     * @param name
39
     * @return Preference
40
     * @throws BaseHttpException Exception
41
     */
42
    public Preference savePreference(String name, String value, String category) throws BaseHttpException {
43
        HashMap<String, String> parameters = new HashMap<>();
44
        parameters.put("value", value);
45
        parameters.put("category", category);
46
        parameters.put("name", name);
47
        String URL = "/preferences/".concat(name);
48
        Response response = this.base.sendRequest(URL, Request.METHOD_PUT, parameters);
49
        return (Preference) Base.makeModel(Preference.class, response.getBody());
50
    }
51
52
    /**
53
     * Get All The Preference of User
54
     *
55
     * @return List of Preference
56
     * @throws BaseHttpException Exception
57
     */
58
    public List<Preference> getAllPreferences() throws BaseHttpException {
59
        String URL = "/preferences";
60
        Response response = this.base.sendRequest(URL, Request.METHOD_GET);
61
        Preference[] preferencesArray = (Preference[]) Base.makeModel(Preference[].class, response.getBody());
62
        return new ArrayList<>(Arrays.asList(preferencesArray));
63
    }
64
65
66
    /**
67
     * Get Perfereneces by name
68
     *
69
     * @param name
70
     * @return Preference
71
     * @throws PreferenceNotFound Exception
72
     * @throws BaseHttpException  Exception
73
     */
74
    public Preference getPreference(String name) throws PreferenceNotFound, BaseHttpException {
75
        try {
76
            String URL = "/preferences/".concat(name);
77
            Response response = this.base.sendRequest(URL, Request.METHOD_GET);
78
            return (Preference) Base.makeModel(Preference.class, response.getBody());
79
        } catch (NotFound e) {
80
            throw new PreferenceNotFound(name);
81
        }
82
    }
83
84
    /**
85
     * Delete Preference By name
86
     *
87
     * @param name
88
     * @return Boolean
89
     * @throws BaseHttpException  Exception
90
     * @throws PreferenceNotFound Exception
91
     */
92
    public boolean deletePreference(String name) throws BaseHttpException, PreferenceNotFound {
93
        try {
94
            String URL = "/preferences/".concat(name);
95
            Response response = this.base.sendRequest(URL, Request.METHOD_DELETE);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable response.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "response".
Loading history...
96
            return true;
97
        } catch (NotFound e) {
98
            throw new PreferenceNotFound(name);
99
        }
100
    }
101
102
103
}
104