Documentation
¶
Index ¶
- Variables
- type Handler
- type ListFilter
- type Service
- func (s *Service) Delete(ctx context.Context, userID uint, workID uuid.UUID) error
- func (s *Service) Get(ctx context.Context, userID uint, workID uuid.UUID) (*UserBook, error)
- func (s *Service) List(ctx context.Context, f ListFilter) ([]UserBook, error)
- func (s *Service) Upsert(ctx context.Context, userID uint, workID uuid.UUID, req UpsertRequest) (*UserBook, error)
- type Status
- type UpsertRequest
- type UserBook
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶
type ListFilter ¶
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 (*Service) Delete ¶
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) 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 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.