-
Notifications
You must be signed in to change notification settings - Fork 2
/
user_handler.go
265 lines (246 loc) · 7.13 KB
/
user_handler.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
package handlers
import (
"errors"
"net/http"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog"
kerr "github.com/krok-o/krok/errors"
"github.com/krok-o/krok/pkg/krok/providers"
"github.com/krok-o/krok/pkg/models"
)
// UserHandler .
type UserHandler struct {
UserHandlerDependencies
}
// UserHandlerDependencies .
type UserHandlerDependencies struct {
Logger zerolog.Logger
UserStore providers.UserStorer
APIKeyAuth providers.APIKeysAuthenticator
}
// NewUserHandler .
func NewUserHandler(deps UserHandlerDependencies) *UserHandler {
return &UserHandler{
UserHandlerDependencies: deps,
}
}
var _ providers.UserHandler = &UserHandler{}
// GetUser returns a user.
// swagger:operation GET /user/{id} getUser
// Gets the user with the corresponding ID.
// ---
// produces:
// - application/json
// parameters:
// - name: id
// in: path
// type: integer
// format: int
// required: true
// responses:
// '200':
// schema:
// "$ref": "#/definitions/User"
// '400':
// description: 'invalid user id'
// schema:
// "$ref": "#/responses/Message"
// '404':
// description: 'user not found'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'failed to get user'
// schema:
// "$ref": "#/responses/Message"
func (u *UserHandler) GetUser() echo.HandlerFunc {
return func(c echo.Context) error {
n, err := GetParamAsInt("id", c)
if err != nil {
apiError := kerr.APIError("invalid id", http.StatusBadRequest, nil)
return c.JSON(http.StatusBadRequest, apiError)
}
ctx := c.Request().Context()
// Get the user from store.
user, err := u.UserStore.Get(ctx, n)
if err != nil {
if errors.Is(err, kerr.ErrNotFound) {
return c.JSON(http.StatusNotFound, kerr.APIError("user not found", http.StatusNotFound, err))
}
apiError := kerr.APIError("failed to get user", http.StatusInternalServerError, err)
return c.JSON(http.StatusInternalServerError, apiError)
}
return c.JSON(http.StatusOK, user)
}
}
// ListUsers lists all users.
// swagger:operation POST /users listUsers
// List users
// ---
// produces:
// - application/json
// responses:
// '200':
// schema:
// type: array
// items:
// "$ref": "#/definitions/User"
// '500':
// description: 'failed to list user'
// schema:
// "$ref": "#/responses/Message"
func (u *UserHandler) ListUsers() echo.HandlerFunc {
return func(c echo.Context) error {
ctx := c.Request().Context()
// Get the users from store.
users, err := u.UserStore.List(ctx)
if err != nil {
apiError := kerr.APIError("failed to list users", http.StatusInternalServerError, err)
return c.JSON(http.StatusInternalServerError, apiError)
}
return c.JSON(http.StatusOK, users)
}
}
// DeleteUser deletes a user.
// swagger:operation DELETE /user/{id} deleteUser
// Deletes the given user.
// ---
// parameters:
// - name: id
// in: path
// description: 'The ID of the user to delete'
// required: true
// type: integer
// format: int
// responses:
// '200':
// description: 'OK in case the deletion was successful'
// '400':
// description: 'in case of missing user context or invalid ID'
// schema:
// "$ref": "#/responses/Message"
// '404':
// description: 'in case of user not found'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'when the deletion operation failed'
// schema:
// "$ref": "#/responses/Message"
func (u *UserHandler) DeleteUser() echo.HandlerFunc {
return func(c echo.Context) error {
n, err := GetParamAsInt("id", c)
if err != nil {
apiError := kerr.APIError("invalid id", http.StatusBadRequest, nil)
return c.JSON(http.StatusBadRequest, apiError)
}
ctx := c.Request().Context()
if err := u.UserStore.Delete(ctx, n); err != nil {
if errors.Is(err, kerr.ErrNotFound) {
apiError := kerr.APIError("user not found", http.StatusNotFound, err)
return c.JSON(http.StatusNotFound, apiError)
}
apiError := kerr.APIError("failed to delete users", http.StatusInternalServerError, err)
return c.JSON(http.StatusInternalServerError, apiError)
}
return c.NoContent(http.StatusOK)
}
}
// UpdateUser update a specific user.
// swagger:operation POST /user/update updateUser
// Updates an existing user.
// ---
// produces:
// - application/json
// consumes:
// - application/json
// parameters:
// - name: user
// in: body
// required: true
// schema:
// "$ref": "#/definitions/User"
// responses:
// '200':
// description: 'user successfully updated'
// schema:
// "$ref": "#/definitions/User"
// '400':
// description: 'invalid json payload'
// schema:
// "$ref": "#/responses/Message"
// '404':
// description: 'user not found'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'failed to update user'
// schema:
// "$ref": "#/responses/Message"
func (u *UserHandler) UpdateUser() echo.HandlerFunc {
return func(c echo.Context) error {
var update *models.User
if err := c.Bind(&update); err != nil {
return c.JSON(http.StatusBadRequest, kerr.APIError("failed to bind user", http.StatusBadRequest, err))
}
result, err := u.UserStore.Update(c.Request().Context(), update)
if err != nil {
if errors.Is(err, kerr.ErrNotFound) {
apiError := kerr.APIError("user not found", http.StatusNotFound, err)
return c.JSON(http.StatusNotFound, apiError)
}
apiError := kerr.APIError("failed to update user", http.StatusInternalServerError, err)
return c.JSON(http.StatusInternalServerError, apiError)
}
return c.JSON(http.StatusOK, result)
}
}
// CreateUser creates a new user.
// swagger:operation POST /user createUser
// Creates a new user
// ---
// produces:
// - application/json
// consumes:
// - application/json
// parameters:
// - name: user
// in: body
// required: true
// schema:
// "$ref": "#/definitions/User"
// responses:
// '200':
// description: 'the created user'
// schema:
// "$ref": "#/definitions/User"
// '400':
// description: 'invalid json payload'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'failed to create user or generating a new api key'
// schema:
// "$ref": "#/responses/Message"
func (u *UserHandler) CreateUser() echo.HandlerFunc {
return func(c echo.Context) error {
var create *models.User
if err := c.Bind(&create); err != nil {
return c.JSON(http.StatusBadRequest, kerr.APIError("failed to bind user", http.StatusBadRequest, err))
}
// Create the user
result, err := u.UserStore.Create(c.Request().Context(), create)
if err != nil {
apiError := kerr.APIError("failed to create user", http.StatusInternalServerError, err)
return c.JSON(http.StatusInternalServerError, apiError)
}
// Create initial API key
key, err := u.APIKeyAuth.Generate(c.Request().Context(), "New API Key", result.ID)
if err != nil {
apiError := kerr.APIError("failed to create new api keys for user", http.StatusInternalServerError, err)
return c.JSON(http.StatusInternalServerError, apiError)
}
result.APIKeys = append(result.APIKeys, key)
return c.JSON(http.StatusCreated, result)
}
}