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:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
|
||||
@@ -197,28 +198,80 @@ func (s *Service) DeleteObject(ctx context.Context, actorID, objectID int64) err
|
||||
}
|
||||
|
||||
// GardenFull returns the whole editor payload for a garden the actor can view.
|
||||
func (s *Service) GardenFull(ctx context.Context, actorID, gardenID int64) (*FullGarden, error) {
|
||||
// year nil means "what's planted now"; a year means the season view (#54).
|
||||
func (s *Service) GardenFull(ctx context.Context, actorID, gardenID int64, year *int) (*FullGarden, error) {
|
||||
g, err := s.requireGardenRole(ctx, actorID, gardenID, roleViewer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.assembleFull(ctx, g)
|
||||
if year != nil && (*year < minSeasonYear || *year > maxSeasonYear) {
|
||||
return nil, domain.ErrInvalidInput
|
||||
}
|
||||
return s.assembleFullFor(ctx, g, year)
|
||||
}
|
||||
|
||||
// Seasons are bounded to keep a typo'd year from producing a confidently empty
|
||||
// garden; the range is wide enough to cover any real record.
|
||||
const (
|
||||
minSeasonYear = 1900
|
||||
maxSeasonYear = 2200
|
||||
)
|
||||
|
||||
// GardenYears lists the calendar years a garden has planting data for, newest
|
||||
// first, always including the current one.
|
||||
//
|
||||
// This exists so the year selector can offer only years that actually hold
|
||||
// something. A free numeric field invites a typo and an empty view that looks
|
||||
// like data loss rather than a mistake.
|
||||
func (s *Service) GardenYears(ctx context.Context, actorID, gardenID int64) ([]int, error) {
|
||||
if _, err := s.requireGardenRole(ctx, actorID, gardenID, roleViewer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
years, err := s.store.GardenPlantingYears(ctx, gardenID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
current := s.now().UTC().Year()
|
||||
for _, y := range years {
|
||||
if y == current {
|
||||
return years, nil
|
||||
}
|
||||
}
|
||||
// Newest first, and this year is newest unless the data runs into the future.
|
||||
out := append([]int{current}, years...)
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(out)))
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// assembleFull builds the read-only /full payload for an already-authorized
|
||||
// garden: its objects, active plops (with DerivedCount filled in), and the
|
||||
// plants those plops reference. Shared by the authenticated GardenFull and the
|
||||
// unauthenticated PublicGarden read, so both return the identical shape.
|
||||
// garden as it stands NOW. Shared by the unauthenticated PublicGarden read,
|
||||
// which never offers a season view.
|
||||
func (s *Service) assembleFull(ctx context.Context, g *domain.Garden) (*FullGarden, error) {
|
||||
return s.assembleFullFor(ctx, g, nil)
|
||||
}
|
||||
|
||||
// assembleFullFor builds the /full payload: objects, plops (with DerivedCount
|
||||
// filled in), and the plants those plops reference.
|
||||
//
|
||||
// year nil is today's behaviour — active plops only. A year widens it to every
|
||||
// plop whose time in the ground overlapped that year, which necessarily includes
|
||||
// plops since removed, so the referenced-plant lookup has to widen with it or
|
||||
// those plops would render with no icon and no colour.
|
||||
func (s *Service) assembleFullFor(ctx context.Context, g *domain.Garden, year *int) (*FullGarden, error) {
|
||||
objects, err := s.store.ListObjectsForGarden(ctx, g.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
plantings, err := s.store.ListActivePlantingsForGarden(ctx, g.ID)
|
||||
var plantings []domain.Planting
|
||||
if year != nil {
|
||||
plantings, err = s.store.ListPlantingsForGardenYear(ctx, g.ID, *year)
|
||||
} else {
|
||||
plantings, err = s.store.ListActivePlantingsForGarden(ctx, g.ID)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
plants, err := s.store.ListReferencedPlants(ctx, g.ID)
|
||||
plants, err := s.store.ListReferencedPlants(ctx, g.ID, year != nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user