/
user_group.go
279 lines (234 loc) · 7.22 KB
/
user_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package router
import (
"github.com/labstack/echo"
"github.com/satori/go.uuid"
"github.com/traPtitech/traQ/model"
"github.com/traPtitech/traQ/repository"
"net/http"
)
type userGroupResponse struct {
GroupID uuid.UUID `json:"groupId"`
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
AdminUserID uuid.UUID `json:"adminUserId"`
Members []uuid.UUID `json:"members"`
}
// GetUserGroups GET /groups
func (h *Handlers) GetUserGroups(c echo.Context) error {
gs, err := h.Repo.GetAllUserGroups()
if err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
res, err := h.formatUserGroups(gs)
if err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
return c.JSON(http.StatusOK, res)
}
// PostUserGroups POST /groups
func (h *Handlers) PostUserGroups(c echo.Context) error {
reqUserID := getRequestUserID(c)
var req struct {
Name string `json:"name" validate:"max=30,required"`
Description string `json:"description"`
}
if err := bindAndValidate(c, &req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
g, err := h.Repo.CreateUserGroup(req.Name, req.Description, reqUserID)
if err != nil {
switch err {
case repository.ErrAlreadyExists:
return c.NoContent(http.StatusConflict)
default:
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
}
res, err := h.formatUserGroup(g)
if err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
return c.JSON(http.StatusCreated, res)
}
// GetUserGroup GET /groups/:groupID
func (h *Handlers) GetUserGroup(c echo.Context) error {
g := getGroupFromContext(c)
res, err := h.formatUserGroup(g)
if err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
return c.JSON(http.StatusOK, res)
}
// PatchUserGroup PATCH /groups/:groupID
func (h *Handlers) PatchUserGroup(c echo.Context) error {
groupID := getRequestParamAsUUID(c, paramGroupID)
reqUserID := getRequestUserID(c)
g := getGroupFromContext(c)
var req struct {
Name string `json:"name" validate:"max=30"`
Description *string `json:"description"`
AdminUserID *uuid.UUID `json:"adminUserId"`
}
if err := bindAndValidate(c, &req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
// 管理者ユーザーかどうか
if g.AdminUserID != reqUserID {
return c.NoContent(http.StatusForbidden)
}
args := repository.UpdateUserGroupNameArgs{
Name: req.Name,
}
if req.Description != nil {
args.Description.Valid = true
args.Description.String = *req.Description
}
if req.AdminUserID != nil {
// ユーザーが存在するか
if ok, err := h.Repo.UserExists(*req.AdminUserID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
} else if !ok {
c.Logger().Error(err)
return c.NoContent(http.StatusBadRequest)
}
args.AdminUserID.Valid = true
args.AdminUserID.UUID = *req.AdminUserID
}
if err := h.Repo.UpdateUserGroup(groupID, args); err != nil {
switch err {
case repository.ErrAlreadyExists:
return c.NoContent(http.StatusConflict)
default:
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
}
return c.NoContent(http.StatusNoContent)
}
// DeleteUserGroup DELETE /groups/:groupID
func (h *Handlers) DeleteUserGroup(c echo.Context) error {
groupID := getRequestParamAsUUID(c, paramGroupID)
userID := getRequestUserID(c)
g := getGroupFromContext(c)
// 管理者ユーザーかどうか
if g.AdminUserID != userID {
return c.NoContent(http.StatusForbidden)
}
if err := h.Repo.DeleteUserGroup(groupID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
return c.NoContent(http.StatusNoContent)
}
// GetUserGroupMembers GET /groups/:groupID/members
func (h *Handlers) GetUserGroupMembers(c echo.Context) error {
groupID := getRequestParamAsUUID(c, paramGroupID)
res, err := h.Repo.GetUserGroupMemberIDs(groupID)
if err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
return c.JSON(http.StatusOK, res)
}
// PostUserGroupMembers POST /groups/:groupID/members
func (h *Handlers) PostUserGroupMembers(c echo.Context) error {
groupID := getRequestParamAsUUID(c, paramGroupID)
reqUserID := getRequestUserID(c)
g := getGroupFromContext(c)
var req struct {
UserID uuid.UUID `json:"userId"`
}
if err := bindAndValidate(c, &req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
// 管理者ユーザーかどうか
if g.AdminUserID != reqUserID {
return c.NoContent(http.StatusForbidden)
}
// ユーザーが存在するか
if ok, err := h.Repo.UserExists(req.UserID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
} else if !ok {
c.Logger().Error(err)
return c.NoContent(http.StatusBadRequest)
}
if err := h.Repo.AddUserToGroup(req.UserID, groupID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
return c.NoContent(http.StatusNoContent)
}
// DeleteUserGroupMembers DELETE /groups/:groupID/members/:userID
func (h *Handlers) DeleteUserGroupMembers(c echo.Context) error {
groupID := getRequestParamAsUUID(c, paramGroupID)
userID := getRequestParamAsUUID(c, paramUserID)
reqUserID := getRequestUserID(c)
g := getGroupFromContext(c)
// 管理者ユーザーかどうか
if g.AdminUserID != reqUserID {
return c.NoContent(http.StatusForbidden)
}
// ユーザーが存在するか
if ok, err := h.Repo.UserExists(userID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
} else if !ok {
c.Logger().Error(err)
return c.NoContent(http.StatusBadRequest)
}
if err := h.Repo.RemoveUserFromGroup(userID, groupID); err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
return c.NoContent(http.StatusNoContent)
}
// GetMyBelongingGroup GET /users/me/groups
func (h *Handlers) GetMyBelongingGroup(c echo.Context) error {
userID := getRequestUserID(c)
ids, err := h.Repo.GetUserBelongingGroupIDs(userID)
if err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
return c.JSON(http.StatusOK, ids)
}
// GetUserBelongingGroup GET /users/:userID/groups
func (h *Handlers) GetUserBelongingGroup(c echo.Context) error {
userID := getRequestParamAsUUID(c, paramUserID)
ids, err := h.Repo.GetUserBelongingGroupIDs(userID)
if err != nil {
c.Logger().Error(err)
return c.NoContent(http.StatusInternalServerError)
}
return c.JSON(http.StatusOK, ids)
}
func (h *Handlers) formatUserGroup(g *model.UserGroup) (r *userGroupResponse, err error) {
r = &userGroupResponse{
GroupID: g.ID,
Name: g.Name,
Description: g.Description,
Type: g.Type,
AdminUserID: g.AdminUserID,
}
r.Members, err = h.Repo.GetUserGroupMemberIDs(g.ID)
return
}
func (h *Handlers) formatUserGroups(gs []*model.UserGroup) ([]*userGroupResponse, error) {
arr := make([]*userGroupResponse, len(gs))
for i, g := range gs {
r, err := h.formatUserGroup(g)
if err != nil {
return nil, err
}
arr[i] = r
}
return arr, nil
}