Skip to content

Commit

Permalink
fix: refactor filter param ussage
Browse files Browse the repository at this point in the history
  • Loading branch information
iljaSL committed Sep 24, 2024
1 parent 7ecdaaf commit 5f468c2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
27 changes: 21 additions & 6 deletions api/auth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,36 +89,51 @@ func (c *Auth) RegenerateIdpClientConfig(idpId string) (*IdpClientConfig, error)

// MARK: Session Storage
// GetUserSessions get valid sessions by userID.
func (c *Auth) GetUserSessions(filters filters.Params, userId string) (*response.ResultSet[Session], error) {
func (c *Auth) GetUserSessions(userId string, opts ...filters.Option) (*response.ResultSet[Session], error) {
userSessions := &response.ResultSet[Session]{}
params := filters.Default()

for _, opt := range opts {
opt(&params)
}

_, err := c.api.
URL("/auth/api/v1/sessionstorage/users/%s/sessions", userId).
Query(filters).
Query(params).
Get(&userSessions)

return userSessions, err
}

// GetSourceSessions get valid sessions by sourceID.
func (c *Auth) GetSourceSessions(filters filters.Params, sourceId string) (*response.ResultSet[Session], error) {
func (c *Auth) GetSourceSessions(sourceId string, opts ...filters.Option) (*response.ResultSet[Session], error) {
sourceSessions := &response.ResultSet[Session]{}
params := filters.Default()

for _, opt := range opts {
opt(&params)
}

_, err := c.api.
URL("/auth/api/v1/sessionstorage/sources/%s/sessions", sourceId).
Query(filters).
Query(params).
Get(&sourceSessions)

return sourceSessions, err
}

// SearchSessions searches for sessions
func (c *Auth) SearchSessions(filters filters.Params, search *SessionSearchRequest) (*response.ResultSet[Session], error) {
func (c *Auth) SearchSessions(search *SessionSearch, opts ...filters.Option) (*response.ResultSet[Session], error) {
sessions := &response.ResultSet[Session]{}
params := filters.Default()

for _, opt := range opts {
opt(&params)
}

_, err := c.api.
URL("/auth/api/v1/sessionstorage/sessions/search").
Query(filters).
Query(params).
Post(search, &sessions)

return sessions, err
Expand Down
4 changes: 2 additions & 2 deletions api/auth/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ type SessionPasswordPolicy struct {
PasswordStrength string `json:"password_strength,omitempty"`
}

// SessionSearchRequest session search request parameter definition.
type SessionSearchRequest struct {
// SessionSearch session search request parameter definition.
type SessionSearch struct {
Keywords string `json:"keywords,omitempty"`
UserId string `json:"user_id,omitempty"`
Type string `json:"type,omitempty"`
Expand Down
59 changes: 59 additions & 0 deletions api/filters/filters.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
package filters

const (
ASC = "ASC" // ASC defines ascending sort direction.
DESC = "DESC" // DESC defines descending sort direction.
)

// Params defines optional query parameters.
type Params struct {
Offset int `json:"offset,omitempty"`
Limit int `json:"limit,omitempty"`
Sortkey string `json:"sortkey,omitempty"`
Sortdir string `json:"sortdir,omitempty"`
}

// Default constructor for default filters.
func Default() Params {
return Params{
Offset: 0,
Limit: 50,
Sortdir: "ASC",
}
}

// Option function type modifying Params.
type Option func(p *Params)

// Offset sets the offset for Params.
func Offset(o int) Option {
return func(p *Params) { p.Offset = o }
}

// Limit sets the limit for Params.
func Limit(l int) Option {
return func(p *Params) { p.Limit = l }
}

// Paging sets both the offset and limit for Params.
func Paging(o, l int) Option {
return func(p *Params) {
p.Offset = o
p.Limit = l
}
}

// Sort sets the sort key and direction for Params.
func Sort(key, dir string) Option {
return func(p *Params) {
p.Sortkey = key
p.Sortdir = dir
}
}

// SortAsc sets the sort key with ascending order for Params.
func SortAsc(key string) Option {
return func(p *Params) {
p.Sortkey = key
p.Sortdir = ASC
}
}

// SortDesc sets the sort key with descending order for Params.
func SortDesc(key string) Option {
return func(p *Params) {
p.Sortkey = key
p.Sortdir = DESC
}
}

0 comments on commit 5f468c2

Please sign in to comment.