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