Seed provenance and inventory: source links and seed lots (#50)
"German Red Garlic" now links back to the Johnny's page it came from, and pansy knows how much is left. Two modelling calls, both of which look like omissions unless stated. The plant catalog stays FLAT — no species/variety hierarchy. A row in your catalog just is the thing you plant; the built-in "Garlic" is a generic starting point you clone. A hierarchy buys taxonomy nobody asked for and complicates every join. Inventory belongs to a PURCHASE, not to a variety. You might buy the same garlic from two vendors in two years at two prices and two germination rates; quantity columns on plants would collapse all of that into one number and lose it. So lots get their own table, and owner_id sits on the lot rather than being inherited from the plant — you can buy generic built-in Garlic from Johnny's and the record is still yours alone. Lots are never shared along with a garden. `remaining` is derived, never stored: quantity minus the summed effective count of the active plantings attributed to the lot. The alternative — decrementing a column when you plant — drifts the moment anything edits or removes a planting behind its back, and once it has drifted there's no way to tell what the true number was. Removing a plop gives the seed back with nothing having to remember to. The usage query returns raw radius/count/spacing rather than a SUM, because the effective-count formula lives in the service and reproducing it in SQL would guarantee the two drift apart. source_url is validated as http(s) with a host, on both plants and lots. It renders as a clickable link, so that check is load-bearing rather than tidiness: without it a stored javascript: URL becomes script execution the moment someone clicks it. Schemeless and relative URLs are refused too — they'd resolve against pansy's own origin, which is never what a vendor link means. A planting's lot must be the actor's AND a lot of the plant being planted. Attributing a garlic planting to a tomato lot would silently corrupt every remaining count downstream, so it's refused rather than stored, and re-checked on update in case a plant swap invalidated a previously-valid attribution. Deleting a lot leaves its plantings alone with a null link: the plants really were in the ground, whatever happened to the record of the packet. Closes #50 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01H3zbym8Doka2d7D48maSgZ
This commit is contained in:
@@ -124,6 +124,15 @@ func New(cfg *config.Config, svc *service.Service) *gin.Engine {
|
||||
plants.PATCH("/:id", h.updatePlant)
|
||||
plants.DELETE("/:id", h.deletePlant)
|
||||
|
||||
// Seed lots: what the actor bought, and what's left. Private to the buyer,
|
||||
// so these hang off the session actor rather than off a garden.
|
||||
seedLots := v1.Group("/seed-lots", h.requireAuth())
|
||||
seedLots.GET("", h.listSeedLots)
|
||||
seedLots.POST("", h.createSeedLot)
|
||||
seedLots.GET("/:id", h.getSeedLot)
|
||||
seedLots.PATCH("/:id", h.updateSeedLot)
|
||||
seedLots.DELETE("/:id", h.deleteSeedLot)
|
||||
|
||||
// Public, unauthenticated read of a garden by its share token. Deliberately
|
||||
// NOT behind requireAuth: the token is the capability, so a logged-out visitor
|
||||
// opens a shared link without being redirected to /login or OIDC. Only GET,
|
||||
|
||||
@@ -22,12 +22,13 @@ type plantingCreateRequest struct {
|
||||
Count *int `json:"count"`
|
||||
Label *string `json:"label"`
|
||||
PlantedAt *string `json:"plantedAt"`
|
||||
SeedLotID *int64 `json:"seedLotId"`
|
||||
}
|
||||
|
||||
func (r plantingCreateRequest) toInput() service.PlantingInput {
|
||||
return service.PlantingInput{
|
||||
PlantID: r.PlantID, XCM: r.XCM, YCM: r.YCM, RadiusCM: r.RadiusCM,
|
||||
Count: r.Count, Label: r.Label, PlantedAt: r.PlantedAt,
|
||||
Count: r.Count, Label: r.Label, PlantedAt: r.PlantedAt, SeedLotID: r.SeedLotID,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +46,7 @@ type plantingUpdateRequest struct {
|
||||
Label json.RawMessage `json:"label"`
|
||||
PlantedAt json.RawMessage `json:"plantedAt"`
|
||||
RemovedAt json.RawMessage `json:"removedAt"`
|
||||
SeedLotID json.RawMessage `json:"seedLotId"`
|
||||
Version int64 `json:"version" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
@@ -61,6 +63,10 @@ func (r plantingUpdateRequest) toPatch() (service.PlantingPatch, error) {
|
||||
if err != nil {
|
||||
return service.PlantingPatch{}, err
|
||||
}
|
||||
seedLot, setSeedLot, err := parseNullable[int64](r.SeedLotID)
|
||||
if err != nil {
|
||||
return service.PlantingPatch{}, err
|
||||
}
|
||||
removedAt, setRemovedAt, err := parseNullableString(r.RemovedAt)
|
||||
if err != nil {
|
||||
return service.PlantingPatch{}, err
|
||||
@@ -71,6 +77,7 @@ func (r plantingUpdateRequest) toPatch() (service.PlantingPatch, error) {
|
||||
SetLabel: setLabel, Label: label,
|
||||
SetPlantedAt: setPlantedAt, PlantedAt: plantedAt,
|
||||
SetRemovedAt: setRemovedAt, RemovedAt: removedAt,
|
||||
SetSeedLotID: setSeedLot, SeedLotID: seedLot,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -20,13 +20,16 @@ type plantCreateRequest struct {
|
||||
Color string `json:"color" binding:"required"`
|
||||
Icon string `json:"icon" binding:"required"`
|
||||
DaysToMaturity *int `json:"daysToMaturity"`
|
||||
SourceURL string `json:"sourceUrl"`
|
||||
Vendor string `json:"vendor"`
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
func (r plantCreateRequest) toInput() service.PlantInput {
|
||||
return service.PlantInput{
|
||||
Name: r.Name, Category: r.Category, SpacingCM: r.SpacingCM,
|
||||
Color: r.Color, Icon: r.Icon, DaysToMaturity: r.DaysToMaturity, Notes: r.Notes,
|
||||
Color: r.Color, Icon: r.Icon, DaysToMaturity: r.DaysToMaturity,
|
||||
SourceURL: r.SourceURL, Vendor: r.Vendor, Notes: r.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +44,8 @@ type plantUpdateRequest struct {
|
||||
Color *string `json:"color"`
|
||||
Icon *string `json:"icon"`
|
||||
DaysToMaturity json.RawMessage `json:"daysToMaturity"`
|
||||
SourceURL *string `json:"sourceUrl"`
|
||||
Vendor *string `json:"vendor"`
|
||||
Notes *string `json:"notes"`
|
||||
Version int64 `json:"version" binding:"required,min=1"`
|
||||
}
|
||||
@@ -53,7 +58,8 @@ func (r plantUpdateRequest) toPatch() (service.PlantPatch, error) {
|
||||
return service.PlantPatch{
|
||||
Name: r.Name, Category: r.Category, SpacingCM: r.SpacingCM,
|
||||
Color: r.Color, Icon: r.Icon,
|
||||
SetDays: setDays, DaysToMaturity: days, Notes: r.Notes,
|
||||
SetDays: setDays, DaysToMaturity: days,
|
||||
SourceURL: r.SourceURL, Vendor: r.Vendor, Notes: r.Notes,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
|
||||
"gitea.stevedudenhoeffer.com/steve/pansy/internal/service"
|
||||
)
|
||||
|
||||
// Seed lots (#50): what you actually bought, and what's left of it. Private to
|
||||
// the buyer — a lot is never shared along with a garden — so every handler here
|
||||
// scopes to the session actor with no garden in the picture.
|
||||
|
||||
// seedLotCreateRequest is the body for POST /seed-lots.
|
||||
type seedLotCreateRequest struct {
|
||||
PlantID int64 `json:"plantId" binding:"required"`
|
||||
Vendor string `json:"vendor"`
|
||||
SourceURL string `json:"sourceUrl"`
|
||||
SKU string `json:"sku"`
|
||||
LotCode string `json:"lotCode"`
|
||||
PurchasedAt *string `json:"purchasedAt"`
|
||||
PackedForYear *int `json:"packedForYear"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
Unit string `json:"unit" binding:"required"`
|
||||
CostCents *int `json:"costCents"`
|
||||
GerminationPct *float64 `json:"germinationPct"`
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
func (r seedLotCreateRequest) toInput() service.SeedLotInput {
|
||||
return service.SeedLotInput{
|
||||
PlantID: r.PlantID, Vendor: r.Vendor, SourceURL: r.SourceURL, SKU: r.SKU,
|
||||
LotCode: r.LotCode, PurchasedAt: r.PurchasedAt, PackedForYear: r.PackedForYear,
|
||||
Quantity: r.Quantity, Unit: r.Unit, CostCents: r.CostCents,
|
||||
GerminationPct: r.GerminationPct, Notes: r.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
// seedLotUpdateRequest is the body for PATCH /seed-lots/:id: every field
|
||||
// optional, plus the required current version. The nullable columns are
|
||||
// json.RawMessage so an explicit null (clear it) is distinguishable from an
|
||||
// absent field (leave it alone). plantId is not accepted — see SeedLotPatch.
|
||||
type seedLotUpdateRequest struct {
|
||||
Vendor *string `json:"vendor"`
|
||||
SourceURL *string `json:"sourceUrl"`
|
||||
SKU *string `json:"sku"`
|
||||
LotCode *string `json:"lotCode"`
|
||||
PurchasedAt json.RawMessage `json:"purchasedAt"`
|
||||
PackedForYear json.RawMessage `json:"packedForYear"`
|
||||
Quantity *float64 `json:"quantity"`
|
||||
Unit *string `json:"unit"`
|
||||
CostCents json.RawMessage `json:"costCents"`
|
||||
GerminationPct json.RawMessage `json:"germinationPct"`
|
||||
Notes *string `json:"notes"`
|
||||
Version int64 `json:"version" binding:"required"`
|
||||
}
|
||||
|
||||
func (r seedLotUpdateRequest) toPatch() (service.SeedLotPatch, error) {
|
||||
p := service.SeedLotPatch{
|
||||
Vendor: r.Vendor, SourceURL: r.SourceURL, SKU: r.SKU, LotCode: r.LotCode,
|
||||
Quantity: r.Quantity, Unit: r.Unit, Notes: r.Notes,
|
||||
}
|
||||
var err error
|
||||
if p.PurchasedAt, p.SetPurchasedAt, err = parseNullable[string](r.PurchasedAt); err != nil {
|
||||
return p, err
|
||||
}
|
||||
if p.PackedForYear, p.SetPackedForYear, err = parseNullable[int](r.PackedForYear); err != nil {
|
||||
return p, err
|
||||
}
|
||||
if p.CostCents, p.SetCostCents, err = parseNullable[int](r.CostCents); err != nil {
|
||||
return p, err
|
||||
}
|
||||
if p.GerminationPct, p.SetGerminationPct, err = parseNullable[float64](r.GerminationPct); err != nil {
|
||||
return p, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// parseNullable decodes any JSON value into (value, present) for a nullable
|
||||
// column. Absent → (nil, false); null → (nil, true); otherwise the decoded value.
|
||||
func parseNullable[T any](raw json.RawMessage) (*T, bool, error) {
|
||||
if len(raw) == 0 {
|
||||
return nil, false, nil
|
||||
}
|
||||
if string(raw) == "null" {
|
||||
return nil, true, nil
|
||||
}
|
||||
var v T
|
||||
if err := json.Unmarshal(raw, &v); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return &v, true, nil
|
||||
}
|
||||
|
||||
func (h *handlers) listSeedLots(c *gin.Context) {
|
||||
var plantID *int64
|
||||
if raw := c.Query("plantId"); raw != "" {
|
||||
id, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || id < 1 {
|
||||
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "invalid plantId")
|
||||
return
|
||||
}
|
||||
plantID = &id
|
||||
}
|
||||
lots, err := h.svc.ListSeedLots(c.Request.Context(), mustActor(c).ID, plantID)
|
||||
if err != nil {
|
||||
writeServiceError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, lots)
|
||||
}
|
||||
|
||||
func (h *handlers) createSeedLot(c *gin.Context) {
|
||||
var req seedLotCreateRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "invalid seed lot: plantId and unit are required")
|
||||
return
|
||||
}
|
||||
lot, err := h.svc.CreateSeedLot(c.Request.Context(), mustActor(c).ID, req.toInput())
|
||||
if err != nil {
|
||||
writeServiceError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, lot)
|
||||
}
|
||||
|
||||
func (h *handlers) getSeedLot(c *gin.Context) {
|
||||
id, ok := parseIDParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
lot, err := h.svc.GetSeedLot(c.Request.Context(), mustActor(c).ID, id)
|
||||
if err != nil {
|
||||
writeServiceError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, lot)
|
||||
}
|
||||
|
||||
func (h *handlers) updateSeedLot(c *gin.Context) {
|
||||
id, ok := parseIDParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req seedLotUpdateRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "invalid update: a current version is required")
|
||||
return
|
||||
}
|
||||
patch, err := req.toPatch()
|
||||
if err != nil {
|
||||
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "invalid update payload")
|
||||
return
|
||||
}
|
||||
lot, err := h.svc.UpdateSeedLot(c.Request.Context(), mustActor(c).ID, id, patch, req.Version)
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrVersionConflict) {
|
||||
writeVersionConflict(c, lot)
|
||||
return
|
||||
}
|
||||
writeServiceError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, lot)
|
||||
}
|
||||
|
||||
func (h *handlers) deleteSeedLot(c *gin.Context) {
|
||||
id, ok := parseIDParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.svc.DeleteSeedLot(c.Request.Context(), mustActor(c).ID, id); err != nil {
|
||||
writeServiceError(c, err)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
Reference in New Issue
Block a user