1
|
|
|
package com.base.Services; |
2
|
|
|
|
3
|
|
|
import com.base.Base; |
4
|
|
|
import com.base.Exceptions.BaseHttpException; |
5
|
|
|
import com.base.Exceptions.ChannelNotFound; |
6
|
|
|
import com.base.Exceptions.Http.NotFound; |
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 ChannelMemberService { |
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 ChannelMemberService(Base base) { |
27
|
|
|
this.base = base; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Add Member to Channel |
32
|
|
|
* |
33
|
|
|
* @param teamSlug Team Slug |
34
|
|
|
* @param channelSlug Channel Slug |
35
|
|
|
* @param user_id User Id |
36
|
|
|
* @return Boolean True or False |
37
|
|
|
* @throws BaseHttpException Exception |
38
|
|
|
* @throws ChannelNotFound Exception |
39
|
|
|
*/ |
40
|
|
|
public boolean addChannelMember(String teamSlug, String channelSlug, String user_id) throws BaseHttpException, ChannelNotFound { |
41
|
|
|
Map<String, String> parameters = new HashMap<>(); |
42
|
|
|
parameters.put("user_id", user_id); |
43
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/members"); |
44
|
|
|
try { |
45
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_POST, parameters); |
|
|
|
|
46
|
|
|
return true; |
47
|
|
|
} catch (NotFound e) { |
48
|
|
|
throw new ChannelNotFound(channelSlug); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Show Member By Channel |
54
|
|
|
* |
55
|
|
|
* @param teamSlug Team Slug |
56
|
|
|
* @param channelSlug Channel Slug |
57
|
|
|
* @param user_id User Id |
58
|
|
|
* @return User |
59
|
|
|
* @throws ChannelNotFound Exception |
60
|
|
|
* @throws BaseHttpException Exception |
61
|
|
|
*/ |
62
|
|
|
public User getChannelMember(String teamSlug, String channelSlug, String user_id) throws ChannelNotFound, BaseHttpException { |
63
|
|
|
try { |
64
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/members/").concat(user_id); |
65
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_GET); |
66
|
|
|
return (User) Base.makeModel(User.class, response.getBody()); |
67
|
|
|
} catch (NotFound e) { |
68
|
|
|
throw new ChannelNotFound(channelSlug); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Show All Users in Channel |
74
|
|
|
* |
75
|
|
|
* @param teamSlug Slug Name |
76
|
|
|
* @param channelSlug Channel Slug |
77
|
|
|
* @param page Page number |
78
|
|
|
* @param limit Limit Value |
79
|
|
|
* @return List of Users |
80
|
|
|
* @throws ChannelNotFound Exception |
81
|
|
|
* @throws BaseHttpException Exception |
82
|
|
|
*/ |
83
|
|
|
public List<User> getAllChannelMembers(String teamSlug, String channelSlug, int page, int limit) throws ChannelNotFound, BaseHttpException { |
84
|
|
|
|
85
|
|
|
ArrayList<String> parameters = new ArrayList<>(); |
86
|
|
|
|
87
|
|
|
if (page != 0) { |
88
|
|
|
parameters.add("page=".concat(Integer.toString(page))); |
89
|
|
|
} |
90
|
|
|
if (limit != 0) { |
91
|
|
|
parameters.add("limit=".concat(Integer.toString(limit))); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
String URL = Helpers.buildUrlWithQuery("/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/members"), parameters); |
96
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_GET); |
97
|
|
|
User[] usersArray = (User[]) Base.makeModel(User[].class, response.getBody()); |
98
|
|
|
return new ArrayList<>(Arrays.asList(usersArray)); |
99
|
|
|
} catch (NotFound e) { |
100
|
|
|
throw new ChannelNotFound(channelSlug); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Show All Users in Channel |
106
|
|
|
* |
107
|
|
|
* @param teamSlug Slug Name |
108
|
|
|
* @param channelSlug Channel Slug |
109
|
|
|
* @param page Page number |
110
|
|
|
* @return List of Users |
111
|
|
|
* @throws ChannelNotFound Exception |
112
|
|
|
* @throws BaseHttpException Exception |
113
|
|
|
*/ |
114
|
|
|
public List<User> getAllChannelMembers(String teamSlug, String channelSlug, int page) throws ChannelNotFound, BaseHttpException { |
115
|
|
|
return getAllChannelMembers(teamSlug, channelSlug, page, 0); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Show All Users in Channel |
120
|
|
|
* |
121
|
|
|
* @param teamSlug Slug Name |
122
|
|
|
* @param channelSlug Channel Slug |
123
|
|
|
* @return List of Users |
124
|
|
|
* @throws ChannelNotFound Exception |
125
|
|
|
* @throws BaseHttpException Exception |
126
|
|
|
*/ |
127
|
|
|
public List<User> getAllChannelMembers(String teamSlug, String channelSlug) throws ChannelNotFound, BaseHttpException { |
128
|
|
|
return getAllChannelMembers(teamSlug, channelSlug, 0, 0); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Delete Channel Member |
133
|
|
|
* |
134
|
|
|
* @param teamSlug Team Slug |
135
|
|
|
* @param channelSlug Channel Slug |
136
|
|
|
* @param user_id User Id |
137
|
|
|
* @throws BaseHttpException Exception |
138
|
|
|
* @throws ChannelNotFound Exception |
139
|
|
|
*/ |
140
|
|
|
public boolean deleteChannelMember(String teamSlug, String channelSlug, String user_id) throws BaseHttpException, ChannelNotFound { |
141
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/members/").concat(user_id); |
142
|
|
|
try { |
143
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_DELETE); |
|
|
|
|
144
|
|
|
return true; |
145
|
|
|
} catch (NotFound e) { |
146
|
|
|
throw new ChannelNotFound(channelSlug); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|