Files
pansy/internal/api/gardens.go
T
steveandClaude Opus 4.8 3f4d5627d8
Build image / build-and-push (push) Successful in 15s
Gadfly review (reusable) / review (pull_request) Successful in 10m8s
Adversarial Review (Gadfly) / review (pull_request) Successful in 10m9s
Add configurable grid + snap to the layout system
Two independent grids, each with a size and a snap toggle:

- Garden grid (owner-set, in the garden settings form): drives the
  editor's visual grid and, when snapping is on, snaps objects on
  place/move and snaps their dimensions to whole grid steps on resize.
- Bed grid (per plantable object, in the bed Inspector): when snapping
  is on, plants snap to the bed's grid on place/move, and the grid is
  drawn inside the focused bed. Plant radius stays free.

Snapping defaults off on both, so existing gardens keep free placement.
Grid spacing shares the [1cm, 100m] range and defaults to 1m (garden) /
30cm (bed); 0 is treated as unset and re-defaulted.

Backend: migration 0005 adds grid_size_cm/snap_to_grid to gardens and
garden_objects, threaded through domain/store/service/api with service +
api round-trip tests. Frontend: new geometry snap helpers (unit-tested),
schema/type plumbing, canvas + overlay snapping, and the two forms.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdQpdYYsTgtkJBxbcpAszi
2026-07-19 02:51:44 -04:00

117 lines
3.1 KiB
Go

package api
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
"gitea.stevedudenhoeffer.com/steve/pansy/internal/service"
)
// gardenFields is the shared mutable field set for the gardens endpoints.
// Dimensions are centimeters (the API is metric-only; imperial is a display
// concern). On create, omitted dimensions default server-side.
type gardenFields struct {
Name string `json:"name" binding:"required"`
WidthCM float64 `json:"widthCm"`
HeightCM float64 `json:"heightCm"`
UnitPref string `json:"unitPref"`
Notes string `json:"notes"`
GridSizeCM float64 `json:"gridSizeCm"`
SnapToGrid bool `json:"snapToGrid"`
}
func (f gardenFields) toInput() service.GardenInput {
return service.GardenInput{
Name: f.Name, WidthCM: f.WidthCM, HeightCM: f.HeightCM, UnitPref: f.UnitPref, Notes: f.Notes,
GridSizeCM: f.GridSizeCM, SnapToGrid: f.SnapToGrid,
}
}
// gardenCreateRequest / gardenUpdateRequest are the JSON bodies. Update adds a
// required current version (>= 1) for the optimistic-concurrency guard.
type gardenCreateRequest struct {
gardenFields
}
type gardenUpdateRequest struct {
gardenFields
Version int64 `json:"version" binding:"required,min=1"`
}
// listGardens returns the actor's gardens as a JSON array (always an array,
// never null).
func (h *handlers) listGardens(c *gin.Context) {
gardens, err := h.svc.ListGardens(c.Request.Context(), mustActor(c).ID)
if err != nil {
writeServiceError(c, err)
return
}
c.JSON(http.StatusOK, gardens)
}
func (h *handlers) createGarden(c *gin.Context) {
var req gardenCreateRequest
if err := c.ShouldBindJSON(&req); err != nil {
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "a garden name is required")
return
}
g, err := h.svc.CreateGarden(c.Request.Context(), mustActor(c).ID, req.toInput())
if err != nil {
writeServiceError(c, err)
return
}
c.JSON(http.StatusCreated, g)
}
func (h *handlers) getGarden(c *gin.Context) {
id, ok := parseIDParam(c, "id")
if !ok {
return
}
g, err := h.svc.GetGarden(c.Request.Context(), mustActor(c).ID, id)
if err != nil {
writeServiceError(c, err)
return
}
c.JSON(http.StatusOK, g)
}
func (h *handlers) updateGarden(c *gin.Context) {
id, ok := parseIDParam(c, "id")
if !ok {
return
}
var req gardenUpdateRequest
if err := c.ShouldBindJSON(&req); err != nil {
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "name and a current version are required")
return
}
g, err := h.svc.UpdateGarden(c.Request.Context(), mustActor(c).ID, id, req.toInput(), req.Version)
if err != nil {
// On a version conflict the service returns the current row so the client
// can rebase; everything else is a plain error.
if errors.Is(err, domain.ErrVersionConflict) {
writeVersionConflict(c, g)
return
}
writeServiceError(c, err)
return
}
c.JSON(http.StatusOK, g)
}
func (h *handlers) deleteGarden(c *gin.Context) {
id, ok := parseIDParam(c, "id")
if !ok {
return
}
if err := h.svc.DeleteGarden(c.Request.Context(), mustActor(c).ID, id); err != nil {
writeServiceError(c, err)
return
}
c.Status(http.StatusNoContent)
}