feat(18.1-01): task 3 flat reorder on the server

- config_reorder.yaml without parentColumn on a record with no parent_id
  column compiles as a flat (WinterCMS Sortable) reorder; maxDepth above 0
  on it fails boot, an explicit parentColumn still means a tree
- ListReorder.Flat and AdminReorderTree.Flat (flat, omitempty); flat nodes
  carry parent_id null, a save writes only the sort column, a parent is a
  422 on nodes (reorder.flat_parent); 409 and duplicate rules unchanged
- swag descriptions, OpenAPI and schema.d.ts regenerated
- acme.catalog badges reorder fixture, TestReorderFlatCompile and
  TestReorderFlatSmoke; docs Flat reorder section and cabana README
This commit is contained in:
Jakub Zych
2026-10-14 15:56:57 +02:00
parent cf63fb3152
commit 9edff8890f
13 changed files with 300 additions and 28 deletions

View File

@@ -285,3 +285,90 @@ func TestTaglistFieldSmoke(t *testing.T) {
// customTags false: only the options are accepted.
expectCatalog422(t, e, map[string]any{"name": "Bad", "tags": []any{"alpha", "delta"}}, "tags", "Tag delta jest niedozwolony.")
}
// catalogReorder is the data of the badges reorder routes.
type catalogReorder struct {
Data struct {
Flat bool `json:"flat"`
Nodes []struct {
ID uint `json:"id"`
ParentID *uint `json:"parent_id"`
SortOrder int64 `json:"sort_order"`
Name string `json:"name"`
} `json:"nodes"`
} `json:"data"`
}
// TestReorderFlatSmoke checks the flat reorder routes on Postgres: the GET
// answers flat true with parent_id null on every node, the POST stores the
// submitted sort values (only the sort column) and answers the new order, a
// node with a parent is a 422 on nodes and a missing node a 409.
func TestReorderFlatSmoke(t *testing.T) {
e := newCatalogEnv(t)
ids := make([]uint, 0, 3)
for i, name := range []string{"One", "Two", "Three"} {
badge := catalogBadge{Name: name, SortOrder: i + 1}
if err := e.db.Create(&badge).Error; err != nil {
t.Fatal(err)
}
ids = append(ids, badge.ID)
}
rec := e.send(t, http.MethodGet, "/acme/catalog/badges/reorder", nil, true)
if rec.Code != http.StatusOK {
t.Fatalf("reorder GET status=%d body=%s", rec.Code, rec.Body.String())
}
var got catalogReorder
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if !got.Data.Flat || len(got.Data.Nodes) != 3 || got.Data.Nodes[0].Name != "One" {
t.Fatalf("reorder GET = %s", rec.Body.String())
}
if !strings.Contains(rec.Body.String(), `"flat":true`) {
t.Fatalf("reorder GET lacks flat: %s", rec.Body.String())
}
for _, node := range got.Data.Nodes {
if node.ParentID != nil {
t.Fatalf("flat node %d has parent %d", node.ID, *node.ParentID)
}
}
reversed := []map[string]any{
{"id": ids[2], "parent_id": nil, "sort_order": 1},
{"id": ids[1], "parent_id": nil, "sort_order": 2},
{"id": ids[0], "parent_id": nil, "sort_order": 3},
}
rec = e.send(t, http.MethodPost, "/acme/catalog/badges/reorder", map[string]any{"nodes": reversed}, true)
if rec.Code != http.StatusOK {
t.Fatalf("reorder POST status=%d body=%s", rec.Code, rec.Body.String())
}
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if len(got.Data.Nodes) != 3 || got.Data.Nodes[0].ID != ids[2] || got.Data.Nodes[2].ID != ids[0] || !got.Data.Flat {
t.Fatalf("reorder POST answer = %s", rec.Body.String())
}
for i, id := range []uint{ids[2], ids[1], ids[0]} {
var badge catalogBadge
if err := e.db.First(&badge, id).Error; err != nil {
t.Fatal(err)
}
if badge.SortOrder != i+1 {
t.Fatalf("badge %d sort_order = %d, want %d", id, badge.SortOrder, i+1)
}
}
nested := []map[string]any{
{"id": ids[0], "parent_id": nil, "sort_order": 1},
{"id": ids[1], "parent_id": ids[0], "sort_order": 2},
{"id": ids[2], "parent_id": nil, "sort_order": 3},
}
rec = e.send(t, http.MethodPost, "/acme/catalog/badges/reorder", map[string]any{"nodes": nested}, true)
if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), `"nodes"`) || !strings.Contains(rec.Body.String(), "Kolejność płaska nie ma elementów nadrzędnych.") {
t.Fatalf("flat parent status=%d body=%s", rec.Code, rec.Body.String())
}
rec = e.send(t, http.MethodPost, "/acme/catalog/badges/reorder", map[string]any{"nodes": reversed[:2]}, true)
if rec.Code != http.StatusConflict {
t.Fatalf("missing node status=%d body=%s", rec.Code, rec.Body.String())
}
}