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) }