Skip to content

Commit

Permalink
fix: use params per_page and page for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Jul 23, 2020
1 parent c60bf44 commit 5dfb6e3
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 65 deletions.
4 changes: 2 additions & 2 deletions .schema/api.swagger.json
Expand Up @@ -94,8 +94,8 @@
"type": "integer",
"format": "int64",
"default": 100,
"description": "Pagination Limit\n\nThis is the number of items per page.",
"name": "limit",
"description": "Items per Page\n\nThis is the number of items per page.",
"name": "per_page",
"in": "query"
},
{
Expand Down
15 changes: 11 additions & 4 deletions identity/handler.go
Expand Up @@ -71,7 +71,7 @@ type identitiesListResponse struct {

// swagger:parameters listIdentities
type listIdentityParameters struct {
// Pagination Limit
// Items per Page
//
// This is the number of items per page.
//
Expand All @@ -80,7 +80,7 @@ type listIdentityParameters struct {
// default: 100
// min: 1
// max: 500
Limit int `json:"limit"`
ItemsPerPage int `json:"per_page"`

// Pagination Page
//
Expand Down Expand Up @@ -108,13 +108,20 @@ type listIdentityParameters struct {
// 200: identityList
// 500: genericError
func (h *Handler) list(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
page, limit := x.ParsePagination(r)
is, err := h.r.IdentityPool().ListIdentities(r.Context(), page, limit)
page, itemsPerPage := x.ParsePagination(r)
is, err := h.r.IdentityPool().ListIdentities(r.Context(), page, itemsPerPage)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}

total, err := h.r.IdentityPool().CountIdentities(r.Context())
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}

x.PaginationHeader(w, urlx.AppendPaths(h.c.SelfAdminURL(), IdentitiesPath), total, page, itemsPerPage)
h.r.Writer().Write(w, r, is)
}

Expand Down
1 change: 1 addition & 0 deletions identity/handler_test.go
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ory/x/urlx"

"github.com/ory/kratos/internal/httpclient/client"
"github.com/ory/kratos/internal/testhelpers"
"github.com/ory/kratos/schema"

Expand Down
14 changes: 13 additions & 1 deletion identity/pool.go
Expand Up @@ -30,7 +30,11 @@ import (

type (
Pool interface {
ListIdentities(ctx context.Context, page, limit int) ([]Identity, error)
// ListIdentities lists all identities in the store given the page and itemsPerPage.
ListIdentities(ctx context.Context, page, itemsPerPage int) ([]Identity, error)

// CountIdentities counts the number of identities in the store.
CountIdentities(ctx context.Context) (int64, error)

// GetIdentity returns an identity by its id. Will return an error if the identity does not exist or backend
// connectivity is broken.
Expand Down Expand Up @@ -142,6 +146,10 @@ func TestPool(p PrivilegedPool) func(t *testing.T) {
require.NoError(t, p.CreateIdentity(context.Background(), i))
assert.NotEqual(t, uuid.Nil, i.ID)
createdIDs = append(createdIDs, i.ID)

count, err := p.CountIdentities(context.Background())
require.NoError(t, err)
assert.EqualValues(t, 1, count)
})

t.Run("case=create with default values", func(t *testing.T) {
Expand All @@ -156,6 +164,10 @@ func TestPool(p PrivilegedPool) func(t *testing.T) {
assert.Equal(t, configuration.DefaultIdentityTraitsSchemaID, actual.SchemaID)
assert.Equal(t, defaultSchema.SchemaURL(exampleServerURL).String(), actual.SchemaURL)
assertEqual(t, expected, actual)

count, err := p.CountIdentities(context.Background())
require.NoError(t, err)
assert.EqualValues(t, 2, count)
})

t.Run("case=should error when the identity ID does not exist", func(t *testing.T) {
Expand Down
98 changes: 49 additions & 49 deletions internal/httpclient/client/admin/list_identities_parameters.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions persistence/sql/persister_identity.go
Expand Up @@ -157,6 +157,14 @@ func createRecoveryAddresses(ctx context.Context, tx *pop.Connection, i *identit
return nil
}

func (p *Persister) CountIdentities(ctx context.Context) (int64, error) {
count, err := p.c.WithContext(ctx).Count(new(identity.Identity))
if err != nil {
return 0, sqlcon.HandleError(err)
}
return int64(count), nil
}

func (p *Persister) CreateIdentity(ctx context.Context, i *identity.Identity) error {
if i.SchemaID == "" {
i.SchemaID = configuration.DefaultIdentityTraitsSchemaID
Expand Down

0 comments on commit 5dfb6e3

Please sign in to comment.