Skip to content

Commit

Permalink
feat(bux-685): Added static filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazarii-4chain committed Apr 3, 2024
1 parent 864a530 commit 6b6cb29
Show file tree
Hide file tree
Showing 11 changed files with 400 additions and 1,253 deletions.
42 changes: 42 additions & 0 deletions actions/destinations/methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package destinations

import (
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/engine/datastore"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/gin-gonic/gin"
)

// SearchRequestDestinationParameters is a struct for handling request parameters for search requests
type SearchRequestDestinationParameters struct {
// Custom conditions used for filtering the search results
Conditions models.DestinationFilters `json:"conditions"`
// Accepts a JSON object for embedding custom metadata, enabling arbitrary additional information to be associated with the resource
Metadata engine.Metadata `json:"metadata" swaggertype:"object,string" example:"key:value,key2:value2"`
// Pagination and sorting options to streamline data exploration and analysis
QueryParams datastore.QueryParams `json:"params" swaggertype:"object,string" example:"page:1,page_size:10,order_by_field:created_at,order_by_direction:desc"`
}

// CountRequestDestinationParameters is a struct for handling request parameters for count requests
type CountRequestDestinationParameters struct {
// Custom conditions used for filtering the search results
Conditions map[string]interface{} `json:"conditions" swaggertype:"object,string" example:"testColumn:testValue"`
// Accepts a JSON object for embedding custom metadata, enabling arbitrary additional information to be associated with the resource
Metadata engine.Metadata `json:"metadata" swaggertype:"object,string" example:"key:value,key2:value2"`
}

// GetSearchDestinationQueryParameters get all filtering parameters related to the db query
func GetSearchDestinationQueryParameters(c *gin.Context) (*datastore.QueryParams, *engine.Metadata, *models.DestinationFilters, error) {
var requestParameters SearchRequestDestinationParameters
if err := c.Bind(&requestParameters); err != nil {
return nil, nil, nil, err
}

conditions := *models.NewDestinationFilters()

if requestParameters.Conditions == (models.DestinationFilters{}) {
requestParameters.Conditions = conditions
}

return &requestParameters.QueryParams, &requestParameters.Metadata, &requestParameters.Conditions, nil
}
14 changes: 4 additions & 10 deletions actions/destinations/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package destinations
import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/actions"
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/engine/utils"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/server/auth"
Expand All @@ -18,25 +16,21 @@ import (
// @Description Search for a destination
// @Tags Destinations
// @Produce json
// @Param SearchRequestParameters body actions.SearchRequestParameters false "Supports targeted resource searches with filters for metadata and custom conditions, plus options for pagination and sorting to streamline data exploration and analysis"
// @Param SearchRequestDestinationParameters body SearchRequestDestinationParameters false "Supports targeted resource searches with filters for metadata and custom conditions, plus options for pagination and sorting to streamline data exploration and analysis"
// @Success 200 {object} []models.Destination "List of destinations
// @Failure 400 "Bad request - Error while parsing SearchRequestParameters from request body"
// @Failure 400 "Bad request - Error while parsing SearchRequestDestinationParameters from request body"
// @Failure 500 "Internal server error - Error while searching for destinations"
// @Router /v1/destination/search [post]
// @Security x-auth-xpub
func (a *Action) search(c *gin.Context) {
reqXPubID := c.GetString(auth.ParamXPubHashKey)

queryParams, metadata, conditions, err := actions.GetSearchQueryParameters(c)
queryParams, metadata, conditions, err := GetSearchDestinationQueryParameters(c)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

var destFilters models.DestinationFilters

destConditions, err := utils.FilterMapByStructFields(*conditions, &destFilters)

if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
Expand All @@ -47,7 +41,7 @@ func (a *Action) search(c *gin.Context) {
c.Request.Context(),
reqXPubID,
metadata,
&destConditions,
conditions,
queryParams,
); err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
Expand Down
Loading

0 comments on commit 6b6cb29

Please # to comment.