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 25, 2024
1 parent 0b3ecfc commit 500a2e0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 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
21 changes: 19 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,27 @@ 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:])
}
if strings.HasPrefix(string(f), "=") {
return string(f[1:])
}
return string(f)
}

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

type Manager interface {
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.Name.IsNegated() {
stmt = "owner != ?"
}
query.Where(stmt, filters.Owner.Value())
}

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

0 comments on commit 500a2e0

Please sign in to comment.