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"],
|
||||
"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",
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue