2023-10-23 14:01:58 +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 14:01:58 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// FavoritesBuilder represents a builder for constructing queries to retrieve user's favorite posts.
|
2023-10-23 14:01:58 +00:00
|
|
|
type FavoritesBuilder interface {
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetUserID sets the user ID for the query.
|
2023-10-24 13:10:39 +00:00
|
|
|
SetUserID(userID model.UserID) FavoritesBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetLimit sets the maximum number of favorite posts to retrieve.
|
2023-10-23 14:01:58 +00:00
|
|
|
SetLimit(limitFavorites int) FavoritesBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// Page sets the page number for paginated results.
|
|
|
|
Page(pageNumber int) FavoritesBuilder
|
|
|
|
// Execute sends the constructed query and returns a slice of favorite posts and an error if any.
|
2023-10-23 14:01:58 +00:00
|
|
|
Execute() ([]model.Post, error)
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// NewGetFavoritesBuilder creates a new FavoritesBuilder with the provided RequestContext.
|
2023-10-23 14:01:58 +00:00
|
|
|
func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilder {
|
|
|
|
return &getFavorites{
|
|
|
|
requestContext: requestContext,
|
|
|
|
query: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type getFavorites struct {
|
|
|
|
query map[string]string
|
|
|
|
requestContext model.RequestContext
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetUserID sets the user ID for the query.
|
2023-11-06 14:26:47 +00:00
|
|
|
func (g *getFavorites) SetUserID(userID model.UserID) FavoritesBuilder {
|
2023-10-24 13:10:39 +00:00
|
|
|
g.query["user_id"] = strconv.Itoa(int(userID))
|
2023-10-23 14:01:58 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetLimit sets the maximum number of favorite posts to retrieve.
|
2023-11-06 14:26:47 +00:00
|
|
|
func (g *getFavorites) SetLimit(limitFavorites int) FavoritesBuilder {
|
2023-10-23 14:01:58 +00:00
|
|
|
g.query["limit"] = strconv.Itoa(limitFavorites)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// Page sets the page number for paginated results.
|
2023-11-06 14:26:47 +00:00
|
|
|
func (g *getFavorites) Page(pageNumber int) FavoritesBuilder {
|
|
|
|
g.query["page"] = strconv.Itoa(pageNumber)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// Execute sends the constructed query and returns a slice of favorite posts and an error if any.
|
2023-11-06 14:26:47 +00:00
|
|
|
func (g *getFavorites) Execute() ([]model.Post, 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 14:01:58 +00:00
|
|
|
favorites, err := endpoints.GetFavorites(g.requestContext, g.query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return favorites, nil
|
|
|
|
}
|