Skip to content

Commit

Permalink
Merge pull request #521 from trheyi/main
Browse files Browse the repository at this point in the history
[change] Template.RemovePage to recursively remove empty paths
  • Loading branch information
trheyi authored Dec 12, 2023
2 parents 20db534 + 087b507 commit 693e847
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions sui/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 6 additions & 4 deletions sui/api/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion sui/api/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions sui/storages/local/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 693e847

Please sign in to comment.