Completed
Push — master ( bbac12...7fcfab )
by Kunal
03:42
created

uploadTeamPicture(String,File)   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
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.Media;
11
import com.base.Models.Message;
12
import com.base.Models.Team;
13
14
import java.io.File;
15
import java.util.*;
16
17
public class TeamService {
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 TeamService(Base base) {
30
        this.base = base;
31
    }
32
33
    /**
34
     * Create New Team
35
     *
36
     * @param name        Team name
37
     * @param description Team Description
38
     * @return Created Team
39
     * @throws BaseHttpException Exception
40
     */
41
    public Team createTeam(String name, String description) throws BaseHttpException {
42
        Map<String, String> parameters = new HashMap<>();
43
        parameters.put("name", name);
44
        parameters.put("description", description);
45
46
        Response response = this.base.sendRequest("/teams", Request.METHOD_POST, parameters);
47
        return (Team) Base.makeModel(Team.class, response.getBody());
48
    }
49
50
    /**
51
     * Get Team By Slug.
52
     *
53
     * @param teamSlug
54
     * @return Team
55
     * @throws TeamNotFound
56
     */
57
    public Team getTeam(String teamSlug) throws TeamNotFound, BaseHttpException {
58
        try {
59
            Response response = this.base.sendRequest("/teams/".concat(teamSlug), Request.METHOD_GET);
60
            return (Team) Base.makeModel(Team.class, response.getBody());
61
        } catch (NotFound e) {
62
            throw new TeamNotFound(teamSlug);
63
        }
64
    }
65
66
67
    /**
68
     * Update a Team
69
     *
70
     * @param name        Team Name
71
     * @param description Team Description
72
     * @param teamSlug    slug name of team
73
     * @return Updated Team
74
     * @throws BaseHttpException Exception
75
     */
76
    public Team updateTeam(String teamSlug, String name, String description) throws BaseHttpException {
77
        Map<String, String> parameters = new HashMap<>();
78
79
80
        if (!name.isEmpty()) {
81
            parameters.put("name", name);
82
        }
83
84
        if (!description.isEmpty()) {
85
            parameters.put("description", description);
86
        }
87
88
        Response response = this.base.sendRequest("/teams/".concat(teamSlug), Request.METHOD_PATCH, parameters);
89
        return (Team) Base.makeModel(Team.class, response.getBody());
90
    }
91
92
    /**
93
     * Delete Team by Slug
94
     *
95
     * @param slug Team of slug
96
     * @throws TeamNotFound
97
     */
98
    public boolean deleteTeam(String slug) throws TeamNotFound {
99
100
        try {
101
            Response response = this.base.sendRequest("/teams/".concat(slug), 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...
102
            return true;
103
        } catch (BaseHttpException e) {
104
            throw new TeamNotFound(slug);
105
        }
106
    }
107
108
    /**
109
     * List all the Teams by Pages and Limit
110
     *
111
     * @param page  Page number
112
     * @param limit Limit Value
113
     * @return List of All the Teams
114
     */
115
    public List<Team> getAllTeams(int page, int limit) throws BaseHttpException {
116
117
        ArrayList<String> parameters = new ArrayList<>();
118
119
120
        if (page != 0) {
121
            parameters.add("page=".concat(Integer.toString(page)));
122
        }
123
        if (limit != 0) {
124
            parameters.add("limit=".concat(Integer.toString(limit)));
125
        }
126
127
        String URL = Helpers.buildUrlWithQuery("/teams", parameters);
128
129
        Response response = this.base.sendRequest(URL, Request.METHOD_GET);
130
        Team[] teamArray = (Team[]) Base.makeModel(Team[].class, response.getBody());
131
        return new ArrayList<>(Arrays.asList(teamArray));
132
    }
133
134
    /**
135
     * Get all teams by pages
136
     *
137
     * @param page Pages
138
     * @return List of Teams
139
     */
140
    public List<Team> getAllTeams(int page) throws BaseHttpException {
141
        return getAllTeams(page, 0);
142
    }
143
144
    /**
145
     * Get all teams
146
     *
147
     * @return List of Teams
148
     */
149
    public List<Team> getAllTeams() throws BaseHttpException {
150
        return getAllTeams(0, 0);
151
    }
152
153
    /**
154
     * Get All Stared Message of Team
155
     *
156
     * @param teamSlug SLug Name of Team
157
     * @return List of Messages
158
     * @throws TeamNotFound      Exception
159
     * @throws BaseHttpException Exception
160
     */
161
    public List<Message> getAllStarredMessages(String teamSlug) throws TeamNotFound, BaseHttpException {
162
        String URL = "/teams/".concat(teamSlug).concat("/starred-messages");
163
        try {
164
            Response response = this.base.sendRequest(URL, Request.METHOD_GET);
165
            Message[] messagesArray = (Message[]) Base.makeModel(Message[].class, response.getBody());
166
            return new ArrayList<>(Arrays.asList(messagesArray));
167
        } catch (NotFound e) {
168
            throw new TeamNotFound(teamSlug);
169
        }
170
    }
171
172
    /**
173
     * Upload Team Picture.
174
     *
175
     * @param slug
176
     * @param picture
177
     * @return
178
     * @throws BaseHttpException
179
     */
180
    public Media uploadTeamPicture(String slug, File picture) throws BaseHttpException {
181
        Map<String, String> parameters = new HashMap<>();
182
183
        Map<String, File> files = new HashMap<>();
184
        files.put("file", picture);
185
186
        String url = "/teams/".concat(slug).concat("/picture");
187
        Response response = this.base.sendRequest(url, Request.METHOD_POST, parameters, new HashMap<>(),
188
                files);
189
        return (Media) Base.makeModel(Media.class, response.getBody());
190
    }
191
192
}
193