-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.go
More file actions
206 lines (176 loc) · 4.99 KB
/
Copy pathusers.go
File metadata and controls
206 lines (176 loc) · 4.99 KB
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
// Patchwork - automated patch tracking system
// Copyright (C) The Patchwork Contributors (see CONTRIBUTORS)
//
// SPDX-License-Identifier: GPL-2.0-or-later
package api
import (
"context"
"fmt"
"net/http"
"github.com/danielgtaylor/huma/v2"
"github.com/uptrace/bun"
"github.com/getpatchwork/patchwork/pkg/db"
"github.com/getpatchwork/patchwork/pkg/log"
)
func registerUserRoutes(api huma.API, h *handler, prefix string, mw huma.Middlewares) {
huma.Register(api, huma.Operation{
Method: http.MethodGet, Path: prefix + "/users",
OperationID: fmt.Sprintf("list-users-v%s", prefix[5:]),
Security: authRequired,
Middlewares: mw,
}, h.ListUsers)
huma.Register(api, huma.Operation{
Method: http.MethodGet, Path: prefix + "/users/{id}",
OperationID: fmt.Sprintf("get-user-v%s", prefix[5:]),
Security: authRequired,
Middlewares: mw,
}, h.GetUserDetail)
huma.Register(api, huma.Operation{
Method: http.MethodPatch, Path: prefix + "/users/{id}",
OperationID: fmt.Sprintf("update-user-v%s", prefix[5:]),
Security: authRequired,
Middlewares: mw,
}, h.UpdateUser)
huma.Register(api, huma.Operation{
Method: http.MethodPut, Path: prefix + "/users/{id}",
OperationID: fmt.Sprintf("put-user-v%s", prefix[5:]),
Security: authRequired,
Middlewares: mw,
}, h.UpdateUser)
}
type ListUsersInput struct {
PageParams
SearchParams
}
type ListUsersOutput struct {
Link string `header:"Link" doc:"Pagination links"`
Body []UserResponse `json:"body"`
}
func (h *handler) ListUsers(
ctx context.Context, input *ListUsersInput,
) (*ListUsersOutput, error) {
if _, err := h.requireUser(ctx); err != nil {
return nil, err
}
base := h.apiBase(ctx)
sq := db.GetQueries(ctx).DB.NewSelect().Model((*db.User)(nil))
if input.Q != "" {
sq = sq.WhereGroup(" AND ", func(sq *bun.SelectQuery) *bun.SelectQuery {
return sq.Where("username LIKE ?", "%"+input.Q+"%").
WhereOr("first_name LIKE ?", "%"+input.Q+"%").
WhereOr("last_name LIKE ?", "%"+input.Q+"%").
WhereOr("email LIKE ?", "%"+input.Q+"%")
})
}
total, err := sq.Count(ctx)
if err != nil {
log.Errorf("count users: %v", err)
return nil, huma.Error500InternalServerError("Internal error.")
}
perPage := input.PerPage
if perPage < 1 {
perPage = h.cfg.Http.ApiPageSize
}
if perPage > h.cfg.Http.ApiPageMax {
perPage = h.cfg.Http.ApiPageMax
}
offset := (input.Page - 1) * perPage
var users []db.User
if err := sq.OrderExpr("id ASC").Offset(offset).Limit(perPage).Scan(ctx, &users); err != nil {
log.Errorf("list users: %v", err)
return nil, huma.Error500InternalServerError("Internal error.")
}
resp := &ListUsersOutput{
Link: buildLinkHeader(input.Page, perPage, total),
Body: make([]UserResponse, len(users)),
}
for i := range users {
resp.Body[i] = userToResponse(&users[i], base)
}
return resp, nil
}
type GetUserInput struct {
ID int `path:"id" doc:"User ID"`
}
type GetUserOutput struct {
Body UserDetailResponse
}
func (h *handler) GetUserDetail(
ctx context.Context, input *GetUserInput,
) (*GetUserOutput, error) {
q := db.GetQueries(ctx)
caller, err := h.requireUser(ctx)
if err != nil {
return nil, err
}
base := h.apiBase(ctx)
var user db.User
if err := q.DB.NewSelect().Model(&user).
Where("id = ?", input.ID).Scan(ctx); err != nil {
return nil, huma.Error404NotFound("Not found.")
}
resp := &GetUserOutput{
Body: userToDetailResponse(&user, base),
}
if caller.ID == user.ID {
resp.Body.Settings = &UserSettings{
SendEmail: user.SendEmail,
ItemsPerPage: user.ItemsPerPage,
ShowIds: user.ShowIds,
}
}
return resp, nil
}
type UpdateUserInput struct {
ID int `path:"id" doc:"User ID"`
Body UserUpdateBody `json:"body"`
}
func (h *handler) UpdateUser(
ctx context.Context, input *UpdateUserInput,
) (*GetUserOutput, error) {
q := db.GetQueries(ctx)
caller, err := h.requireUser(ctx)
if caller == nil {
return nil, err
}
if caller.ID != input.ID {
return nil, ForbiddenErr
}
var user db.User
if err := q.DB.NewSelect().Model(&user).
Where("id = ?", input.ID).Scan(ctx); err != nil {
return nil, huma.Error404NotFound("Not found.")
}
body := &input.Body
uq := q.DB.NewUpdate().Model(&user).Where("id = ?", input.ID)
if body.FirstName != nil {
uq = uq.Set("first_name = ?", *body.FirstName)
user.FirstName = *body.FirstName
}
if body.LastName != nil {
uq = uq.Set("last_name = ?", *body.LastName)
user.LastName = *body.LastName
}
if _, err := uq.Exec(ctx); err != nil {
return nil, huma.Error400BadRequest("Update failed.")
}
base := h.apiBase(ctx)
return &GetUserOutput{
Body: userToDetailResponse(&user, base),
}, nil
}
func userToResponse(u *db.User, base string) UserResponse {
return UserResponse{
ID: u.ID,
URL: fmt.Sprintf("%s/users/%d", base, u.ID),
Username: u.Username,
FirstName: u.FirstName,
LastName: u.LastName,
Email: u.Email,
}
}
func userToDetailResponse(u *db.User, base string) UserDetailResponse {
return UserDetailResponse{
UserResponse: userToResponse(u, base),
}
}