Files
pansy/internal/service/plants.go
T
steveandClaude Opus 4.8 94aed26e14
Build image / build-and-push (push) Successful in 15s
Address Gadfly review on seed lots
Three real bugs, two of which would have quietly corrupted inventory numbers.

CopyGarden carried seed_lot_id into the copied plantings, so copying a garden
double-counted the original lot's usage — the copy is a plan, not a second
planting out of the same packet. It also quietly attached a private lot to a
garden that may later be shared, contradicting the "lots are never shared with a
garden" invariant this feature was built on. The copy now drops the link.

RestorePlanting restored a seed_lot_id verbatim from a history snapshot. Live
rows get their link nulled by ON DELETE SET NULL when a lot is deleted, but the
snapshot still holds the old id, so undoing a planting deletion after retiring
its lot would violate the foreign key and abort the whole revert. The revert now
drops a dangling link before restoring: the plant really was in the ground,
which is the part worth restoring.

CreateSeedLot returned a lot with Used/Remaining at their zero values, so a
packet you just bought reported "0 remaining" — the exact opposite of the truth.
The version-conflict path had the same gap, and that row is what the client
rebases on, so a 409 reporting remaining=0 is worse than no number at all. Both
fill them now.

Remaining can go negative and that is deliberate — it means more was planted
than the lot recorded buying, which happens (a miscounted packet, a lot entered
after the fact). Clamping to zero would hide the discrepancy at exactly the
moment it is worth seeing. Now documented and tested rather than incidental.

Performance and tidiness: SeedLotUsage is scoped to the lots being filled
instead of scanning every planting the owner has, so reading one lot is one
lot's work; ListSeedLotsForOwner gained the LIMIT every other list read has;
parseNullable moved out of seed_lots.go into the shared request helpers, since
every nullable field in the API needs that three-way absent/null/value
distinction; finalizePlant validates against its own maxPlantVendorLen rather
than borrowing a constant named for seed lots; seedLotForPlanting became
checkSeedLotForPlanting, since it validates rather than fetches; and the
duplicate intPtr test helper gave way to the existing ptrInt.

Declined: recording seed-lot mutations in the revision history. change_sets are
anchored to a garden and lots are user-scoped, deliberately — they belong to
you, not to any one garden, so there is no garden to record them against.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01H3zbym8Doka2d7D48maSgZ
2026-07-21 01:38:54 -04:00

225 lines
7.2 KiB
Go

package service
import (
"context"
"strings"
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
)
// Plant field bounds. Spacing is mature in-row spacing in cm; the ceiling is a
// generous sanity limit (also rejecting NaN/Inf). Name/notes are length-capped so
// untrusted input can't balloon storage; icon holds one emoji (possibly a
// multi-codepoint ZWJ sequence, hence a byte cap rather than length 1).
const (
maxPlantNameLen = 200
maxPlantNotesLen = 10_000
maxPlantIconLen = 32
minPlantSpacingCM = 0.1
maxPlantSpacingCM = 10_000 // 100 m — headroom for large trees/shrubs
maxDaysToMaturity = 3650 // ~10 years
maxPlantVendorLen = 200
)
// plantCategories is the set of valid category enum values (mirrors the schema
// CHECK constraint in 0001_init.sql).
var plantCategories = map[string]struct{}{
domain.CategoryVegetable: {},
domain.CategoryHerb: {},
domain.CategoryFlower: {},
domain.CategoryFruit: {},
domain.CategoryTreeShrub: {},
domain.CategoryCover: {},
}
// PlantInput is the mutable field set for creating a plant. DaysToMaturity is
// optional (nil = unknown).
type PlantInput struct {
Name string
Category string
SpacingCM float64
Color string
Icon string
DaysToMaturity *int
// Where this variety came from — the page you'd go back to to buy it again.
// What you actually bought, and how much is left, is a SeedLot instead.
SourceURL string
Vendor string
Notes string
}
// PlantPatch is a partial update: nil fields are left unchanged. DaysToMaturity
// is itself nullable, so SetDays distinguishes "clear to NULL" (SetDays=true,
// value nil) from "leave unchanged" (SetDays=false) — a plain nil can't.
type PlantPatch struct {
Name *string
Category *string
SpacingCM *float64
Color *string
Icon *string
SetDays bool
DaysToMaturity *int
SourceURL *string
Vendor *string
Notes *string
}
// ListPlants returns the plants the actor can see: built-ins plus their own.
func (s *Service) ListPlants(ctx context.Context, actorID int64) ([]domain.Plant, error) {
return s.store.ListPlantsForActor(ctx, actorID)
}
// CreatePlant adds a plant owned by the actor. To "clone" a built-in the client
// simply POSTs a copy — there is no dedicated endpoint, and the copy is owned by
// (and editable by) the actor.
func (s *Service) CreatePlant(ctx context.Context, actorID int64, in PlantInput) (*domain.Plant, error) {
p := &domain.Plant{
Name: strings.TrimSpace(in.Name),
Category: in.Category,
SpacingCM: in.SpacingCM,
Color: in.Color,
Icon: strings.TrimSpace(in.Icon),
DaysToMaturity: in.DaysToMaturity,
SourceURL: strings.TrimSpace(in.SourceURL),
Vendor: strings.TrimSpace(in.Vendor),
Notes: strings.TrimSpace(in.Notes),
}
if err := finalizePlant(p); err != nil {
return nil, err
}
owner := actorID
p.OwnerID = &owner
return s.store.CreatePlant(ctx, p)
}
// writablePlant loads a plant and enforces that the actor may modify it. It is
// THE authorization point for plant mutation. Built-in rows (owner_id nil) are
// visible to everyone but read-only → ErrForbidden. A row owned by someone else
// is invisible → ErrNotFound (masking existence, as gardens/objects do). Only the
// actor's own rows are writable.
func (s *Service) writablePlant(ctx context.Context, actorID, plantID int64) (*domain.Plant, error) {
p, err := s.store.GetPlant(ctx, plantID)
if err != nil {
return nil, err // ErrNotFound
}
if p.OwnerID == nil {
return nil, domain.ErrForbidden // built-in: seeded and read-only
}
if *p.OwnerID != actorID {
return nil, domain.ErrNotFound // another user's plant: not visible
}
return p, nil
}
// UpdatePlant applies a partial, version-guarded patch to the actor's own plant.
// On a version mismatch it returns (current, ErrVersionConflict).
func (s *Service) UpdatePlant(ctx context.Context, actorID, plantID int64, patch PlantPatch, version int64) (*domain.Plant, error) {
p, err := s.writablePlant(ctx, actorID, plantID)
if err != nil {
return nil, err
}
applyPlantPatch(p, patch)
if err := finalizePlant(p); err != nil {
return nil, err
}
p.Version = version
return s.store.UpdatePlant(ctx, p)
}
// DeletePlant removes the actor's own plant. A plant still referenced by any
// planting (even a removed one — the FK is RESTRICT) is refused with
// ErrPlantInUse rather than orphaning history; the client clones-and-edits or
// clears the plantings first.
//
// Seed lots are refused for a different reason: seed_lots.plant_id is ON DELETE
// CASCADE, so without this check deleting a plant would silently destroy the
// purchase records attached to it — vendor, cost, germination rate, all gone
// with no warning. Refusing lets the owner delete the lots deliberately if that
// is really what they meant.
func (s *Service) DeletePlant(ctx context.Context, actorID, plantID int64) error {
if _, err := s.writablePlant(ctx, actorID, plantID); err != nil {
return err
}
n, err := s.store.CountPlantingsForPlant(ctx, plantID)
if err != nil {
return err
}
if n > 0 {
return domain.ErrPlantInUse
}
lots, err := s.store.CountSeedLotsForPlant(ctx, plantID)
if err != nil {
return err
}
if lots > 0 {
return domain.ErrPlantInUse
}
return s.store.DeletePlant(ctx, plantID)
}
// applyPlantPatch mutates p with each provided (non-nil) patch field.
func applyPlantPatch(p *domain.Plant, patch PlantPatch) {
if patch.Name != nil {
p.Name = strings.TrimSpace(*patch.Name)
}
if patch.Category != nil {
p.Category = *patch.Category
}
if patch.SpacingCM != nil {
p.SpacingCM = *patch.SpacingCM
}
if patch.Color != nil {
p.Color = *patch.Color
}
if patch.Icon != nil {
p.Icon = strings.TrimSpace(*patch.Icon)
}
if patch.SetDays {
p.DaysToMaturity = patch.DaysToMaturity
}
if patch.SourceURL != nil {
p.SourceURL = strings.TrimSpace(*patch.SourceURL)
}
if patch.Vendor != nil {
p.Vendor = strings.TrimSpace(*patch.Vendor)
}
if patch.Notes != nil {
p.Notes = strings.TrimSpace(*patch.Notes)
}
}
// finalizePlant validates a built/merged plant. Shared by create and update so
// both enforce the same invariants. isFinite/isHexColor live in objects.go.
func finalizePlant(p *domain.Plant) error {
if p.Name == "" || len(p.Name) > maxPlantNameLen {
return domain.ErrInvalidInput
}
if _, ok := plantCategories[p.Category]; !ok {
return domain.ErrInvalidInput
}
if !isFinite(p.SpacingCM) || p.SpacingCM < minPlantSpacingCM || p.SpacingCM > maxPlantSpacingCM {
return domain.ErrInvalidInput
}
if !isHexColor(p.Color) {
return domain.ErrInvalidInput
}
if p.Icon == "" || len(p.Icon) > maxPlantIconLen {
return domain.ErrInvalidInput
}
if p.DaysToMaturity != nil && (*p.DaysToMaturity < 1 || *p.DaysToMaturity > maxDaysToMaturity) {
return domain.ErrInvalidInput
}
if len(p.Notes) > maxPlantNotesLen {
return domain.ErrInvalidInput
}
if len(p.Vendor) > maxPlantVendorLen {
return domain.ErrInvalidInput
}
// Rendered as a clickable link, so the scheme check is load-bearing (see
// validSourceURL) rather than tidiness.
if !validSourceURL(p.SourceURL) {
return domain.ErrInvalidInput
}
return nil
}