Season view: filter the editor to a year (#54)
Build image / build-and-push (push) Successful in 9s
Build image / build-and-push (push) Successful in 9s
"Change the garlic bed to cucumbers this year" has a year in it, and pansy had no notion of one — the editor showed whatever is currently planted, full stop. The data has always supported seasons: plantings carry planted_at and removed_at, and "clear bed" soft-removes rather than deleting. A season is a date range over data that already exists. No schema change, and specifically no seasons table — it would duplicate what the dates already say and create a second source of truth about when something was in the ground. ?year=YYYY on /full returns every plop whose [planted_at, removed_at] interval overlapped that calendar year, so garlic planted in October and pulled the following July appears in BOTH years, which is what actually happened. Undated plantings appear in every year: everything that predates this feature has a null planted_at, and a rule that excluded them would empty every existing garden the moment a year was selected. Without the param /full behaves exactly as before — the existing tests pass unchanged, which was the point of doing it this way. Widening to past plops means widening the referenced-plant lookup with it. A plant pulled last July isn't active, but its plops still have to render with the right icon and colour, so ListReferencedPlants takes an includeRemoved flag that tracks the same switch. A past season is READ-ONLY, gated at the single canEdit the palette, inspector, nudging, placement and drag handles all key off. Editing the past by accident is the failure mode this feature introduces, so the state is stated in a banner rather than implied by a dropdown you set a while ago, with the way back to the live garden next to it. The season is a separate query under its own key. The optimistic mutations all patch gardenFullKey(gardenId); folding a year into that key would let them write into whichever season happened to be on screen. Read-only views never need that machinery, and keeping them out of it means they can't accidentally join it. The year selector offers only years the garden holds data for, plus the current one. A free numeric field invites a typo, and a typo'd year produces a confidently empty garden that reads as data loss rather than a mistake — the server bounds the year for the same reason. Closes #54 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01H3zbym8Doka2d7D48maSgZ
This commit is contained in:
@@ -62,6 +62,68 @@ func queryPlantings(ctx context.Context, q queryer, query string, args ...any) (
|
||||
return plantings, nil
|
||||
}
|
||||
|
||||
// ListPlantingsForGardenYear returns every plop in a garden whose time in the
|
||||
// ground overlaps the given calendar year — the season view (#54).
|
||||
//
|
||||
// The interval is [planted_at, removed_at]: planted before the year ended, and
|
||||
// either still in the ground or removed after the year began. Garlic planted in
|
||||
// October and pulled the following July therefore appears in BOTH years, which
|
||||
// is what actually happened.
|
||||
//
|
||||
// Undated plantings are always included. Every planting that predates this
|
||||
// feature has a null planted_at, and a rule that excluded them would empty every
|
||||
// existing garden the moment a year was selected — an unhelpful way to be
|
||||
// technically correct.
|
||||
func (d *DB) ListPlantingsForGardenYear(ctx context.Context, gardenID int64, year int) ([]domain.Planting, error) {
|
||||
start := fmt.Sprintf("%04d-01-01", year)
|
||||
end := fmt.Sprintf("%04d-12-31", year)
|
||||
return queryPlantings(ctx, d.sql,
|
||||
`SELECT `+qualifyColumns("pl", plantingColumns)+` FROM plantings pl
|
||||
JOIN garden_objects o ON o.id = pl.object_id
|
||||
WHERE o.garden_id = ?
|
||||
AND (pl.planted_at IS NULL OR pl.planted_at <= ?)
|
||||
AND (pl.removed_at IS NULL OR pl.removed_at >= ?)
|
||||
ORDER BY pl.id`,
|
||||
gardenID, end, start)
|
||||
}
|
||||
|
||||
// GardenPlantingYears lists the calendar years a garden has planting data for,
|
||||
// newest first — every year touched by a [planted_at, removed_at] interval.
|
||||
//
|
||||
// Undated plantings contribute no year, deliberately: they belong to every year
|
||||
// the selector offers, so adding one here would be noise.
|
||||
func (d *DB) GardenPlantingYears(ctx context.Context, gardenID int64) ([]int, error) {
|
||||
rows, err := d.sql.QueryContext(ctx,
|
||||
`SELECT DISTINCT CAST(substr(y, 1, 4) AS INTEGER) AS year FROM (
|
||||
SELECT pl.planted_at AS y FROM plantings pl
|
||||
JOIN garden_objects o ON o.id = pl.object_id
|
||||
WHERE o.garden_id = ? AND pl.planted_at IS NOT NULL
|
||||
UNION
|
||||
SELECT pl.removed_at AS y FROM plantings pl
|
||||
JOIN garden_objects o ON o.id = pl.object_id
|
||||
WHERE o.garden_id = ? AND pl.removed_at IS NOT NULL
|
||||
)
|
||||
ORDER BY year DESC`,
|
||||
gardenID, gardenID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: garden planting years: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
years := []int{}
|
||||
for rows.Next() {
|
||||
var y int
|
||||
if err := rows.Scan(&y); err != nil {
|
||||
return nil, fmt.Errorf("store: scan planting year: %w", err)
|
||||
}
|
||||
years = append(years, y)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("store: iterate planting years: %w", err)
|
||||
}
|
||||
return years, nil
|
||||
}
|
||||
|
||||
// ListActivePlantingsForObject returns an object's currently-planted plops
|
||||
// (removed_at IS NULL). Always a non-nil slice. Used by FillRegion to avoid
|
||||
// stacking new plops inside existing ones.
|
||||
|
||||
@@ -24,16 +24,22 @@ func scanPlant(s scanner) (*domain.Plant, error) {
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
// ListReferencedPlants returns the distinct plants used by a garden's active
|
||||
// plantings — the catalog subset the editor needs to render them. Always a
|
||||
// non-nil slice (empty when the garden has no active plantings). This is the
|
||||
// /full read side; full plant CRUD/seeding lives in plants.go / plantings.go.
|
||||
func (d *DB) ListReferencedPlants(ctx context.Context, gardenID int64) ([]domain.Plant, error) {
|
||||
// ListReferencedPlants returns the distinct plants used by a garden's plantings
|
||||
// — the catalog subset the editor needs to render them. Always a non-nil slice.
|
||||
//
|
||||
// includeRemoved widens it past the active plops to every plop the garden has
|
||||
// ever held, which is what a past season needs: a plant pulled last July is not
|
||||
// active, but its plops still have to render with the right icon and color.
|
||||
func (d *DB) ListReferencedPlants(ctx context.Context, gardenID int64, includeRemoved bool) ([]domain.Plant, error) {
|
||||
activeOnly := ` AND pl.removed_at IS NULL`
|
||||
if includeRemoved {
|
||||
activeOnly = ``
|
||||
}
|
||||
rows, err := d.sql.QueryContext(ctx,
|
||||
`SELECT DISTINCT `+qualifyColumns("p", plantColumns)+` FROM plants p
|
||||
JOIN plantings pl ON pl.plant_id = p.id
|
||||
JOIN garden_objects o ON o.id = pl.object_id
|
||||
WHERE o.garden_id = ? AND pl.removed_at IS NULL
|
||||
WHERE o.garden_id = ?`+activeOnly+`
|
||||
ORDER BY p.id`,
|
||||
gardenID,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user