Adds POST /gardens/:id/copy: duplicates a garden the actor owns, along
with its objects and their currently-planted plops, into a new garden
owned by the actor. A blank name derives "<source> (copy)".
Deliberately not carried over:
* public_token — the share link is a capability granted to the
original; a copy must not silently inherit a live public URL
* garden_shares — a copy is private to whoever made it
* removed plantings — the copy is a fresh layout, not a history
Owner-only for now: a copy's plops keep pointing at the SOURCE's
plant_ids, so letting a viewer copy someone else's garden would hand
them plants outside their catalog and pin the original owner's plants
against deletion (plantings.plant_id is ON DELETE RESTRICT). Copying a
shared garden needs a plant-cloning policy first.
The whole copy runs in one transaction, so a failure part-way leaves no
half-populated garden. The object/planting list scans are factored into
queryObjects/queryPlantings so the same code serves a plain read and a
read inside that transaction.
UI: a Copy action on the owner's garden card opens a modal prefilled
with the derived name, then lands in the new garden.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
145 lines
3.9 KiB
Go
145 lines
3.9 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)
|
|
}
|
|
|
|
// gardenCopyRequest is the (optional) body of POST /gardens/:id/copy. A blank or
|
|
// absent name lets the service derive "<source> (copy)".
|
|
type gardenCopyRequest struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func (h *handlers) copyGarden(c *gin.Context) {
|
|
id, ok := parseIDParam(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
// The body is optional — POST with no body means "copy under the default
|
|
// name" — so only decode when one was actually sent.
|
|
var req gardenCopyRequest
|
|
if c.Request.ContentLength != 0 {
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "name must be a string")
|
|
return
|
|
}
|
|
}
|
|
g, err := h.svc.CopyGarden(c.Request.Context(), mustActor(c).ID, id, req.Name)
|
|
if err != nil {
|
|
writeServiceError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, 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)
|
|
}
|