-
Notifications
You must be signed in to change notification settings - Fork 905
feat: add count to get users endpoint #5016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f771959
ed5a147
5da633e
8a00932
206381d
a1b7e4e
482b59f
0d738e8
d5b5000
ada3995
bd34b05
6249824
8a92b2b
ef32b99
7f43a6c
991b518
b86e0cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,7 +128,7 @@ WHERE | |
|
||
-- name: GetUsers :many | ||
SELECT | ||
* | ||
*, COUNT(*) OVER() AS count | ||
FROM | ||
users | ||
WHERE | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,27 +198,32 @@ func (api *API) users(rw http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
users, err := api.Database.GetUsers(ctx, database.GetUsersParams{ | ||
userRows, err := api.Database.GetUsers(ctx, database.GetUsersParams{ | ||
AfterID: paginationParams.AfterID, | ||
OffsetOpt: int32(paginationParams.Offset), | ||
LimitOpt: int32(paginationParams.Limit), | ||
Search: params.Search, | ||
Status: params.Status, | ||
RbacRole: params.RbacRole, | ||
}) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
httpapi.Write(ctx, rw, http.StatusOK, []codersdk.User{}) | ||
return | ||
} | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching users.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
// GetUsers does not return ErrNoRows because it uses a window function to get the count. | ||
// So we need to check if the userRows is empty and return an empty array if so. | ||
if len(userRows) == 0 { | ||
httpapi.Write(ctx, rw, http.StatusOK, codersdk.GetUsersResponse{ | ||
Users: []codersdk.User{}, | ||
Count: 0, | ||
}) | ||
return | ||
} | ||
Comment on lines
+216
to
+224
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it ever could return that error anyways since it returns an array, and only |
||
|
||
users, err = AuthorizeFilter(api.HTTPAuth, r, rbac.ActionRead, users) | ||
users, err := AuthorizeFilter(api.HTTPAuth, r, rbac.ActionRead, database.ConvertUserRows(userRows)) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching users.", | ||
|
@@ -248,42 +253,9 @@ func (api *API) users(rw http.ResponseWriter, r *http.Request) { | |
} | ||
|
||
render.Status(r, http.StatusOK) | ||
render.JSON(rw, r, convertUsers(users, organizationIDsByUserID)) | ||
} | ||
|
||
func (api *API) userCount(rw http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
query := r.URL.Query().Get("q") | ||
params, errs := userSearchQuery(query) | ||
if len(errs) > 0 { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Invalid user search query.", | ||
Validations: errs, | ||
}) | ||
return | ||
} | ||
|
||
sqlFilter, err := api.HTTPAuth.AuthorizeSQLFilter(r, rbac.ActionRead, rbac.ResourceUser.Type) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error preparing sql filter.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
count, err := api.Database.GetAuthorizedUserCount(ctx, database.GetFilteredUserCountParams{ | ||
Search: params.Search, | ||
Status: params.Status, | ||
RbacRole: params.RbacRole, | ||
}, sqlFilter) | ||
if err != nil { | ||
httpapi.InternalServerError(rw, err) | ||
return | ||
} | ||
|
||
httpapi.Write(ctx, rw, http.StatusOK, codersdk.UserCountResponse{ | ||
Count: count, | ||
render.JSON(rw, r, codersdk.GetUsersResponse{ | ||
Users: convertUsers(users, organizationIDsByUserID), | ||
Count: int(userRows[0].Count), | ||
}) | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.