|
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); |
|
|
|
|
|
|
96
|
|
|
return true; |
|
97
|
|
|
} catch (NotFound e) { |
|
98
|
|
|
throw new PreferenceNotFound(name); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|