|
1
|
|
|
package com.base.Services; |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
import com.base.Base; |
|
5
|
|
|
import com.base.Exceptions.BaseHttpException; |
|
6
|
|
|
import com.base.Exceptions.UserNotFound; |
|
7
|
|
|
import com.base.Http.Request.Request; |
|
8
|
|
|
import com.base.Http.Response.Response; |
|
9
|
|
|
import com.base.Models.Media; |
|
10
|
|
|
import com.base.Models.User; |
|
11
|
|
|
|
|
12
|
|
|
import java.io.File; |
|
13
|
|
|
import java.util.HashMap; |
|
14
|
|
|
import java.util.Map; |
|
15
|
|
|
|
|
16
|
|
|
public class UserService { |
|
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 UserService(Base base) { |
|
29
|
|
|
this.base = base; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get User By ID. |
|
34
|
|
|
* |
|
35
|
|
|
* @param user_id |
|
36
|
|
|
* @return |
|
37
|
|
|
* @throws UserNotFound |
|
38
|
|
|
*/ |
|
39
|
|
|
public User getUser(String user_id) throws UserNotFound { |
|
40
|
|
|
try { |
|
41
|
|
|
Response response = this.base.sendRequest("/users/".concat(user_id), Request.METHOD_GET); |
|
42
|
|
|
return (User) Base.makeModel(User.class, response.getBody()); |
|
43
|
|
|
} catch (BaseHttpException e) { |
|
44
|
|
|
throw new UserNotFound(user_id); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get Currently Logged in User. |
|
50
|
|
|
* |
|
51
|
|
|
* @return |
|
52
|
|
|
* @throws UserNotFound |
|
53
|
|
|
*/ |
|
54
|
|
|
public User getCurrentUser() throws UserNotFound { |
|
55
|
|
|
return this.getUser("me"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public User createUser(String name, String email, String password) throws BaseHttpException { |
|
59
|
|
|
Map<String, String> parameters = new HashMap<>(); |
|
60
|
|
|
parameters.put("name", name); |
|
61
|
|
|
parameters.put("email", email); |
|
62
|
|
|
parameters.put("password", password); |
|
63
|
|
|
parameters.put("password_confirmation", password); |
|
64
|
|
|
|
|
65
|
|
|
Response response = this.base.sendRequest("/users", Request.METHOD_POST, parameters); |
|
66
|
|
|
return (User) Base.makeModel(User.class, response.getBody()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Update a User. |
|
71
|
|
|
* |
|
72
|
|
|
* @param name Name |
|
73
|
|
|
* @param email Email |
|
74
|
|
|
* @param password Password |
|
75
|
|
|
* @return Updated User |
|
76
|
|
|
* @throws BaseHttpException Exception. |
|
77
|
|
|
*/ |
|
78
|
|
|
public User updateUser(String name, String email, String password) throws BaseHttpException { |
|
79
|
|
|
Map<String, String> parameters = new HashMap<>(); |
|
80
|
|
|
|
|
81
|
|
|
if (!name.isEmpty()) { |
|
82
|
|
|
parameters.put("name", name); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (!email.isEmpty()) { |
|
86
|
|
|
parameters.put("email", email); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (!password.isEmpty()) { |
|
90
|
|
|
parameters.put("password", password); |
|
91
|
|
|
parameters.put("password_confirmation", password); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
Response response = this.base.sendRequest("/users/me", Request.METHOD_PATCH, parameters); |
|
95
|
|
|
System.out.println(response.getBody()); |
|
96
|
|
|
return (User) Base.makeModel(User.class, response.getBody()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Upload Current User's Profile Picture. |
|
102
|
|
|
* |
|
103
|
|
|
* @param picture |
|
104
|
|
|
* @return |
|
105
|
|
|
* @throws BaseHttpException |
|
106
|
|
|
*/ |
|
107
|
|
|
public Media uploadProfilePicture(File picture) throws BaseHttpException { |
|
108
|
|
|
Map<String, String> parameters = new HashMap<>(); |
|
109
|
|
|
|
|
110
|
|
|
Map<String, File> files = new HashMap<>(); |
|
111
|
|
|
files.put("file", picture); |
|
112
|
|
|
|
|
113
|
|
|
Response response = this.base.sendRequest("/users/picture", Request.METHOD_POST, parameters, new HashMap<>(), |
|
114
|
|
|
files); |
|
115
|
|
|
return (Media) Base.makeModel(Media.class, response.getBody()); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|