From 5f468c2ea70499a59fcae09403a6931d0d3f54ff Mon Sep 17 00:00:00 2001 From: iljaSL Date: Tue, 24 Sep 2024 15:39:59 +0300 Subject: [PATCH] fix: refactor filter param ussage --- api/auth/client.go | 27 ++++++++++++++----- api/auth/model.go | 4 +-- api/filters/filters.go | 59 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/api/auth/client.go b/api/auth/client.go index b0088fd..8cb9a81 100644 --- a/api/auth/client.go +++ b/api/auth/client.go @@ -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(¶ms) + } _, 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(¶ms) + } _, 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(¶ms) + } _, err := c.api. URL("/auth/api/v1/sessionstorage/sessions/search"). - Query(filters). + Query(params). Post(search, &sessions) return sessions, err diff --git a/api/auth/model.go b/api/auth/model.go index be525b5..bef8f90 100644 --- a/api/auth/model.go +++ b/api/auth/model.go @@ -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"` diff --git a/api/filters/filters.go b/api/filters/filters.go index 91b0384..2a2653d 100644 --- a/api/filters/filters.go +++ b/api/filters/filters.go @@ -1,5 +1,10 @@ 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"` @@ -7,3 +12,57 @@ type Params struct { 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 + } +}