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.Exceptions.TeamNotFound; |
||
8 | import com.base.Helpers; |
||
9 | import com.base.Http.Request.Request; |
||
10 | import com.base.Http.Response.Response; |
||
11 | import com.base.Models.Channel; |
||
12 | import com.base.Models.Media; |
||
13 | |||
14 | import java.io.File; |
||
15 | import java.util.*; |
||
16 | |||
17 | public class ChannelService { |
||
18 | |||
19 | /** |
||
20 | * {@link Base} |
||
21 | */ |
||
22 | private Base base; |
||
23 | |||
24 | /** |
||
25 | * Construct Instance of Base Class |
||
26 | * |
||
27 | * @param base |
||
28 | */ |
||
29 | public ChannelService(Base base) { |
||
30 | this.base = base; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Create channel by slug name of team |
||
35 | * |
||
36 | * @param teamSlug |
||
37 | * @param name |
||
38 | * @param description |
||
39 | * @param color |
||
40 | * @param is_private |
||
41 | * @return Created Channel |
||
42 | * @throws BaseHttpException |
||
43 | */ |
||
44 | public Channel createChannel(String teamSlug, String name, String description, String color, boolean is_private) throws BaseHttpException, TeamNotFound { |
||
45 | Map<String, String> parameters = new HashMap<>(); |
||
46 | parameters.put("name", name); |
||
47 | parameters.put("description", description); |
||
48 | parameters.put("color", color); |
||
49 | parameters.put("is_private", (is_private ? "1" : "0")); |
||
50 | try { |
||
51 | Response response = this.base.sendRequest("/teams/".concat(teamSlug).concat("/channels"), Request.METHOD_POST, parameters); |
||
52 | return (Channel) Base.makeModel(Channel.class, response.getBody()); |
||
53 | } catch (NotFound e) { |
||
54 | throw new TeamNotFound(teamSlug); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Get All Channels By Slug |
||
60 | * |
||
61 | * @param teamSlug Slug of Team |
||
62 | * @param page Page number |
||
63 | * @param limit Limit Value |
||
64 | * @return List of Channel in slug |
||
65 | * @throws TeamNotFound Exception |
||
66 | * @throws BaseHttpException Exception |
||
67 | */ |
||
68 | public List<Channel> getAllChannels(String teamSlug, int page, int limit) throws TeamNotFound, BaseHttpException { |
||
69 | ArrayList<String> parameters = new ArrayList<>(); |
||
70 | |||
71 | if (page != 0) { |
||
72 | parameters.add("page=".concat(Integer.toString(page))); |
||
73 | } |
||
74 | if (limit != 0) { |
||
75 | parameters.add("limit=".concat(Integer.toString(limit))); |
||
76 | } |
||
77 | |||
78 | String URL = Helpers.buildUrlWithQuery("/teams/".concat(teamSlug).concat("/channels"), parameters); |
||
79 | |||
80 | try { |
||
81 | Response response = base.sendRequest(URL, Request.METHOD_GET); |
||
82 | Channel[] channelArray = (Channel[]) Base.makeModel(Channel[].class, response.getBody()); |
||
83 | return new ArrayList<>(Arrays.asList(channelArray)); |
||
84 | } catch (NotFound e) { |
||
85 | throw new TeamNotFound(teamSlug); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get All Channels By Slug |
||
91 | * |
||
92 | * @param teamSlug Slug of Team |
||
93 | * @param page Page number |
||
94 | * @return List of Channel in slug |
||
95 | * @throws TeamNotFound Exception |
||
96 | * @throws BaseHttpException Exception |
||
97 | */ |
||
98 | public List<Channel> getAllChannels(String teamSlug, int page) throws TeamNotFound, BaseHttpException { |
||
99 | return getAllChannels(teamSlug, page, 0); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Get All Channels By Slug |
||
104 | * |
||
105 | * @param teamSlug Slug of Team |
||
106 | * @return List of Channel in slug |
||
107 | * @throws TeamNotFound Exception |
||
108 | * @throws BaseHttpException Exception |
||
109 | */ |
||
110 | public List<Channel> getAllChannels(String teamSlug) throws TeamNotFound, BaseHttpException { |
||
111 | return getAllChannels(teamSlug, 0, 0); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Delete Channel by Slug |
||
116 | * |
||
117 | * @param teamSlug |
||
118 | * @param channelSlug |
||
119 | * @throws ChannelNotFound |
||
120 | */ |
||
121 | public boolean deleteChannel(String teamSlug, String channelSlug) throws ChannelNotFound { |
||
122 | try { |
||
123 | Response response = this.base.sendRequest("/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug), Request.METHOD_DELETE); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
124 | return true; |
||
125 | } catch (BaseHttpException e) { |
||
126 | throw new ChannelNotFound(channelSlug); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Update Channel |
||
132 | * |
||
133 | * @param teamSlug Channel teamSlug |
||
134 | * @param channelSlug Channel channelSlug |
||
135 | * @param name Channel name |
||
136 | * @param description Channel description |
||
137 | * @param color Channel color |
||
138 | * @param is_private Channel is_private |
||
139 | * @return Updated Channel |
||
140 | * @throws BaseHttpException |
||
141 | */ |
||
142 | public Channel updateChannel(String teamSlug, String channelSlug, String name, String description, String color, boolean is_private) throws BaseHttpException, ChannelNotFound { |
||
143 | Map<String, String> parameters = new HashMap<>(); |
||
144 | |||
145 | if (!name.isEmpty()) { |
||
146 | parameters.put("name", name); |
||
147 | } |
||
148 | |||
149 | if (!description.isEmpty()) { |
||
150 | parameters.put("description", description); |
||
151 | } |
||
152 | |||
153 | if (!color.isEmpty()) { |
||
154 | parameters.put("color", color); |
||
155 | } |
||
156 | |||
157 | parameters.put("is_private", (is_private ? "1" : "0")); |
||
158 | |||
159 | String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug); |
||
160 | |||
161 | try { |
||
162 | Response response = this.base.sendRequest(URL, Request.METHOD_PATCH, parameters); |
||
163 | return (Channel) Base.makeModel(Channel.class, response.getBody()); |
||
164 | } catch (NotFound e) { |
||
165 | throw new ChannelNotFound(channelSlug); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Get Channel By Name |
||
171 | * |
||
172 | * @param teamSlug |
||
173 | * @param channelSlug |
||
174 | * @return Channel |
||
175 | * @throws ChannelNotFound |
||
176 | * @throws BaseHttpException |
||
177 | */ |
||
178 | public Channel getChannel(String teamSlug, String channelSlug) throws ChannelNotFound, BaseHttpException { |
||
179 | try { |
||
180 | String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug); |
||
181 | Response response = base.sendRequest(URL, Request.METHOD_GET); |
||
182 | return (Channel) Base.makeModel(Channel.class, response.getBody()); |
||
183 | } catch (NotFound e) { |
||
184 | throw new ChannelNotFound(teamSlug); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | public Media[] uploadMedia(String teamSlug, String channelSlug, File[] files) throws ChannelNotFound { |
||
189 | Map<String, String> parameters = new HashMap<>(); |
||
190 | |||
191 | Map<String, File> requestFiles = new HashMap<>(); |
||
192 | |||
193 | for (int i = 0; i < files.length; i++) { |
||
194 | requestFiles.put("files*" + i, files[i]); |
||
195 | } |
||
196 | |||
197 | Response response = null; |
||
198 | try { |
||
199 | String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/media"); |
||
200 | response = this.base.sendRequest(URL, Request.METHOD_POST, parameters, new HashMap<>(), |
||
201 | requestFiles); |
||
202 | } catch (NotFound e) { |
||
203 | throw new ChannelNotFound(teamSlug); |
||
204 | } catch (BaseHttpException e) { |
||
205 | e.printStackTrace(); |
||
0 ignored issues
–
show
|
|||
206 | } |
||
207 | return (Media[]) Base.makeModel(Media[].class, response.getBody()); |
||
0 ignored issues
–
show
|
|||
208 | } |
||
209 | |||
210 | } |
||
211 |