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.Invitation; |
11
|
|
|
|
12
|
|
|
import java.util.*; |
13
|
|
|
|
14
|
|
|
public class InvitationService { |
15
|
|
|
/** |
16
|
|
|
* {@link Base} |
17
|
|
|
*/ |
18
|
|
|
private Base base; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Construct Instance of Base Class |
22
|
|
|
* |
23
|
|
|
* @param base |
24
|
|
|
*/ |
25
|
|
|
public InvitationService(Base base) { |
26
|
|
|
this.base = base; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create Invitation Link for Team |
32
|
|
|
* |
33
|
|
|
* @param teamSlug Team Slug Name |
34
|
|
|
* @param email User's Invitation |
35
|
|
|
* @param message Invitation Message |
36
|
|
|
* @return |
37
|
|
|
* @throws BaseHttpException |
38
|
|
|
* @throws TeamNotFound |
39
|
|
|
*/ |
40
|
|
|
public boolean sendInvitation(String teamSlug, String email, String message) throws BaseHttpException, TeamNotFound { |
41
|
|
|
Map<String, String> parameters = new HashMap<>(); |
42
|
|
|
parameters.put("email", email); |
43
|
|
|
parameters.put("message", message); |
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
Response response = this.base |
|
|
|
|
47
|
|
|
.sendRequest("/teams/".concat(teamSlug).concat("/invitations"), Request.METHOD_POST, parameters); |
48
|
|
|
return true; |
49
|
|
|
} catch (NotFound e) { |
50
|
|
|
throw new TeamNotFound(teamSlug); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* List of Invitation of Team |
57
|
|
|
* |
58
|
|
|
* @param teamSlug Slug of Team |
59
|
|
|
* @param page Page number |
60
|
|
|
* @param limit Limit Value |
61
|
|
|
* @return List of Teams |
62
|
|
|
* @throws TeamNotFound Exception |
63
|
|
|
* @throws BaseHttpException Exception |
64
|
|
|
*/ |
65
|
|
|
public List<Invitation> getAllInvitations(String teamSlug, int page, int limit) throws TeamNotFound, BaseHttpException { |
66
|
|
|
ArrayList<String> parameters = new ArrayList<>(); |
67
|
|
|
|
68
|
|
|
if (page != 0) { |
69
|
|
|
parameters.add("page=".concat(Integer.toString(page))); |
70
|
|
|
} |
71
|
|
|
if (limit != 0) { |
72
|
|
|
parameters.add("limit=".concat(Integer.toString(limit))); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
String URL = Helpers.buildUrlWithQuery("/teams/".concat(teamSlug).concat("/invitations"), parameters); |
76
|
|
|
|
77
|
|
|
try { |
78
|
|
|
Response response = base.sendRequest(URL, Request.METHOD_GET); |
79
|
|
|
Invitation[] invitationsArray = (Invitation[]) Base.makeModel(Invitation[].class, response.getBody()); |
80
|
|
|
return new ArrayList<>(Arrays.asList(invitationsArray)); |
81
|
|
|
} catch (NotFound e) { |
82
|
|
|
throw new TeamNotFound(teamSlug); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* List of Invitation of Team |
89
|
|
|
* |
90
|
|
|
* @param teamSlug Slug of Team |
91
|
|
|
* @param page Page number |
92
|
|
|
* @return List of Teams |
93
|
|
|
* @throws TeamNotFound Exception |
94
|
|
|
* @throws BaseHttpException Exception |
95
|
|
|
*/ |
96
|
|
|
public List<Invitation> getAllInvitations(String teamSlug, int page) throws TeamNotFound, BaseHttpException { |
97
|
|
|
return getAllInvitations(teamSlug, page, 0); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* List of Invitation of Team |
102
|
|
|
* |
103
|
|
|
* @param teamSlug Slug of Team |
104
|
|
|
* @return List of Teams |
105
|
|
|
* @throws TeamNotFound Exception |
106
|
|
|
* @throws BaseHttpException Exception |
107
|
|
|
*/ |
108
|
|
|
public List<Invitation> getAllInvitations(String teamSlug) throws TeamNotFound, BaseHttpException { |
109
|
|
|
return getAllInvitations(teamSlug, 0, 0); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Cancel Invitation Link for Team |
114
|
|
|
* |
115
|
|
|
* @param teamSlug Team Slug Name |
116
|
|
|
* @return |
117
|
|
|
* @throws BaseHttpException |
118
|
|
|
* @throws TeamNotFound |
119
|
|
|
*/ |
120
|
|
|
public boolean cancelInvitation(String teamSlug, String id) throws BaseHttpException, TeamNotFound { |
121
|
|
|
|
122
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/invitations/").concat(id); |
123
|
|
|
try { |
124
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_DELETE); |
|
|
|
|
125
|
|
|
return true; |
126
|
|
|
} catch (NotFound e) { |
127
|
|
|
throw new TeamNotFound(teamSlug); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|