Garden objects (beds, bags, containers, in-ground, trees, paths, structures) in one polymorphic table, following the #7 service conventions. - store/objects.go: scanObject + Create (RETURNING), Get, ListForGarden (z_index order), version-guarded Update (RETURNING, returns current row on conflict), Delete (plantings cascade via FK). - store/plantings.go + plants.go: the read side /full needs now — ListActivePlantingsForGarden (removed_at IS NULL) and ListReferencedPlants (distinct plants used by active plantings). Both return empty until #14 adds plantings; #12/#14 extend these files. - service/objects.go: Create/Update(partial patch)/Delete/GardenFull, all (ctx, actorID, ...) through requireGardenRole(editor for mutations, viewer for /full). finalizeObject validates kind + shape (rect/circle; polygon reserved), finite dims in [1cm,100m], NaN/Inf rejection, loose bbox-overlaps-garden placement (partial overhang OK), hex color, valid JSON props, name/notes caps; normalizes rotation to [0,360). Plantable defaults by kind, overridable. - api/objects.go: POST /gardens/:id/objects, PATCH/DELETE /objects/:id, GET /gardens/:id/full ({garden,objects,plantings,plants}). props is any JSON value stored as text; PATCH is partial + required version, 409 returns the current row. Tests: service (defaults, plantable-by-kind, rotation normalize, validation rejects incl. off-field/polygon/bad-color/bad-props, partial patch + version conflict, cross-user ErrNotFound, delete, /full shape) and api (CRUD + /full, 409 envelope, cross-user 404, polygon/no-kind 400, props round-trip). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JdQpdYYsTgtkJBxbcpAszi
57 lines
1.8 KiB
Go
57 lines
1.8 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
|
|
)
|
|
|
|
// scanPlanting reads a plantings row in table-declared column order (id,
|
|
// object_id, plant_id, x_cm, y_cm, radius_cm, count, label, planted_at,
|
|
// removed_at, version, created_at, updated_at) — the order SELECT pl.* yields.
|
|
func scanPlanting(s scanner) (*domain.Planting, error) {
|
|
var p domain.Planting
|
|
if err := s.Scan(
|
|
&p.ID, &p.ObjectID, &p.PlantID, &p.XCM, &p.YCM, &p.RadiusCM,
|
|
&p.Count, &p.Label, &p.PlantedAt, &p.RemovedAt,
|
|
&p.Version, &p.CreatedAt, &p.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
return &p, nil
|
|
}
|
|
|
|
// ListActivePlantingsForGarden returns every currently-planted plop (removed_at
|
|
// IS NULL) across all objects in a garden — the editor's one-shot load. Always a
|
|
// non-nil slice (empty until plantings CRUD lands in #14). Plop CRUD itself is
|
|
// #14; this is only the /full read side.
|
|
func (d *DB) ListActivePlantingsForGarden(ctx context.Context, gardenID int64) ([]domain.Planting, error) {
|
|
// pl.* returns the plantings columns in table-declared order, which matches
|
|
// plantingColumns / scanPlanting.
|
|
rows, err := d.sql.QueryContext(ctx,
|
|
`SELECT pl.* FROM plantings pl
|
|
JOIN garden_objects o ON o.id = pl.object_id
|
|
WHERE o.garden_id = ? AND pl.removed_at IS NULL
|
|
ORDER BY pl.id`,
|
|
gardenID,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: list plantings: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
plantings := []domain.Planting{}
|
|
for rows.Next() {
|
|
p, err := scanPlanting(rows)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: scan planting: %w", err)
|
|
}
|
|
plantings = append(plantings, *p)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("store: iterate plantings: %w", err)
|
|
}
|
|
return plantings, nil
|
|
}
|