From 669d94acd9d54f7dc66c41d59db00320745d1012 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 12 Dec 2023 18:14:47 +0800 Subject: [PATCH 1/2] [fix] page create api --- sui/api/api.go | 4 ++-- sui/api/process.go | 10 ++++++---- sui/api/process_test.go | 7 ++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/sui/api/api.go b/sui/api/api.go index 73d2bfce1..7bc649739 100644 --- a/sui/api/api.go +++ b/sui/api/api.go @@ -118,10 +118,10 @@ var dsl = []byte(` "in": ["$param.id", "$param.template_id", "$param.route", ":context"], "out": { "status": 200, "type": "application/json" } },{ - "path": "/:id/page/create/:template_id", + "path": "/:id/page/create/:template_id/*route", "method": "POST", "process": "sui.Page.Create", - "in": ["$param.id", "$param.template_id", ":payload", ":context"], + "in": ["$param.id", "$param.template_id", "$param.route", ":context", ":payload"], "out": { "status": 200, "type": "application/json" } },{ "path": "/:id/page/exist/:template_id/*route", diff --git a/sui/api/process.go b/sui/api/process.go index 6aadf3e72..cb38898ff 100644 --- a/sui/api/process.go +++ b/sui/api/process.go @@ -530,17 +530,19 @@ func PageCreate(process *process.Process) interface{} { process.ValidateArgNums(3) sui := get(process) templateID := process.ArgsString(1) - payload := process.ArgsMap(2, map[string]interface{}{}) + route := process.ArgsString(2) + payload := process.ArgsMap(4, map[string]interface{}{}) tmpl, err := sui.GetTemplate(templateID) if err != nil { exception.New(err.Error(), 500).Throw() } - route, ok := payload["route"].(string) - if !ok { - exception.New("the route is required", 400).Throw() + // Get the route from payload + if v, ok := payload["route"].(string); ok { + route = v } + title := route if v, ok := payload["title"].(string); ok { title = v diff --git a/sui/api/process_test.go b/sui/api/process_test.go index 8602520cf..44dedb981 100644 --- a/sui/api/process_test.go +++ b/sui/api/process_test.go @@ -314,7 +314,12 @@ func TestPageCreate(t *testing.T) { load(t) defer clean() - + defer func() { + _, err := process.New("sui.page.remove", "demo", "tech-blue", "/unit-test").Exec() + if err != nil { + t.Fatal(err) + } + }() // test demo p, err := process.Of("sui.page.create", "demo", "tech-blue", "/unit-test") if err != nil { From 087b507427dee7a5b6a7eb782aa89b10d22d2b7a Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 12 Dec 2023 18:24:14 +0800 Subject: [PATCH 2/2] [change] Template.RemovePage to recursively remove empty paths --- sui/storages/local/page.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/sui/storages/local/page.go b/sui/storages/local/page.go index 3de35b807..241a659f5 100644 --- a/sui/storages/local/page.go +++ b/sui/storages/local/page.go @@ -199,17 +199,27 @@ func (tmpl *Template) RemovePage(route string) error { return err } + return tmpl.removeEmptyPath(path) +} + +func (tmpl *Template) removeEmptyPath(path string) error { dirs, err := tmpl.local.fs.ReadDir(path, false) if err != nil { return err } if len(dirs) == 0 { - return tmpl.local.fs.Remove(path) + err = tmpl.local.fs.Remove(path) + if err != nil { + return err + } + parent := filepath.Dir(path) + if parent == tmpl.Root { + return nil + } + return tmpl.removeEmptyPath(parent) } - return nil - } // CreateEmptyPage create a new empty