updateChannelThread(String,String,String,String,String)   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
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.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.Thread;
12
13
import java.util.*;
14
15
public class ThreadService {
16
17
    /**
18
     * {@link Base}
19
     */
20
    private Base base;
21
22
    /**
23
     * Construct Instance of Base Class
24
     *
25
     * @param base
26
     */
27
    public ThreadService(Base base) {
28
        this.base = base;
29
    }
30
31
    /**
32
     * Create Thread Messages
33
     *
34
     * @param teamSlug    Team Slug Name
35
     * @param channelSlug Channel Slug Name
36
     * @param subject     Thread subject
37
     * @param description Thread description
38
     * @return Thread
39
     * @throws BaseHttpException Exception
40
     * @throws ChannelNotFound   Exception
41
     */
42
    public Thread addChannelThread(String teamSlug, String channelSlug, String subject, String description) throws BaseHttpException, ChannelNotFound {
43
        Map<String, String> parameters = new HashMap<>();
44
        parameters.put("subject", subject);
45
        parameters.put("description", description);
46
        try {
47
            String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads");
48
            Response response = this.base.sendRequest(URL, Request.METHOD_POST, parameters);
49
            return (Thread) Base.makeModel(Thread.class, response.getBody());
50
        } catch (NotFound e) {
51
            throw new ChannelNotFound(channelSlug);
52
        }
53
    }
54
55
    /**
56
     * List of All Channels Threads
57
     *
58
     * @param teamSlug    Team Slug
59
     * @param channelSlug Channel Slug
60
     * @param page        Page number
61
     * @param limit       Limit Value
62
     * @return List of Threads
63
     * @throws ChannelNotFound   Exception
64
     * @throws BaseHttpException Exception
65
     */
66
    public List<Thread> getAllChannelThreads(String teamSlug, String channelSlug, int page, int limit) throws ChannelNotFound, BaseHttpException {
67
68
        ArrayList<String> parameters = new ArrayList<>();
69
70
        if (page != 0) {
71
            parameters.add("page=".concat(Integer.toString(page)));
72
        }
73
        if (limit != 0) {
74
            parameters.add("limit=".concat(Integer.toString(limit)));
75
        }
76
77
        String URL = Helpers.buildUrlWithQuery("/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads"), parameters);
78
        try {
79
            Response response = this.base.sendRequest(URL, Request.METHOD_GET);
80
            Thread[] threadsArray = (Thread[]) Base.makeModel(Thread[].class, response.getBody());
81
            return new ArrayList<>(Arrays.asList(threadsArray));
82
        } catch (NotFound e) {
83
            throw new ChannelNotFound(channelSlug);
84
        }
85
    }
86
87
    /**
88
     * List of All Channels Threads
89
     *
90
     * @param teamSlug    Team Slug
91
     * @param channelSlug Channel Slug
92
     * @param page        Page number
93
     * @return List of Threads
94
     * @throws ChannelNotFound   Exception
95
     * @throws BaseHttpException Exception
96
     */
97
    public List<Thread> getAllChannelThreads(String teamSlug, String channelSlug, int page) throws ChannelNotFound, BaseHttpException {
98
        return getAllChannelThreads(teamSlug, channelSlug, page, 0);
99
    }
100
101
    /**
102
     * List of All Channels Threads
103
     *
104
     * @param teamSlug    Team Slug
105
     * @param channelSlug Channel Slug
106
     * @return List of Threads
107
     * @throws ChannelNotFound   Exception
108
     * @throws BaseHttpException Exception
109
     */
110
    public List<Thread> getAllChannelThreads(String teamSlug, String channelSlug) throws ChannelNotFound, BaseHttpException {
111
        return getAllChannelThreads(teamSlug, channelSlug, 0, 0);
112
    }
113
114
115
    /**
116
     * Show Channel's Thread
117
     *
118
     * @param teamSlug    Team Slug Name
119
     * @param channelSlug Channel Slug Name
120
     * @param threadSlug  Thread Slug Name
121
     * @return Thread
122
     * @throws ThreadNotFound
123
     * @throws BaseHttpException
124
     */
125
    public Thread getChannelThread(String teamSlug, String channelSlug, String threadSlug) throws ThreadNotFound, BaseHttpException {
126
        try {
127
            String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug);
128
            Response response = this.base.sendRequest(URL, Request.METHOD_GET);
129
            return (Thread) Base.makeModel(Thread.class, response.getBody());
130
        } catch (NotFound e) {
131
            throw new ThreadNotFound(threadSlug);
132
        }
133
    }
134
135
    /**
136
     * Delete Thread Channel
137
     *
138
     * @param teamSlug    Team Slug Name
139
     * @param channelSlug Channel Slug
140
     * @param threadSlug  Thread Slug Name
141
     * @throws ThreadNotFound    Exception
142
     * @throws BaseHttpException Exception
143
     */
144
    public boolean deleteChannelThread(String teamSlug, String channelSlug, String threadSlug) throws ThreadNotFound, BaseHttpException {
145
146
        try {
147
            String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug);
148
            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...
149
            return true;
150
        } catch (NotFound e) {
151
            throw new ThreadNotFound(threadSlug);
152
        }
153
    }
154
155
156
    /**
157
     * Update Channel Thread
158
     *
159
     * @param teamSlug          Team Slug
160
     * @param channelSlug       Channel Slug
161
     * @param threadSlug        Thread Slug
162
     * @param threadSubject     Thread Subject
163
     * @param threadDescription Thread Description
164
     * @return Thread
165
     * @throws BaseHttpException
166
     */
167
    public Thread updateChannelThread(String teamSlug, String channelSlug, String threadSlug, String threadSubject, String threadDescription) throws BaseHttpException, ThreadNotFound {
168
        Map<String, String> parameters = new HashMap<>();
169
        if (!threadSubject.isEmpty()) {
170
            parameters.put("subject", threadSubject);
171
        }
172
173
        if (!threadDescription.isEmpty()) {
174
            parameters.put("description", threadDescription);
175
        }
176
177
        String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug);
178
        try {
179
            Response response = this.base.sendRequest(URL, Request.METHOD_PATCH, parameters);
180
            return (Thread) Base.makeModel(Thread.class, response.getBody());
181
        } catch (NotFound e) {
182
            throw new ThreadNotFound(threadSlug);
183
        }
184
185
    }
186
187
}
188