e621-sdk-go/pkg/e621/builder/pools.go

115 lines
4.0 KiB
Go
Raw Normal View History

package builder
import (
"context"
"git.anthrove.art/Anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.anthrove.art/Anthrove/e621-sdk-go/pkg/e621/model"
"strconv"
)
// PoolsBuilder represents a builder for constructing queries to retrieve pools.
type PoolsBuilder interface {
// SearchName sets the query to search for pools with a specific name.
SearchName(name string) PoolsBuilder
// SearchID sets the query to search for pools with specific pool IDs.
SearchID(poolIDs string) PoolsBuilder
// SearchDescription sets the query to search for pools with specific descriptions.
SearchDescription(description string) PoolsBuilder
// SearchCreatorID sets the query to search for pools created by a specific user (by their user ID).
SearchCreatorID(creatorID int) PoolsBuilder
// SearchCreatorName sets the query to search for pools created by a specific user (by their username).
SearchCreatorName(creatorName string) PoolsBuilder
// Active sets whether to search for active or inactive pools.
Active(isActive bool) PoolsBuilder
// SearchCategory sets the query to search for pools in a specific category.
SearchCategory(category model.PoolCategory) PoolsBuilder
// Order sets the ordering of the retrieved pools.
Order(order model.PoolOrder) PoolsBuilder
// SetLimit sets the maximum number of pools to retrieve.
SetLimit(limitPools int) PoolsBuilder
// Execute sends the constructed query and returns a slice of pools and an error, if any.
Execute() ([]model.Pool, error)
}
// NewGetPoolsBuilder creates a new PoolsBuilder with the provided RequestContext.
func NewGetPoolsBuilder(requestContext model.RequestContext) PoolsBuilder {
return &getPools{
requestContext: requestContext,
query: make(map[string]string),
}
}
type getPools struct {
requestContext model.RequestContext
query map[string]string
}
// SearchName sets the query to search for pools with a specific name.
func (g *getPools) SearchName(name string) PoolsBuilder {
g.query["search[name_matches]"] = name
return g
}
// SearchID sets the query to search for pools with specific pool IDs.
func (g *getPools) SearchID(poolIDs string) PoolsBuilder {
g.query["search[id]"] = poolIDs
return g
}
// SearchDescription sets the query to search for pools with specific descriptions.
func (g *getPools) SearchDescription(description string) PoolsBuilder {
g.query["search[description_matches]"] = description
return g
}
// SearchCreatorID sets the query to search for pools created by a specific user (by their user ID).
func (g *getPools) SearchCreatorID(creatorID int) PoolsBuilder {
g.query["search[creator_id]"] = strconv.Itoa(creatorID)
return g
}
// SearchCreatorName sets the query to search for pools created by a specific user (by their username).
func (g *getPools) SearchCreatorName(creatorName string) PoolsBuilder {
g.query["search[creator_name]"] = creatorName
return g
}
// Active sets whether to search for active or inactive pools.
func (g *getPools) Active(isActive bool) PoolsBuilder {
g.query["search[is_active]"] = strconv.FormatBool(isActive)
return g
}
// SearchCategory sets the query to search for pools in a specific category.
func (g *getPools) SearchCategory(category model.PoolCategory) PoolsBuilder {
g.query["search[category]"] = string(category)
return g
}
// Order sets the ordering of the retrieved pools.
func (g *getPools) Order(order model.PoolOrder) PoolsBuilder {
g.query["search[order]"] = string(order)
return g
}
// SetLimit sets the maximum number of pools to retrieve.
func (g *getPools) SetLimit(limitPools int) PoolsBuilder {
g.query["limit"] = strconv.Itoa(limitPools)
return g
}
// Execute sends the constructed query and returns a slice of pools and an error, if any.
func (g *getPools) Execute() ([]model.Pool, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())
if err != nil {
return nil, err
}
}
pools, err := endpoints.GetPools(g.requestContext, g.query)
if err != nil {
return nil, err
}
return pools, nil
}