Skip to content

Commit

Permalink
feat: client query negation
Browse files Browse the repository at this point in the history
  • Loading branch information
awill1988 committed Jul 26, 2024
1 parent 0b3ecfc commit d26da33
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
4 changes: 2 additions & 2 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,8 @@ func (h *Handler) listOAuth2Clients(w http.ResponseWriter, r *http.Request, ps h
filters := Filter{
Limit: itemsPerPage,
Offset: page * itemsPerPage,
Name: r.URL.Query().Get("client_name"),
Owner: r.URL.Query().Get("owner"),
Name: field(r.URL.Query().Get("client_name")),
Owner: field(r.URL.Query().Get("owner")),
}

c, err := h.r.ClientManager().GetClients(r.Context(), filters)
Expand Down
18 changes: 16 additions & 2 deletions client/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package client

import (
"context"
"strings"

"github.com/ory/fosite"
)
Expand All @@ -21,11 +22,24 @@ type Filter struct {

// The name of the clients to filter by.
// in: query
Name string `json:"client_name"`
Name field `json:"client_name"`

// The owner of the clients to filter by.
// in: query
Owner string `json:"owner"`
Owner field `json:"owner"`
}

type field string

func (f field) Value() string {
if f.IsNegated() {
return string(f[2:])
}
return string(f)
}

func (f field) IsNegated() bool {
return strings.HasPrefix(string(f), "!=")
}

type Manager interface {
Expand Down
12 changes: 12 additions & 0 deletions client/manager_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ func TestHelperCreateGetUpdateDeleteClient(k string, connection *pop.Connection,
assert.Len(t, ds, 1)
assert.Equal(t, ds[0].Name, "name")

// get by name negated prefix
ds, err = t1.GetClients(ctx, Filter{Limit: 100, Offset: 0, Name: "!=name"})
assert.NoError(t, err)
assert.Len(t, ds, 1)
assert.Equal(t, ds[0].GetID(), "2-1234")

// get by name not exist
ds, err = t1.GetClients(ctx, Filter{Limit: 100, Offset: 0, Name: "bad name"})
assert.NoError(t, err)
Expand All @@ -309,6 +315,12 @@ func TestHelperCreateGetUpdateDeleteClient(k string, connection *pop.Connection,
assert.Len(t, ds, 1)
assert.Equal(t, ds[0].Owner, "aeneas")

// get by owner negated prefix
ds, err = t1.GetClients(ctx, Filter{Limit: 100, Offset: 0, Owner: "!=aeneas"})
assert.NoError(t, err)
assert.Len(t, ds, 1)
assert.Equal(t, ds[0].GetID(), "2-1234")

testHelperUpdateClient(t, ctx, t1, k)
testHelperUpdateClient(t, ctx, t2, k)

Expand Down
4 changes: 2 additions & 2 deletions internal/httpclient/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ paths:
minimum: 1
type: string
style: form
- description: The name of the clients to filter by.
- description: The name of the clients to filter by. Prefix "!=" to search by negation.
explode: true
in: query
name: client_name
required: false
schema:
type: string
style: form
- description: The owner of the clients to filter by.
- description: The owner of the clients to filter by. Prefix "!=" to search by negation.
explode: true
in: query
name: owner
Expand Down
12 changes: 10 additions & 2 deletions persistence/sql/persister_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,18 @@ func (p *Persister) GetClients(ctx context.Context, filters client.Filter) (_ []
Order("id")

if filters.Name != "" {
query.Where("client_name = ?", filters.Name)
stmt := "client_name = ?"
if filters.Name.IsNegated() {
stmt = "client_name != ?"
}
query.Where(stmt, filters.Name.Value())
}
if filters.Owner != "" {
query.Where("owner = ?", filters.Owner)
stmt := "owner = ?"
if filters.Owner.IsNegated() {
stmt = "owner != ?"
}
query.Where(stmt, filters.Owner.Value())
}

if err := query.All(&cs); err != nil {
Expand Down

0 comments on commit d26da33

Please sign in to comment.