publishers

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidParent      = errors.New("invalid parent: parent_id must be a positive integer")
	ErrParentOtherLibrary = errors.New("parent publisher must be in the same library")
	ErrParentCycle        = errors.New("cannot set parent: would create a cycle")
)

SetParent and ValidateNoCycle return these for a parent the hierarchy cannot accept, which the handlers render as a 422. A publisher that does not exist is errcodes.NotFound instead.

Functions

func RegisterRoutes added in v0.1.0

func RegisterRoutes(g *echo.Group, db *bun.DB, authMiddleware *auth.Middleware, reviewRecomputer review.BookReviewRecomputer)

RegisterRoutes registers publisher routes on a group the server has already configured with authentication and the resource's read permission. reviewRecomputer is required; pass a books service with app settings attached, or the delete handler's review recompute does nothing.

Types

type AncestorResponse added in v0.0.47

type AncestorResponse struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

AncestorResponse is a lightweight ancestor node in a publisher's hierarchy chain, ordered from immediate parent to root in PublisherResponse.Ancestors.

type ChildResponse added in v0.0.47

type ChildResponse struct {
	ID        int    `json:"id"`
	Name      string `json:"name"`
	FileCount int    `json:"file_count"`
}

ChildResponse is a flattened direct child of a publisher, carrying its own direct file count. It replaces the model's Children []*Publisher relation in API responses.

type ListPublisherFilesResponse added in v0.0.47

type ListPublisherFilesResponse struct {
	Items []*models.File `json:"items" tstype:"File[]"`
	Total int            `json:"total"`
}

ListPublisherFilesResponse is the envelope for the files sub-resource. The tstype:"File[]" override makes tygo emit `File[]` (imported via the publishers frontmatter) instead of `(File | undefined)[]` for the []*models.File slice.

type ListPublishersOptions

type ListPublishersOptions struct {
	Limit      *int
	Offset     *int
	LibraryID  *int
	LibraryIDs []int // Filter by multiple library IDs (for access control)
	Search     *string
	ExcludeIDs []int                // Exclude specific publisher IDs from results
	Sort       []sortspec.SortLevel // Defaults to name ascending
	// contains filtered or unexported fields
}

type ListPublishersQuery

type ListPublishersQuery struct {
	Limit      int     `query:"limit" json:"limit,omitempty" default:"24" validate:"min=1,max=100"`
	Offset     int     `query:"offset" json:"offset,omitempty" validate:"min=0"`
	LibraryID  *int    `query:"library_id" json:"library_id,omitempty" validate:"omitempty,min=1" tstype:"number"`
	Search     *string `query:"search" json:"search,omitempty" validate:"omitempty,max=100" tstype:"string"`
	ExcludeIDs []int   `query:"exclude_ids" json:"exclude_ids,omitempty" validate:"omitempty,dive,min=1" tstype:"number[]"`
	Sort       string  `query:"sort" json:"sort,omitempty" validate:"omitempty,max=200"`
}

type ListPublishersResponse added in v0.0.47

type ListPublishersResponse struct {
	Items []PublisherListItem `json:"items"`
	Total int                 `json:"total"`
}

ListPublishersResponse is the list-endpoint envelope.

type MergePublishersPayload

type MergePublishersPayload struct {
	SourceID int `json:"source_id" validate:"required,min=1"`
}

type PublisherListItem added in v0.0.47

type PublisherListItem struct {
	models.Publisher         `tstype:",extends"`
	FileCount                int      `json:"file_count"`
	DescendantFileCount      int      `json:"descendant_file_count"`
	DescendantPublisherCount int      `json:"descendant_publisher_count"`
	ParentName               *string  `json:"parent_name"`
	Aliases                  []string `json:"aliases"`
	// The covers the list shows for this publisher, including its
	// sub-publishers' books.
	PreviewBooks []bookpreviews.PreviewBook `json:"preview_books" tstype:"PreviewBook[]"`
}

PublisherListItem is the light list-row shape returned by GET /publishers. It deliberately omits the full hierarchy (ancestors, flattened children, descendant ids) — computing those per row would be an N+1 — and instead carries the parent's name and rolled-up counts. It embeds the Publisher model by value (so tygo emits `extends Publisher`) and reshapes aliases to []string.

type PublisherResponse added in v0.0.47

type PublisherResponse struct {
	models.Publisher    `tstype:",extends"`
	FileCount           int                `json:"file_count"`
	DescendantFileCount int                `json:"descendant_file_count"`
	Aliases             []string           `json:"aliases"`
	Ancestors           []AncestorResponse `json:"ancestors"`
	DescendantIDs       []int              `json:"descendant_ids"`
	Children            []ChildResponse    `json:"children"`
}

PublisherResponse is the full single-publisher API response used by retrieve, update, and merge. It embeds the Publisher model by value (so tygo emits `extends Publisher`) and reshapes the hierarchy: aliases as a flat []string, ancestors and flattened children, and descendant ids. FileCount and Aliases shadow the embedded model's same-json-tag fields, keeping the wire format byte-identical to the previous anonymous struct.

type RetrievePublisherOptions

type RetrievePublisherOptions struct {
	ID        *int
	Name      *string
	LibraryID *int
}

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(db *bun.DB) *Service

func (*Service) CleanupOrphanedPublishers

func (svc *Service) CleanupOrphanedPublishers(ctx context.Context) ([]int, error)

CleanupOrphanedPublishers deletes publishers with no file associations and no children, returning the IDs of deleted publishers. Callers must pass the returned IDs to searchService.DeleteFromPublisherIndex to keep publishers_fts in sync. Publishers that serve as parents in the hierarchy are preserved even if they have no direct file associations.

func (*Service) CreatePublisher

func (svc *Service) CreatePublisher(ctx context.Context, publisher *models.Publisher) error

func (*Service) DeletePublisher

func (svc *Service) DeletePublisher(ctx context.Context, publisherID int) ([]int, error)

DeletePublisher deletes a publisher and leaves a protected manual empty slot on every file that pointed at it directly: publisher_id becomes NULL and publisher_source becomes manual, whatever the prior source was. The delete is a deliberate user action, so an ordinary Scan must not bring the publisher back, while Refresh all metadata and Reset to file metadata still may (ADR 0006). Only this service path is supported. The foreign key's ON DELETE SET NULL leaves publisher_source untouched.

It returns the IDs of the Books that own an affected file, each once, so the caller can recompute their review state after commit.

func (*Service) FindOrCreatePublisher

func (svc *Service) FindOrCreatePublisher(ctx context.Context, name string, libraryID int) (*models.Publisher, error)

FindOrCreatePublisher finds an existing publisher or creates a new one (case-insensitive match).

func (*Service) GetAncestors added in v0.0.45

func (svc *Service) GetAncestors(ctx context.Context, publisherID int) ([]*models.Publisher, error)

GetAncestors returns the ancestor chain for a publisher, ordered from immediate parent to root.

func (*Service) GetChildren added in v0.0.45

func (svc *Service) GetChildren(ctx context.Context, publisherID int) ([]*models.Publisher, error)

GetChildren returns the direct children of a publisher with their file counts.

func (*Service) GetDescendantFileCount added in v0.0.45

func (svc *Service) GetDescendantFileCount(ctx context.Context, publisherID int) (int, error)

GetDescendantFileCount returns the count of files attached to any descendant of the given publisher (not including the publisher's own direct files).

func (*Service) GetDescendantIDs added in v0.0.45

func (svc *Service) GetDescendantIDs(ctx context.Context, publisherID int) ([]int, error)

GetDescendantIDs returns all descendant IDs of a publisher (children, grandchildren, etc.) using a breadth-first traversal.

func (*Service) GetDescendantPublisherCount added in v0.0.45

func (svc *Service) GetDescendantPublisherCount(ctx context.Context, publisherID int) (int, error)

GetDescendantPublisherCount returns the count of all publishers in the subtree rooted at publisherID (not including the publisher itself).

func (*Service) GetFileCount

func (svc *Service) GetFileCount(ctx context.Context, publisherID int) (int, error)

GetFileCount returns the count of files with this publisher and all its descendants.

func (*Service) GetFileCountForPublisherIDs added in v0.0.45

func (svc *Service) GetFileCountForPublisherIDs(ctx context.Context, publisherIDs []int) (int, error)

GetFileCountForPublisherIDs returns the count of files attached to any of the given publisher IDs. Useful when descendant IDs have already been computed.

func (*Service) GetFiles

func (svc *Service) GetFiles(ctx context.Context, publisherID int) ([]*models.File, error)

GetFiles returns all files with this publisher and all its descendants.

func (*Service) GetFilesPaginated added in v0.0.44

func (svc *Service) GetFilesPaginated(ctx context.Context, publisherID, limit, offset int) ([]*models.File, int, error)

GetFilesPaginated returns a paginated list of files with this publisher and all its descendants.

func (*Service) ListPublishers

func (svc *Service) ListPublishers(ctx context.Context, opts ListPublishersOptions) ([]*models.Publisher, error)

func (*Service) ListPublishersWithTotal

func (svc *Service) ListPublishersWithTotal(ctx context.Context, opts ListPublishersOptions) ([]*models.Publisher, int, error)

func (*Service) MergePublishers

func (svc *Service) MergePublishers(ctx context.Context, targetID, sourceID int) error

MergePublishers merges sourcePublisher into targetPublisher (moves all file associations, re-parents the source's children to the target, deletes source). A Publisher cannot be merged into itself.

When the target sits anywhere below the source, it first takes the source's place under the source's parent (or becomes a root when the source has none). Otherwise re-parenting the source's children to the target would make the target's own ancestor its child while it stayed that ancestor's descendant, a cycle.

func (*Service) RetrievePublisher

func (svc *Service) RetrievePublisher(ctx context.Context, opts RetrievePublisherOptions) (*models.Publisher, error)

func (*Service) SetParent added in v0.0.45

func (svc *Service) SetParent(ctx context.Context, publisherID int, parentID *int) error

SetParent sets or clears the parent of a publisher. If parentID is non-nil, it validates that setting the parent would not create a cycle.

func (*Service) UpdatePublisher

func (svc *Service) UpdatePublisher(ctx context.Context, publisher *models.Publisher, opts UpdatePublisherOptions) error

func (*Service) ValidateNoCycle added in v0.0.45

func (svc *Service) ValidateNoCycle(ctx context.Context, publisherID, proposedParentID int) error

ValidateNoCycle walks the ancestor chain of proposedParentID and rejects the operation if publisherID is found (which would create a cycle). It also rejects self-references (publisherID == proposedParentID).

type SetChildPayload added in v0.0.45

type SetChildPayload struct {
	ChildID int `json:"child_id" validate:"required,min=1"`
}

type SubResourceQuery added in v0.0.44

type SubResourceQuery struct {
	Limit  int `query:"limit" json:"limit,omitempty" default:"24" validate:"min=1,max=100"`
	Offset int `query:"offset" json:"offset,omitempty" validate:"min=0"`
}

type UpdatePublisherOptions

type UpdatePublisherOptions struct {
	Columns []string
}

type UpdatePublisherPayload

type UpdatePublisherPayload struct {
	Name       *string              `json:"name,omitempty" validate:"omitempty,min=1,max=300"`
	Aliases    []string             `json:"aliases,omitempty" validate:"omitempty,dive,min=1,max=300"`
	ParentID   httputil.NullableInt `json:"parent_id,omitempty" tstype:"number | null"`
	ParentName *string              `json:"parent_name,omitempty" validate:"omitempty,min=1,max=300"`
}

UpdatePublisherPayload is the request body for PATCH /publishers/:id. tstype is applied to the generated TypeScript type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址