Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] builder page create and remove apis #520

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/*route",
"path": "/:id/page/create/:template_id",
"method": "POST",
"process": "sui.Page.Create",
"in": ["$param.id", "$param.template_id", "$param.route", ":context"],
"in": ["$param.id", "$param.template_id", ":payload", ":context"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/page/exist/:template_id/*route",
Expand Down
23 changes: 18 additions & 5 deletions sui/api/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ func PageSave(process *process.Process) interface{} {
exception.New(err.Error(), 500).Throw()
}
} else {
page, err = tmpl.CreatePage(route)
page, err = tmpl.CreateEmptyPage(route, nil)
if err != nil {
exception.New(err.Error(), 500).Throw()
}
Expand Down Expand Up @@ -499,7 +499,7 @@ func PageSaveTemp(process *process.Process) interface{} {
exception.New(err.Error(), 500).Throw()
}
} else {
page, err = tmpl.CreatePage(route)
page, err = tmpl.CreateEmptyPage(route, nil)
if err != nil {
exception.New(err.Error(), 500).Throw()
}
Expand Down Expand Up @@ -530,14 +530,23 @@ func PageCreate(process *process.Process) interface{} {
process.ValidateArgNums(3)
sui := get(process)
templateID := process.ArgsString(1)
route := route(process, 2)
payload := process.ArgsMap(2, map[string]interface{}{})

tmpl, err := sui.GetTemplate(templateID)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

page, err := tmpl.CreatePage(route)
route, ok := payload["route"].(string)
if !ok {
exception.New("the route is required", 400).Throw()
}
title := route
if v, ok := payload["title"].(string); ok {
title = v
}
setting := &core.PageSetting{Title: title}
page, err := tmpl.CreateEmptyPage(route, setting)
if err != nil {
exception.New(err.Error(), 500).Throw()
}
Expand All @@ -548,7 +557,7 @@ func PageCreate(process *process.Process) interface{} {

source, err := getSource(process)
if err != nil {
exception.New(err.Error(), 500).Throw()
return nil
}

if source == nil {
Expand All @@ -574,6 +583,10 @@ func PageRemove(process *process.Process) interface{} {
exception.New(err.Error(), 500).Throw()
}

if !tmpl.PageExist(route) {
exception.New("page does not exists!", 400).Throw()
}

err = tmpl.RemovePage(route)
if err != nil {
exception.New(err.Error(), 500).Throw()
Expand Down
2 changes: 1 addition & 1 deletion sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ITemplate interface {
PageTree(route string) ([]*PageTreeNode, error)
Page(route string) (IPage, error)
PageExist(route string) bool
CreatePage(route string) (IPage, error)
CreateEmptyPage(route string, setting *PageSetting) (IPage, error)
RemovePage(route string) error
GetPageFromAsset(asset string) (IPage, error)

Expand Down
41 changes: 22 additions & 19 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,28 @@ type Request struct {

// RequestSource is the struct for the request
type RequestSource struct {
UID string `json:"uid"`
User string `json:"user,omitempty"`
Page *SourceData `json:"page,omitempty"`
Style *SourceData `json:"style,omitempty"`
Script *SourceData `json:"script,omitempty"`
Data *SourceData `json:"data,omitempty"`
Board *BoardSourceData `json:"board,omitempty"`
Mock *PageMock `json:"mock,omitempty"`
Setting *PageSetting `json:"setting,omitempty"`
NeedToSave struct {
Page bool `json:"page,omitempty"`
Style bool `json:"style,omitempty"`
Script bool `json:"script,omitempty"`
Data bool `json:"data,omitempty"`
Board bool `json:"board,omitempty"`
Mock bool `json:"mock,omitempty"`
Setting bool `json:"setting,omitempty"`
Validate bool `json:"validate,omitempty"`
} `json:"needToSave,omitempty"`
UID string `json:"uid"`
User string `json:"user,omitempty"`
Page *SourceData `json:"page,omitempty"`
Style *SourceData `json:"style,omitempty"`
Script *SourceData `json:"script,omitempty"`
Data *SourceData `json:"data,omitempty"`
Board *BoardSourceData `json:"board,omitempty"`
Mock *PageMock `json:"mock,omitempty"`
Setting *PageSetting `json:"setting,omitempty"`
NeedToSave ReqeustSourceNeedToSave `json:"needToSave,omitempty"`
}

// ReqeustSourceNeedToSave is the struct for the request
type ReqeustSourceNeedToSave struct {
Page bool `json:"page,omitempty"`
Style bool `json:"style,omitempty"`
Script bool `json:"script,omitempty"`
Data bool `json:"data,omitempty"`
Board bool `json:"board,omitempty"`
Mock bool `json:"mock,omitempty"`
Setting bool `json:"setting,omitempty"`
Validate bool `json:"validate,omitempty"`
}

// ResponseEditorRender is the struct for the response
Expand Down
25 changes: 21 additions & 4 deletions sui/storages/local/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ func (tmpl *Template) RemovePage(route string) error {

path := filepath.Join(tmpl.Root, route)
name := filepath.Base(path) + ".*"
name = strings.ReplaceAll(name, "[", "\\[")
name = strings.ReplaceAll(name, "]", "\\]")
err := tmpl.local.fs.Walk(path, func(root, file string, isdir bool) error {
if isdir {
return nil
Expand All @@ -210,15 +212,15 @@ func (tmpl *Template) RemovePage(route string) error {

}

// CreatePage create a new page
func (tmpl *Template) CreatePage(route string) (core.IPage, error) {
// CreateEmptyPage create a new empty
func (tmpl *Template) CreateEmptyPage(route string, setting *core.PageSetting) (core.IPage, error) {
if tmpl.PageExist(route) {
return nil, fmt.Errorf("Page %s already exist", route)
}

// Create the page directory
name := tmpl.getPageBase(route)
return &Page{
page := &Page{
tmpl: tmpl,
Page: &core.Page{
Route: route,
Expand All @@ -235,7 +237,22 @@ func (tmpl *Template) CreatePage(route string) (core.IPage, error) {
CONF: core.Source{File: fmt.Sprintf("%s.config", name)},
},
},
}, nil
}

title := route
if setting != nil {
title = setting.Title
}

err := page.Save(&core.RequestSource{
Page: &core.SourceData{Source: fmt.Sprintf("<div>%s</div>", title), Language: "html"},
Setting: setting,
NeedToSave: core.ReqeustSourceNeedToSave{Page: true, Setting: true},
})
if err != nil {
return nil, err
}
return page, nil
}

// Remove remove the page
Expand Down
2 changes: 1 addition & 1 deletion sui/storages/local/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func TestPageSave(t *testing.T) {
req := &core.RequestSource{UID: "19e09e7e-9e19-44c1-bbab-2a55c51c9df3"}
jsoniter.Unmarshal([]byte(payload), &req)

page, err := tmpl.CreatePage("/unit-test")
page, err := tmpl.CreateEmptyPage("/unit-test", nil)
if err != nil {
t.Fatalf("Page error: %v", err)
}
Expand Down