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

99 lines
3.4 KiB
Go

package builder
import (
"context"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"strconv"
)
// NotesBuilder represents a builder for constructing queries to retrieve notes.
type NotesBuilder interface {
// SearchInBody sets the query to search for notes containing a specific text in their body.
SearchInBody(body string) NotesBuilder
// NoteID sets the query to search for a note with a specific note ID.
NoteID(noteID int) NotesBuilder
// SearchTags sets the query to search for notes containing specific tags.
SearchTags(tags string) NotesBuilder
// SearchCreatorID sets the query to search for notes created by a specific user (by their user ID).
SearchCreatorID(creatorID int) NotesBuilder
// SearchCreatorName sets the query to search for notes created by a specific user (by their username).
SearchCreatorName(creatorName string) NotesBuilder
// Active sets whether to search for active or inactive notes.
Active(isActive bool) NotesBuilder
// SetLimit sets the maximum number of notes to retrieve.
SetLimit(limitNotes int) NotesBuilder
// Execute sends the constructed query and returns a slice of notes and an error, if any.
Execute() ([]model.Note, error)
}
// NewGetNotesBuilder creates a new NotesBuilder with the provided RequestContext.
func NewGetNotesBuilder(requestContext model.RequestContext) NotesBuilder {
return &getNotes{
requestContext: requestContext,
query: make(map[string]string),
}
}
type getNotes struct {
requestContext model.RequestContext
query map[string]string
}
// SearchInBody sets the query to search for notes containing a specific text in their body.
func (g *getNotes) SearchInBody(body string) NotesBuilder {
g.query["search[body_matches]"] = body
return g
}
// NoteID sets the query to search for a note with a specific note ID.
func (g *getNotes) NoteID(noteID int) NotesBuilder {
g.query["search[post_id]"] = strconv.Itoa(noteID)
return g
}
// SearchTags sets the query to search for notes containing specific tags.
func (g *getNotes) SearchTags(tags string) NotesBuilder {
g.query["search[post_tags_match]"] = tags
return g
}
// SearchCreatorID sets the query to search for notes created by a specific user (by their user ID).
func (g *getNotes) SearchCreatorID(creatorID int) NotesBuilder {
g.query["search[creator_id]"] = strconv.Itoa(creatorID)
return g
}
// SearchCreatorName sets the query to search for notes created by a specific user (by their username).
func (g *getNotes) SearchCreatorName(creatorName string) NotesBuilder {
g.query["search[creator_name]"] = creatorName
return g
}
// Active sets whether to search for active or inactive notes.
func (g *getNotes) Active(isActive bool) NotesBuilder {
g.query["search[is_active]"] = strconv.FormatBool(isActive)
return g
}
// SetLimit sets the maximum number of notes to retrieve.
func (g *getNotes) SetLimit(limitNotes int) NotesBuilder {
g.query["limit"] = strconv.Itoa(limitNotes)
return g
}
// Execute sends the constructed query and returns a slice of notes and an error, if any.
func (g *getNotes) Execute() ([]model.Note, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())
if err != nil {
return nil, err
}
}
notes, err := endpoints.GetNotes(g.requestContext, g.query)
if err != nil {
return nil, err
}
return notes, nil
}