Configurable grid + snap-to-grid in the layout system (#45)
Build image / build-and-push (push) Successful in 7s
Build image / build-and-push (push) Successful in 7s
Closes #44. Two independent grids (garden + per-bed), each with a size and a snap toggle; snapping defaults off. See PR #45 for details. Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #45.
This commit is contained in:
+11
-6
@@ -14,15 +14,20 @@ import (
|
||||
// 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"`
|
||||
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}
|
||||
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
|
||||
|
||||
@@ -27,6 +27,8 @@ type objectCreateRequest struct {
|
||||
Plantable *bool `json:"plantable"`
|
||||
Color *string `json:"color"`
|
||||
Props json.RawMessage `json:"props"`
|
||||
GridSizeCM float64 `json:"gridSizeCm"`
|
||||
SnapToGrid bool `json:"snapToGrid"`
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
@@ -35,7 +37,8 @@ func (r objectCreateRequest) toInput() service.ObjectInput {
|
||||
Kind: r.Kind, Name: r.Name, Shape: r.Shape,
|
||||
XCM: r.XCM, YCM: r.YCM, WidthCM: r.WidthCM, HeightCM: r.HeightCM,
|
||||
RotationDeg: r.RotationDeg, ZIndex: r.ZIndex, Plantable: r.Plantable,
|
||||
Color: r.Color, Props: propsFromRaw(r.Props), Notes: r.Notes,
|
||||
Color: r.Color, Props: propsFromRaw(r.Props),
|
||||
GridSizeCM: r.GridSizeCM, SnapToGrid: r.SnapToGrid, Notes: r.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +57,8 @@ type objectUpdateRequest struct {
|
||||
Plantable *bool `json:"plantable"`
|
||||
Color json.RawMessage `json:"color"`
|
||||
Props json.RawMessage `json:"props"`
|
||||
GridSizeCM *float64 `json:"gridSizeCm"`
|
||||
SnapToGrid *bool `json:"snapToGrid"`
|
||||
Notes *string `json:"notes"`
|
||||
Version int64 `json:"version" binding:"required,min=1"`
|
||||
}
|
||||
@@ -67,7 +72,8 @@ func (r objectUpdateRequest) toPatch() (service.ObjectPatch, error) {
|
||||
return service.ObjectPatch{
|
||||
Name: r.Name, XCM: r.XCM, YCM: r.YCM, WidthCM: r.WidthCM, HeightCM: r.HeightCM,
|
||||
RotationDeg: r.RotationDeg, ZIndex: r.ZIndex, Plantable: r.Plantable,
|
||||
SetColor: setColor, Color: color, SetProps: setProps, Props: props, Notes: r.Notes,
|
||||
SetColor: setColor, Color: color, SetProps: setProps, Props: props,
|
||||
GridSizeCM: r.GridSizeCM, SnapToGrid: r.SnapToGrid, Notes: r.Notes,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,56 @@ func objectsPath(id int64) string {
|
||||
return "/api/v1/gardens/" + strconv.FormatInt(id, 10) + "/objects"
|
||||
}
|
||||
|
||||
// TestGridFieldsRoundTripAPI guards the JSON wiring (tags + toInput/toPatch) for
|
||||
// the garden and object grid/snap fields end-to-end through the HTTP handlers.
|
||||
func TestGridFieldsRoundTripAPI(t *testing.T) {
|
||||
r := authEngine(t, localCfg())
|
||||
cookie := registerAndCookie(t, r, "[email protected]")
|
||||
|
||||
// A garden created with no grid echoes the 1 m default, snapping off.
|
||||
w := doJSON(t, r, http.MethodPost, "/api/v1/gardens", map[string]any{"name": "G"}, cookie)
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("create garden: status %d, body %s", w.Code, w.Body.String())
|
||||
}
|
||||
g := decodeMap(t, w.Body.Bytes())
|
||||
gid := int64(g["id"].(float64))
|
||||
if g["gridSizeCm"].(float64) != 100 || g["snapToGrid"].(bool) {
|
||||
t.Errorf("garden grid defaults = %v / %v, want 100 / false", g["gridSizeCm"], g["snapToGrid"])
|
||||
}
|
||||
|
||||
// Patching the garden's grid + snap echoes the new values.
|
||||
w = doJSON(t, r, http.MethodPatch, gardenPath(gid), map[string]any{
|
||||
"name": "G", "widthCm": 300, "heightCm": 200, "unitPref": "metric",
|
||||
"gridSizeCm": 25, "snapToGrid": true, "version": 1,
|
||||
}, cookie)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("patch garden: status %d, body %s", w.Code, w.Body.String())
|
||||
}
|
||||
if g := decodeMap(t, w.Body.Bytes()); g["gridSizeCm"].(float64) != 25 || !g["snapToGrid"].(bool) {
|
||||
t.Errorf("patched garden grid = %v / %v, want 25 / true", g["gridSizeCm"], g["snapToGrid"])
|
||||
}
|
||||
|
||||
// A bed created with no grid echoes the 30 cm default; a patch sets grid + snap.
|
||||
w = doJSON(t, r, http.MethodPost, objectsPath(gid),
|
||||
map[string]any{"kind": "bed", "xCm": 150, "yCm": 100, "widthCm": 120, "heightCm": 80}, cookie)
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("create object: status %d, body %s", w.Code, w.Body.String())
|
||||
}
|
||||
o := decodeMap(t, w.Body.Bytes())
|
||||
oid := int64(o["id"].(float64))
|
||||
if o["gridSizeCm"].(float64) != 30 || o["snapToGrid"].(bool) {
|
||||
t.Errorf("object grid defaults = %v / %v, want 30 / false", o["gridSizeCm"], o["snapToGrid"])
|
||||
}
|
||||
w = doJSON(t, r, http.MethodPatch, objectPath(oid),
|
||||
map[string]any{"gridSizeCm": 15, "snapToGrid": true, "version": 1}, cookie)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("patch object: status %d, body %s", w.Code, w.Body.String())
|
||||
}
|
||||
if o := decodeMap(t, w.Body.Bytes()); o["gridSizeCm"].(float64) != 15 || !o["snapToGrid"].(bool) {
|
||||
t.Errorf("patched object grid = %v / %v, want 15 / true", o["gridSizeCm"], o["snapToGrid"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestObjectCRUDAndFull(t *testing.T) {
|
||||
r := authEngine(t, localCfg())
|
||||
cookie := registerAndCookie(t, r, "[email protected]")
|
||||
|
||||
Reference in New Issue
Block a user