Skip to content

Commit

Permalink
fix(SPV-000): enhance DefaultsIfNil to check for empty QueryParams (#816
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jakubmkowalski authored Dec 17, 2024
1 parent c9fe3f6 commit cabd218
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion actions/admin/contact_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func contactsSearchOld(c *gin.Context, _ *reqctx.AdminContext) {
return
}

reqParams.DefaultsIfNil()
reqParams.DefaultsIfNilOrEmpty()

contacts, err := engine.GetContacts(
c.Request.Context(),
Expand Down
4 changes: 2 additions & 2 deletions actions/contacts/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func search(c *gin.Context, userContext *reqctx.UserContext) {
return
}

reqParams.DefaultsIfNil()
reqParams.DefaultsIfNilOrEmpty()

contacts, err := engine.GetContactsByXpubID(
c.Request.Context(),
Expand Down Expand Up @@ -197,7 +197,7 @@ func searchContacts(c *gin.Context, reqXPubID string, paymail string) ([]*engine
return nil, 0
}

reqParams.DefaultsIfNil()
reqParams.DefaultsIfNilOrEmpty()

contacts, err := engine.GetContactsByXpubID(
c.Request.Context(),
Expand Down
10 changes: 7 additions & 3 deletions models/filter/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ type SearchModel[TFilter any] struct {
QueryParams *QueryParams `json:"params,omitempty" swaggertype:"object,string" example:"page:1,page_size:10,order_by_field:created_at,order_by_direction:desc"`
}

// DefaultsIfNil fills empty but necessary fields with default values
func (sm *SearchModel[TFilter]) DefaultsIfNil() {
if sm.QueryParams == nil {
// DefaultsIfNilOrEmpty fills empty but necessary fields with default values
func (sm *SearchModel[TFilter]) DefaultsIfNilOrEmpty() {
if sm.QueryParams == nil || sm.QueryParams.isEmpty() {
sm.QueryParams = DefaultQueryParams()
}
}

func (qp QueryParams) isEmpty() bool {
return qp == (QueryParams{})
}

0 comments on commit cabd218

Please sign in to comment.