Completed
Push — master ( cd072b...050b3c )
by Kunal
02:55
created

createMessage(String,String,String,String,String,int[])   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.4285
c 1
b 0
f 0
cc 3
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.MessageNotFound;
7
import com.base.Exceptions.ThreadNotFound;
8
import com.base.Helpers;
9
import com.base.Http.Request.Request;
10
import com.base.Http.Response.Response;
11
import com.base.Models.Message;
12
13
import java.io.File;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'java.io.File'.
Loading history...
14
import java.util.*;
15
16
public class MessageService {
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 MessageService(Base base) {
29
        this.base = base;
30
    }
31
32
    /**
33
     * Create Message of Thread
34
     *
35
     * @param teamSlug    Message teamSlug
36
     * @param channelSlug Message channelSlug
37
     * @param threadSlug  Message threadSlug
38
     * @param content     Message content
39
     * @param type        Message type
40
     * @return Message
41
     * @throws ThreadNotFound
42
     * @throws BaseHttpException
43
     */
44
    public Message createMessage(String teamSlug, String channelSlug, String threadSlug, String content, String type) throws ThreadNotFound, BaseHttpException {
45
        int[] fileIds = {};
46
        return this.createMessage(teamSlug, channelSlug, threadSlug, content, type, fileIds);
47
    }
48
49
    /**
50
     * Create Message of Thread
51
     *
52
     * @param teamSlug    Message teamSlug
53
     * @param channelSlug Message channelSlug
54
     * @param threadSlug  Message threadSlug
55
     * @param content     Message content
56
     * @param type        Message type
57
     * @param mediaIds    Message Media IDs
58
     * @return Message
59
     * @throws ThreadNotFound
60
     * @throws BaseHttpException
61
     */
62
    public Message createMessage(String teamSlug, String channelSlug, String threadSlug, String content, String type, int[] mediaIds) throws ThreadNotFound, BaseHttpException {
63
        HashMap<String, String> parameters = new HashMap<>();
64
65
        parameters.put("content", content);
66
        parameters.put("type", type);
67
68
        for (int i = 0; i < mediaIds.length; i++) {
69
            parameters.put("media_ids*" + i, String.valueOf(mediaIds[i]));
70
        }
71
72
        try {
73
            String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").
74
                    concat(threadSlug).concat("/messages");
75
            Response response = this.base.sendRequest(URL, Request.METHOD_POST, parameters);
76
            return (Message) Base.makeModel(Message.class, response.getBody());
77
        } catch (NotFound e) {
78
            throw new ThreadNotFound(teamSlug);
79
        }
80
    }
81
82
    /**
83
     * List of all thread messages
84
     *
85
     * @param teamSlug    Team Slug
86
     * @param channelSlug Channel Slug
87
     * @param threadSlug  Thread Slug
88
     * @param page        Page number
89
     * @param limit       Limit Value
90
     * @return List of Messages
91
     * @throws ThreadNotFound    Exception
92
     * @throws BaseHttpException Exception
93
     */
94
    public List<Message> getAllMessages(String teamSlug, String channelSlug, String threadSlug, int page, int limit) throws ThreadNotFound, BaseHttpException {
95
96
        ArrayList<String> parameters = new ArrayList<>();
97
98
        if (page != 0) {
99
            parameters.add("page=".concat(Integer.toString(page)));
100
        }
101
        if (limit != 0) {
102
            parameters.add("limit=".concat(Integer.toString(limit)));
103
        }
104
        String URL = Helpers.buildUrlWithQuery("/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug)
105
                .concat("/messages/"), parameters);
106
107
        try {
108
            Response response = this.base.sendRequest(URL, Request.METHOD_GET);
109
            Message[] messagesArray = (Message[]) Base.makeModel(Message[].class, response.getBody());
110
            return new ArrayList<>(Arrays.asList(messagesArray));
111
        } catch (NotFound e) {
112
            throw new ThreadNotFound(threadSlug);
113
        }
114
    }
115
116
    /**
117
     * List of all thread messages
118
     *
119
     * @param teamSlug    Team Slug
120
     * @param channelSlug Channel Slug
121
     * @param threadSlug  Thread Slug
122
     * @param page        Page number
123
     * @return List of Messages
124
     * @throws ThreadNotFound    Exception
125
     * @throws BaseHttpException Exception
126
     */
127
    public List<Message> getAllMessages(String teamSlug, String channelSlug, String threadSlug, int page) throws ThreadNotFound, BaseHttpException {
128
        return getAllMessages(teamSlug, channelSlug, threadSlug, page, 0);
129
    }
130
131
    /**
132
     * List of all thread messages
133
     *
134
     * @param teamSlug    Team Slug
135
     * @param channelSlug Channel Slug
136
     * @param threadSlug  Thread Slug
137
     * @return List of Messages
138
     * @throws ThreadNotFound    Exception
139
     * @throws BaseHttpException Exception
140
     */
141
    public List<Message> getAllMessages(String teamSlug, String channelSlug, String threadSlug) throws ThreadNotFound, BaseHttpException {
142
        return getAllMessages(teamSlug, channelSlug, threadSlug, 0, 0);
143
    }
144
145
    /**
146
     * Show thread message
147
     *
148
     * @param teamSlug
149
     * @param channelSlug
150
     * @param threadSlug
151
     * @param messageSlug
152
     * @return Message
153
     * @throws MessageNotFound   Exception
154
     * @throws BaseHttpException Exception
155
     */
156
    public Message getMessage(String teamSlug, String channelSlug, String threadSlug, String messageSlug) throws MessageNotFound, BaseHttpException {
157
        try {
158
            String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug)
159
                    .concat("/messages/").concat(messageSlug);
160
            Response response = this.base.sendRequest(URL, Request.METHOD_GET);
161
            return (Message) Base.makeModel(Message.class, response.getBody());
162
        } catch (NotFound e) {
163
            throw new MessageNotFound(messageSlug);
164
        }
165
    }
166
167
    /**
168
     * Update thread Message
169
     *
170
     * @param teamSlug
171
     * @param channelSlug
172
     * @param threadSlug
173
     * @param messageSlug
174
     * @param messageContent
175
     * @param type
176
     * @return Message
177
     * @throws BaseHttpException Exception
178
     * @throws MessageNotFound   Exception
179
     */
180
    public Message updateMessage(String teamSlug, String channelSlug, String threadSlug, String messageSlug, String messageContent, String type) throws BaseHttpException, MessageNotFound {
181
        Map<String, String> parameters = new HashMap<>();
182
        if (!messageContent.isEmpty()) {
183
            parameters.put("content", messageContent);
184
        }
185
186
        if (!type.isEmpty()) {
187
            parameters.put("type", type);
188
        }
189
190
        try {
191
            String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug)
192
                    .concat("/messages/").concat(messageSlug);
193
            Response response = this.base.sendRequest(URL, Request.METHOD_PATCH, parameters);
194
            return (Message) Base.makeModel(Message.class, response.getBody());
195
        } catch (NotFound e) {
196
            throw new MessageNotFound(messageSlug);
197
        }
198
199
    }
200
201
    /**
202
     * Delete Message
203
     *
204
     * @param teamSlug
205
     * @param channelSlug
206
     * @param threadSlug
207
     * @param messageSlug
208
     * @return boolean value
209
     * @throws BaseHttpException Exception
210
     * @throws MessageNotFound   Exception
211
     */
212
    public boolean deleteMessage(String teamSlug, String channelSlug, String threadSlug, String messageSlug) throws BaseHttpException, MessageNotFound {
213
214
        try {
215
            String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug).concat("/messages/").concat(messageSlug);
216
            Response response = this.base.sendRequest(URL, Request.METHOD_DELETE);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable response.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "response".
Loading history...
217
            return true;
218
        } catch (NotFound e) {
219
            throw new MessageNotFound(messageSlug);
220
        }
221
    }
222
223
    /**
224
     * Star Thread Message
225
     *
226
     * @param teamSlug
227
     * @param channelSlug
228
     * @param threadSlug
229
     * @param messageSlug
230
     * @return boolean Value
231
     * @throws BaseHttpException Exception
232
     * @throws MessageNotFound   Exception
233
     */
234
    public boolean starThreadMessage(String teamSlug, String channelSlug, String threadSlug, String messageSlug) throws BaseHttpException, MessageNotFound {
235
236
        try {
237
            String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug).concat("/messages/").concat(messageSlug).concat("/star");
238
            Response response = this.base.sendRequest(URL, Request.METHOD_POST);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable response.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "response".
Loading history...
239
            return true;
240
        } catch (NotFound e) {
241
            throw new MessageNotFound(messageSlug);
242
        }
243
    }
244
245
    /**
246
     * UnStar Thread Message
247
     *
248
     * @param teamSlug
249
     * @param channelSlug
250
     * @param threadSlug
251
     * @param messageSlug
252
     * @return boolean Value
253
     * @throws BaseHttpException Exception
254
     * @throws MessageNotFound   Exception
255
     */
256
    public boolean unStarThreadMessage(String teamSlug, String channelSlug, String threadSlug, String messageSlug) throws BaseHttpException, MessageNotFound {
257
258
        try {
259
            String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug).concat("/messages/").concat(messageSlug).concat("/star");
260
            Response response = this.base.sendRequest(URL, Request.METHOD_DELETE);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable response.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "response".
Loading history...
261
            return true;
262
        } catch (NotFound e) {
263
            throw new MessageNotFound(messageSlug);
264
        }
265
    }
266
267
}
268