library

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound        = errors.New("library entry not found")
	ErrWorkNotFound    = errors.New("work not found")
	ErrInvalidStatus   = errors.New("invalid status")
	ErrInvalidRating   = errors.New("invalid rating")
	ErrInvalidProgress = errors.New("invalid progress percent")
)

Functions

This section is empty.

Types

type Handler

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

func NewHandler

func NewHandler(s *Service) *Handler

func (*Handler) Delete

func (h *Handler) Delete(w http.ResponseWriter, r *http.Request)

func (*Handler) Get

func (h *Handler) Get(w http.ResponseWriter, r *http.Request)

func (*Handler) List

func (h *Handler) List(w http.ResponseWriter, r *http.Request)

List returns the calling user's library entries, newest-touched first. Optional ?status= and ?shelf= filters.

func (*Handler) Upsert

func (h *Handler) Upsert(w http.ResponseWriter, r *http.Request)

type ListFilter

type ListFilter struct {
	UserID uint
	Status Status
	Shelf  string
	Limit  int
	Offset int
}

ListFilter narrows List results. Zero-valued fields (other than UserID, which is always required by the handler) are ignored.

type Service

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

func NewService

func NewService(db *gorm.DB) *Service

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, userID uint, workID uuid.UUID) error

Delete removes the user's entry for a work. Returns ErrNotFound if there was nothing to delete, so the handler can 404 rather than 200 a no-op.

func (*Service) Get

func (s *Service) Get(ctx context.Context, userID uint, workID uuid.UUID) (*UserBook, error)

func (*Service) List

func (s *Service) List(ctx context.Context, f ListFilter) ([]UserBook, error)

func (*Service) Upsert

func (s *Service) Upsert(ctx context.Context, userID uint, workID uuid.UUID, req UpsertRequest) (*UserBook, error)

Upsert creates or updates the calling user's entry for a work. On first touch the entry defaults to status "want"; subsequent calls patch only the fields present in req. Validates the enum/rating/percent before writing. Status transitions auto-stamp StartedAt (->reading) and FinishedAt (->read), idempotently — an existing stamp is never overwritten.

type Status

type Status string
const (
	StatusWant    Status = "want"
	StatusReading Status = "reading"
	StatusRead    Status = "read"
)

type UpsertRequest

type UpsertRequest struct {
	Status          *string `json:"status"`
	Shelf           *string `json:"shelf"`
	CurrentPage     *int    `json:"current_page"`
	TotalPages      *int    `json:"total_pages"`
	ProgressPercent *int    `json:"progress_percent"`
	Rating          *int    `json:"rating"`
	Notes           *string `json:"notes"`
}

UpsertRequest is a partial patch: every field is a pointer so an absent field is left untouched, while a present field (including an explicit null Rating) is applied. PUT /me/library/{work_id} decodes into this.

type UserBook

type UserBook struct {
	ID              uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
	UserID          uint      `gorm:"not null;uniqueIndex:idx_user_work" json:"user_id"`
	WorkID          uuid.UUID `gorm:"type:uuid;not null;uniqueIndex:idx_user_work;index" json:"work_id"`
	Status          Status    `gorm:"not null;index;default:want" json:"status"`
	Shelf           string    `gorm:"index" json:"shelf,omitempty"`
	CurrentPage     int       `json:"current_page,omitempty"`
	TotalPages      int       `json:"total_pages,omitempty"`
	ProgressPercent int       `json:"progress_percent,omitempty"`
	// Rating is a pointer so "unrated" (nil) is distinct from a 0; it also lets
	// an upsert request clear a previously-set rating by sending null.
	Rating     *int       `json:"rating,omitempty"` // 1..5 when set
	Notes      string     `json:"notes,omitempty"`
	StartedAt  *time.Time `json:"started_at,omitempty"`
	FinishedAt *time.Time `json:"finished_at,omitempty"`
	CreatedAt  time.Time  `json:"created_at"`
	UpdatedAt  time.Time  `json:"updated_at"`

	// Work is enriched on read (gorm:"-") so the My Library list renders the
	// title/authors/editions without a second round-trip. Unlike the
	// contribution package — which kept itself decoupled from catalog — the
	// library legitimately *composes* works, so we embed the full Work. The
	// dependency is acyclic: catalog never imports library.
	Work *catalog.Work `gorm:"-" json:"work,omitempty"`
}

UserBook is one user's personal-library entry for a Work: their reading status, an optional free-form shelf, reading progress, a rating, and private notes. Exactly one row per (user, work) — enforced by the composite uniqueIndex, which is also the upsert key.

Jump to

Keyboard shortcuts

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