Merge pull request #7192 from heww/fix-users-api-pagination

Fix pagination for users and users search apis
This commit is contained in:
Wenkai Yin 2019-03-21 14:16:12 +08:00 committed by GitHub
commit 8e870de976
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -318,6 +318,11 @@ func TestListUsers(t *testing.T) {
if users2[0].Username != username {
t.Errorf("The username in result list does not match, expected: %s, actual: %s", username, users2[0].Username)
}
users3, err := ListUsers(&models.UserQuery{Username: username, Pagination: &models.Pagination{Page: 2, Size: 1}})
if len(users3) != 0 {
t.Errorf("Expect no user in list, but the acutal length is %d, the list: %+v", len(users3), users3)
}
}
func TestResetUserPassword(t *testing.T) {

View File

@ -106,10 +106,13 @@ func GetTotalOfUsers(query *models.UserQuery) (int64, error) {
// ListUsers lists all users according to different conditions.
func ListUsers(query *models.UserQuery) ([]models.User, error) {
qs := userQueryConditions(query)
if query != nil && query.Pagination != nil {
offset := (query.Pagination.Page - 1) * query.Pagination.Size
qs = qs.Offset(offset).Limit(query.Pagination.Size)
}
users := []models.User{}
_, err := userQueryConditions(query).Limit(-1).
OrderBy("username").
All(&users)
_, err := qs.OrderBy("username").All(&users)
return users, err
}