Merge pull request #521 from trheyi/main
[change] Template.RemovePage to recursively remove empty paths
This commit is contained in:
commit
693e8470f3
4 changed files with 27 additions and 10 deletions
|
|
@ -118,10 +118,10 @@ var dsl = []byte(`
|
||||||
"in": ["$param.id", "$param.template_id", "$param.route", ":context"],
|
"in": ["$param.id", "$param.template_id", "$param.route", ":context"],
|
||||||
"out": { "status": 200, "type": "application/json" }
|
"out": { "status": 200, "type": "application/json" }
|
||||||
},{
|
},{
|
||||||
"path": "/:id/page/create/:template_id",
|
"path": "/:id/page/create/:template_id/*route",
|
||||||
"method": "POST",
|
"method": "POST",
|
||||||
"process": "sui.Page.Create",
|
"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" }
|
"out": { "status": 200, "type": "application/json" }
|
||||||
},{
|
},{
|
||||||
"path": "/:id/page/exist/:template_id/*route",
|
"path": "/:id/page/exist/:template_id/*route",
|
||||||
|
|
|
||||||
|
|
@ -530,17 +530,19 @@ func PageCreate(process *process.Process) interface{} {
|
||||||
process.ValidateArgNums(3)
|
process.ValidateArgNums(3)
|
||||||
sui := get(process)
|
sui := get(process)
|
||||||
templateID := process.ArgsString(1)
|
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)
|
tmpl, err := sui.GetTemplate(templateID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exception.New(err.Error(), 500).Throw()
|
exception.New(err.Error(), 500).Throw()
|
||||||
}
|
}
|
||||||
|
|
||||||
route, ok := payload["route"].(string)
|
// Get the route from payload
|
||||||
if !ok {
|
if v, ok := payload["route"].(string); ok {
|
||||||
exception.New("the route is required", 400).Throw()
|
route = v
|
||||||
}
|
}
|
||||||
|
|
||||||
title := route
|
title := route
|
||||||
if v, ok := payload["title"].(string); ok {
|
if v, ok := payload["title"].(string); ok {
|
||||||
title = v
|
title = v
|
||||||
|
|
|
||||||
|
|
@ -314,7 +314,12 @@ func TestPageCreate(t *testing.T) {
|
||||||
|
|
||||||
load(t)
|
load(t)
|
||||||
defer clean()
|
defer clean()
|
||||||
|
defer func() {
|
||||||
|
_, err := process.New("sui.page.remove", "demo", "tech-blue", "/unit-test").Exec()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
// test demo
|
// test demo
|
||||||
p, err := process.Of("sui.page.create", "demo", "tech-blue", "/unit-test")
|
p, err := process.Of("sui.page.create", "demo", "tech-blue", "/unit-test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -199,17 +199,27 @@ func (tmpl *Template) RemovePage(route string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return tmpl.removeEmptyPath(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tmpl *Template) removeEmptyPath(path string) error {
|
||||||
dirs, err := tmpl.local.fs.ReadDir(path, false)
|
dirs, err := tmpl.local.fs.ReadDir(path, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(dirs) == 0 {
|
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
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateEmptyPage create a new empty
|
// CreateEmptyPage create a new empty
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue