Copy a garden (deep duplicate) from the gardens list (#46)
Build image / build-and-push (push) Successful in 16s
Build image / build-and-push (push) Successful in 16s
Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #46.
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -103,6 +104,34 @@ func (h *handlers) updateGarden(c *gin.Context) {
|
||||
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". An absent body surfaces as io.EOF from the decoder, which is the
|
||||
// reliable signal: Content-Length is -1 (not 0) for a chunked request, so
|
||||
// testing the length would misread an empty chunked body as malformed.
|
||||
var req gardenCopyRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user