This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/pkg/e621/builder/notes.go
SoXX 9cee99ecb5 docs: added more & unified documentation format
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-15 14:37:44 +01:00

152 lines
5.0 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 instance of NotesBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - NotesBuilder: An instance of the NotesBuilder interface.
func NewGetNotesBuilder(requestContext model.RequestContext) NotesBuilder {
return &getNotes{
requestContext: requestContext,
query: make(map[string]string),
}
}
// getNotes is an implementation of the NotesBuilder interface.
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.
//
// Parameters:
// - body: The text to search for in the notes' body.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
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.
//
// Parameters:
// - noteID: The ID of the note to be retrieved.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
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.
//
// Parameters:
// - tags: The tags to search for in the notes.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
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).
//
// Parameters:
// - creatorID: The ID of the user who created the notes.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
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).
//
// Parameters:
// - creatorName: The username of the user who created the notes.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
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.
//
// Parameters:
// - isActive: A boolean indicating whether to search for active notes (true) or inactive notes (false).
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
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.
//
// Parameters:
// - limitNotes: The maximum number of notes to retrieve.
//
// Returns:
// - NotesBuilder: The instance of the NotesBuilder for method chaining.
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.
//
// Returns:
// - []model.Note: A slice of notes.
// - error: An error, if any, encountered during the API request or response handling.
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
}