Files
pansy/internal/api/objects.go
T
steveandClaude Opus 4.8 3dd935fb19
Build image / build-and-push (push) Successful in 6s
Gadfly review (reusable) / review (pull_request) Successful in 10m9s
Adversarial Review (Gadfly) / review (pull_request) Successful in 10m9s
Add garden objects backend: polymorphic CRUD + /full (#10)
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
2026-07-18 19:30:30 -04:00

151 lines
4.3 KiB
Go

package api
import (
"encoding/json"
"errors"
"net/http"
"github.com/gin-gonic/gin"
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
"gitea.stevedudenhoeffer.com/steve/pansy/internal/service"
)
// objectCreateRequest is the body for POST /gardens/:id/objects. Dimensions are
// centimeters; the object is positioned by its center. Plantable/color/props are
// optional (plantable defaults by kind). props is any JSON value, stored as-is.
type objectCreateRequest struct {
Kind string `json:"kind" binding:"required"`
Name string `json:"name"`
Shape string `json:"shape"`
XCM float64 `json:"xCm"`
YCM float64 `json:"yCm"`
WidthCM float64 `json:"widthCm"`
HeightCM float64 `json:"heightCm"`
RotationDeg float64 `json:"rotationDeg"`
ZIndex int `json:"zIndex"`
Plantable *bool `json:"plantable"`
Color *string `json:"color"`
Props json.RawMessage `json:"props"`
Notes string `json:"notes"`
}
// objectUpdateRequest is the body for PATCH /objects/:id: every field optional
// (absent = unchanged), plus the required current version. Kind and shape are
// immutable and not accepted.
type objectUpdateRequest struct {
Name *string `json:"name"`
XCM *float64 `json:"xCm"`
YCM *float64 `json:"yCm"`
WidthCM *float64 `json:"widthCm"`
HeightCM *float64 `json:"heightCm"`
RotationDeg *float64 `json:"rotationDeg"`
ZIndex *int `json:"zIndex"`
Plantable *bool `json:"plantable"`
Color *string `json:"color"`
Props json.RawMessage `json:"props"`
Notes *string `json:"notes"`
Version int64 `json:"version" binding:"required,min=1"`
}
// propsFromRaw maps a JSON props value to the stored *string: a present, non-null
// value becomes its JSON text; absent or null becomes nil (leave/none).
func propsFromRaw(raw json.RawMessage) *string {
if len(raw) == 0 || string(raw) == "null" {
return nil
}
s := string(raw)
return &s
}
func (h *handlers) createObject(c *gin.Context) {
gardenID, ok := parseIDParam(c, "id")
if !ok {
return
}
var req objectCreateRequest
if err := c.ShouldBindJSON(&req); err != nil {
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "kind is required")
return
}
o, err := h.svc.CreateObject(c.Request.Context(), mustActor(c).ID, gardenID, service.ObjectInput{
Kind: req.Kind,
Name: req.Name,
Shape: req.Shape,
XCM: req.XCM,
YCM: req.YCM,
WidthCM: req.WidthCM,
HeightCM: req.HeightCM,
RotationDeg: req.RotationDeg,
ZIndex: req.ZIndex,
Plantable: req.Plantable,
Color: req.Color,
Props: propsFromRaw(req.Props),
Notes: req.Notes,
})
if err != nil {
writeServiceError(c, err)
return
}
c.JSON(http.StatusCreated, o)
}
func (h *handlers) updateObject(c *gin.Context) {
id, ok := parseIDParam(c, "id")
if !ok {
return
}
var req objectUpdateRequest
if err := c.ShouldBindJSON(&req); err != nil {
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "a current version is required")
return
}
o, err := h.svc.UpdateObject(c.Request.Context(), mustActor(c).ID, id, service.ObjectPatch{
Name: req.Name,
XCM: req.XCM,
YCM: req.YCM,
WidthCM: req.WidthCM,
HeightCM: req.HeightCM,
RotationDeg: req.RotationDeg,
ZIndex: req.ZIndex,
Plantable: req.Plantable,
Color: req.Color,
Props: propsFromRaw(req.Props),
Notes: req.Notes,
}, req.Version)
if err != nil {
if errors.Is(err, domain.ErrVersionConflict) {
writeVersionConflict(c, o)
return
}
writeServiceError(c, err)
return
}
c.JSON(http.StatusOK, o)
}
func (h *handlers) deleteObject(c *gin.Context) {
id, ok := parseIDParam(c, "id")
if !ok {
return
}
if err := h.svc.DeleteObject(c.Request.Context(), mustActor(c).ID, id); err != nil {
writeServiceError(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (h *handlers) gardenFull(c *gin.Context) {
gardenID, ok := parseIDParam(c, "id")
if !ok {
return
}
full, err := h.svc.GardenFull(c.Request.Context(), mustActor(c).ID, gardenID)
if err != nil {
writeServiceError(c, err)
return
}
c.JSON(http.StatusOK, full)
}