2023-10-23 08:21:51 +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 08:21:51 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UsersBuilder interface {
|
|
|
|
SetPage(pageNumber int) UsersBuilder
|
|
|
|
SetLimit(limitUser int) UsersBuilder
|
2023-10-24 13:10:39 +00:00
|
|
|
SearchByName(userName string) UsersBuilder
|
|
|
|
SearchByAbout(about string) UsersBuilder
|
|
|
|
SearchByAvatarID(postID model.PostID) UsersBuilder
|
|
|
|
SearchByLevel(level model.UserLevel) UsersBuilder
|
|
|
|
SearchByMinLevel(level model.UserLevel) UsersBuilder
|
|
|
|
SearchByMaxLevellevel(model.UserLevel) UsersBuilder
|
|
|
|
searchByCanUpload(canUpload bool) UsersBuilder
|
|
|
|
SearchByIsApprover(isApprover bool) UsersBuilder
|
|
|
|
SearchByOrder(order model.Order) UsersBuilder
|
2023-10-23 08:21:51 +00:00
|
|
|
Execute() ([]model.User, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGetUsersBuilder(requestContext model.RequestContext) UsersBuilder {
|
|
|
|
return &getUsers{requestContext: requestContext, query: make(map[string]string)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type getUsers struct {
|
|
|
|
requestContext model.RequestContext
|
|
|
|
query map[string]string
|
|
|
|
}
|
|
|
|
|
2023-10-24 13:10:39 +00:00
|
|
|
func (g *getUsers) SearchByName(userName string) UsersBuilder {
|
|
|
|
g.query["search[name_matches]"] = userName
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *getUsers) SearchByAbout(about string) UsersBuilder {
|
|
|
|
g.query["search[about_me"] = about
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *getUsers) SearchByAvatarID(postID model.PostID) UsersBuilder {
|
|
|
|
g.query["search[avatar_id]"] = strconv.FormatInt(int64(postID), 10)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *getUsers) SearchByLevel(level model.UserLevel) UsersBuilder {
|
|
|
|
g.query["search[level]"] = strconv.Itoa(int(level))
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *getUsers) SearchByMinLevel(level model.UserLevel) UsersBuilder {
|
|
|
|
g.query["search[min_level"] = strconv.Itoa(int(level))
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *getUsers) SearchByMaxLevellevel(level model.UserLevel) UsersBuilder {
|
|
|
|
g.query["search[max_level]"] = strconv.Itoa(int(level))
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *getUsers) searchByCanUpload(canUpload bool) UsersBuilder {
|
|
|
|
g.query["search[can_upload_free]"] = strconv.FormatBool(canUpload)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *getUsers) SearchByIsApprover(isApprover bool) UsersBuilder {
|
|
|
|
g.query["search[can_approve_posts]"] = strconv.FormatBool(isApprover)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *getUsers) SearchByOrder(order model.Order) UsersBuilder {
|
|
|
|
g.query["search[order]"] = string(order)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-10-23 08:21:51 +00:00
|
|
|
func (g *getUsers) SetPage(pageNumber int) UsersBuilder {
|
|
|
|
g.query["page"] = strconv.Itoa(pageNumber)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *getUsers) SetLimit(limitUser int) UsersBuilder {
|
|
|
|
g.query["limit"] = strconv.Itoa(limitUser)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *getUsers) Execute() ([]model.User, 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 08:21:51 +00:00
|
|
|
users, err := endpoints.GetUsers(g.requestContext, g.query)
|
|
|
|
if err != nil {
|
2023-10-24 08:35:33 +00:00
|
|
|
|
2023-10-23 08:21:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|