Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1df750b

Browse files
feat: add GET /api/v2/users (#1028)
1 parentaf67280 commit1df750b

File tree

8 files changed

+102
-0
lines changed

8 files changed

+102
-0
lines changed

‎coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func New(options *Options) (http.Handler, func()) {
145145
r.Group(func(r chi.Router) {
146146
r.Use(httpmw.ExtractAPIKey(options.Database,nil))
147147
r.Post("/",api.postUsers)
148+
r.Get("/",api.users)
148149
r.Route("/{user}",func(r chi.Router) {
149150
r.Use(httpmw.ExtractUserParam(options.Database))
150151
r.Get("/",api.userByName)

‎coderd/database/databasefake/databasefake.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ func (q *fakeQuerier) GetUserCount(_ context.Context) (int64, error) {
164164
returnint64(len(q.users)),nil
165165
}
166166

167+
func (q*fakeQuerier)GetUsers(_ context.Context) ([]database.User,error) {
168+
q.mutex.RLock()
169+
deferq.mutex.RUnlock()
170+
171+
returnq.users,nil
172+
}
173+
167174
func (q*fakeQuerier)GetWorkspacesByTemplateID(_ context.Context,arg database.GetWorkspacesByTemplateIDParams) ([]database.Workspace,error) {
168175
q.mutex.RLock()
169176
deferq.mutex.RUnlock()

‎coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries.sql.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries/users.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ SET
5151
updated_at= $5
5252
WHERE
5353
id= $1 RETURNING*;
54+
55+
-- name: GetUsers :many
56+
SELECT
57+
*
58+
FROM
59+
users;

‎coderd/users.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ import (
2323
"github.com/coder/coder/cryptorand"
2424
)
2525

26+
// Lists all the users
27+
func (api*api)users(rw http.ResponseWriter,r*http.Request) {
28+
users,err:=api.Database.GetUsers(r.Context())
29+
30+
iferr!=nil {
31+
httpapi.Write(rw,http.StatusInternalServerError, httpapi.Response{
32+
Message:fmt.Sprintf("get users: %s",err.Error()),
33+
})
34+
return
35+
}
36+
37+
varres []codersdk.User
38+
for_,user:=rangeusers {
39+
res=append(res,convertUser(user))
40+
}
41+
42+
httpapi.Write(rw,http.StatusOK,res)
43+
}
44+
2645
// Returns whether the initial user has been created or not.
2746
func (api*api)firstUser(rw http.ResponseWriter,r*http.Request) {
2847
userCount,err:=api.Database.GetUserCount(r.Context())

‎coderd/users_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,21 @@ func TestUserByName(t *testing.T) {
313313
require.NoError(t,err)
314314
}
315315

316+
funcTestGetUsers(t*testing.T) {
317+
t.Parallel()
318+
client:=coderdtest.New(t,nil)
319+
user:=coderdtest.CreateFirstUser(t,client)
320+
client.CreateUser(context.Background(), codersdk.CreateUserRequest{
321+
Email:"bruno@coder.com",
322+
Username:"bruno",
323+
Password:"password",
324+
OrganizationID:user.OrganizationID,
325+
})
326+
users,err:=client.GetUsers(context.Background())
327+
require.NoError(t,err)
328+
require.Len(t,users,2)
329+
}
330+
316331
funcTestOrganizationsByUser(t*testing.T) {
317332
t.Parallel()
318333
client:=coderdtest.New(t,nil)

‎codersdk/users.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ func (c *Client) UpdateUserProfile(ctx context.Context, userID uuid.UUID, req Up
136136
returnuser,json.NewDecoder(res.Body).Decode(&user)
137137
}
138138

139+
func (c*Client)GetUsers(ctx context.Context) ([]User,error) {
140+
res,err:=c.request(ctx,http.MethodGet,"/api/v2/users",nil)
141+
iferr!=nil {
142+
return []User{},err
143+
}
144+
deferres.Body.Close()
145+
ifres.StatusCode!=http.StatusOK {
146+
return []User{},readBodyAsError(res)
147+
}
148+
varusers []User
149+
returnusers,json.NewDecoder(res.Body).Decode(&users)
150+
}
151+
139152
// CreateAPIKey generates an API key for the user ID provided.
140153
func (c*Client)CreateAPIKey(ctx context.Context,userID uuid.UUID) (*GenerateAPIKeyResponse,error) {
141154
res,err:=c.request(ctx,http.MethodPost,fmt.Sprintf("/api/v2/users/%s/keys",uuidOrMe(userID)),nil)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp