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.TeamNotFound; |
7
|
|
|
import com.base.Helpers; |
8
|
|
|
import com.base.Http.Request.Request; |
9
|
|
|
import com.base.Http.Response.Response; |
10
|
|
|
import com.base.Models.User; |
11
|
|
|
|
12
|
|
|
import java.util.*; |
13
|
|
|
|
14
|
|
|
public class TeamMemberService { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@link Base} |
18
|
|
|
*/ |
19
|
|
|
private Base base; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Construct Instance of Base Class |
23
|
|
|
* |
24
|
|
|
* @param base |
25
|
|
|
*/ |
26
|
|
|
public TeamMemberService(Base base) { |
27
|
|
|
this.base = base; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get User of Team by Slug of Team |
32
|
|
|
* |
33
|
|
|
* @param teamSlug Team of Slug |
34
|
|
|
* @param user_id User Id |
35
|
|
|
* @return User |
36
|
|
|
* @throws TeamNotFound |
37
|
|
|
* @throws BaseHttpException |
38
|
|
|
*/ |
39
|
|
|
public User getTeamMember(String teamSlug, String user_id) throws TeamNotFound, BaseHttpException { |
40
|
|
|
try { |
41
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/members/").concat(user_id); |
42
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_GET); |
43
|
|
|
return (User) Base.makeModel(User.class, response.getBody()); |
44
|
|
|
} catch (NotFound e) { |
45
|
|
|
throw new TeamNotFound(teamSlug); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Show All Members in Team |
51
|
|
|
* |
52
|
|
|
* @param teamSlug Team Slug Name |
53
|
|
|
* @param page Page number |
54
|
|
|
* @param limit Limit Value |
55
|
|
|
* @return List of Users |
56
|
|
|
* @throws TeamNotFound Exception |
57
|
|
|
*/ |
58
|
|
|
public List<User> getAllTeamMembers(String teamSlug, int page, int limit) throws TeamNotFound, BaseHttpException { |
59
|
|
|
|
60
|
|
|
ArrayList<String> parameters = new ArrayList<>(); |
61
|
|
|
|
62
|
|
|
if (page != 0) { |
63
|
|
|
parameters.add("page=".concat(Integer.toString(page))); |
64
|
|
|
} |
65
|
|
|
if (limit != 0) { |
66
|
|
|
parameters.add("limit=".concat(Integer.toString(limit))); |
67
|
|
|
} |
68
|
|
|
String URL = Helpers.buildUrlWithQuery("/teams/".concat(teamSlug).concat("/members"), parameters); |
69
|
|
|
try { |
70
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_GET); |
71
|
|
|
User[] usersArray = (User[]) Base.makeModel(User[].class, response.getBody()); |
72
|
|
|
return new ArrayList<>(Arrays.asList(usersArray)); |
73
|
|
|
} catch (NotFound e) { |
74
|
|
|
throw new TeamNotFound(teamSlug); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Show All Members in Team |
80
|
|
|
* |
81
|
|
|
* @param teamSlug Team Slug Name |
82
|
|
|
* @return List of Users |
83
|
|
|
* @throws TeamNotFound |
84
|
|
|
* @throws BaseHttpException |
85
|
|
|
*/ |
86
|
|
|
public List<User> getAllTeamMembers(String teamSlug) throws TeamNotFound, BaseHttpException { |
87
|
|
|
return getAllTeamMembers(teamSlug, 0, 0); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Show All Members in Team |
92
|
|
|
* |
93
|
|
|
* @param teamSlug Team Slug Name |
94
|
|
|
* @param page Page number |
95
|
|
|
* @return List of Users |
96
|
|
|
* @throws TeamNotFound |
97
|
|
|
* @throws BaseHttpException |
98
|
|
|
*/ |
99
|
|
|
public List<User> getAllTeamMembers(String teamSlug, int page) throws TeamNotFound, BaseHttpException { |
100
|
|
|
return getAllTeamMembers(teamSlug, page, 0); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Delete Team Members by Team Slug Name |
105
|
|
|
* |
106
|
|
|
* @param teamSlug Name of Slug |
107
|
|
|
* @param user_id Id of User |
108
|
|
|
* @return True or False |
109
|
|
|
* @throws BaseHttpException Exception |
110
|
|
|
* @throws TeamNotFound Exception |
111
|
|
|
*/ |
112
|
|
|
public boolean deleteTeamMember(String teamSlug, String user_id) throws BaseHttpException, TeamNotFound { |
113
|
|
|
try { |
114
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/members/").concat(user_id); |
115
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_DELETE); |
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} catch (NotFound e) { |
118
|
|
|
throw new TeamNotFound(teamSlug); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Add Member into Team |
124
|
|
|
* |
125
|
|
|
* @param teamSlug Team Slug Name |
126
|
|
|
* @param user_id User Id |
127
|
|
|
* @return Boolean Value True or False |
128
|
|
|
* @throws BaseHttpException Exception |
129
|
|
|
* @throws TeamNotFound Exception |
130
|
|
|
*/ |
131
|
|
|
public boolean addTeamMember(String teamSlug, String user_id) throws BaseHttpException, TeamNotFound { |
132
|
|
|
Map<String, String> parameters = new HashMap<>(); |
133
|
|
|
parameters.put("user_id", user_id); |
134
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/members"); |
135
|
|
|
try { |
136
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_POST, parameters); |
|
|
|
|
137
|
|
|
return true; |
138
|
|
|
} catch (NotFound e) { |
139
|
|
|
throw new TeamNotFound(teamSlug); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|