Skip to content

Commit

Permalink
Merge pull request #470 from trheyi/main
Browse files Browse the repository at this point in the history
[add] page api and change api route
  • Loading branch information
trheyi authored Sep 29, 2023
2 parents 04e4f31 + 1aade99 commit eb49703
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 38 deletions.
37 changes: 25 additions & 12 deletions sui/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,60 @@ var dsl = []byte(`
},
{
"path": "/:id/template/:template_id/locale",
"path": "/:id/locale/:template_id",
"method": "GET",
"process": "sui.Template.Locale.Get",
"process": "sui.Locale.Get",
"in": ["$param.id", "$param.template_id"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/template/:template_id/theme",
"path": "/:id/theme/:template_id",
"method": "GET",
"process": "sui.Template.Theme.Get",
"process": "sui.Theme.Get",
"in": ["$param.id", "$param.template_id"],
"out": { "status": 200, "type": "application/json" }
},
{
"path": "/:id/template/:template_id/block",
"path": "/:id/block/:template_id",
"method": "GET",
"process": "sui.Template.Block.Get",
"process": "sui.Block.Get",
"in": ["$param.id", "$param.template_id"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/template/:template_id/block/:block_id",
"path": "/:id/block/:template_id/:block_id",
"method": "GET",
"process": "sui.Template.Block.Find",
"process": "sui.Block.Find",
"in": ["$param.id", "$param.template_id", "$param.block_id"],
"out": { "status": 200, "type": "text/javascript" }
},
{
"path": "/:id/template/:template_id/component",
"path": "/:id/component/:template_id",
"method": "GET",
"process": "sui.Template.Component.Get",
"process": "sui.Component.Get",
"in": ["$param.id", "$param.template_id"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/template/:template_id/component/:component_id",
"path": "/:id/component/:template_id/:component_id",
"method": "GET",
"process": "sui.Template.Component.Find",
"process": "sui.Component.Find",
"in": ["$param.id", "$param.template_id", "$param.component_id"],
"out": { "status": 200, "type": "text/javascript" }
},
{
"path": "/:id/page/:template_id/*route",
"method": "GET",
"process": "sui.Page.Get",
"in": ["$param.id", "$param.template_id", "$param.route"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/page/tree/:template_id/*route",
"method": "GET",
"process": "sui.Page.Tree",
"in": ["$param.id", "$param.template_id", "$param.route"],
"out": { "status": 200, "type": "application/json" }
},
{
"path": "/:id/editor/render/:template_id/*route",
Expand Down
85 changes: 65 additions & 20 deletions sui/api/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ import (

func init() {
process.RegisterGroup("sui", map[string]process.Handler{
"template.get": TemplateGet,
"template.find": TemplateFind,
"template.locale.get": TemplateLocaleGet,
"template.theme.get": TemplateThemeGet,
"template.block.get": TemplateBlockGet,
"template.block.find": TemplateBlockFind,
"template.component.get": TemplateComponentGet,
"template.component.find": TemplateComponentFind,
"template.get": TemplateGet,
"template.find": TemplateFind,

"locale.get": LocaleGet,
"theme.get": ThemeGet,

"block.get": BlockGet,
"block.find": BlockFind,

"component.get": ComponentGet,
"component.find": ComponentFind,

"page.tree": PageTree,
"page.get": PageGet,

"editor.render": EditorRender,
"editor.source": EditorSource,
Expand Down Expand Up @@ -48,8 +54,8 @@ func TemplateFind(process *process.Process) interface{} {
return template
}

// TemplateLocaleGet handle the find Template request
func TemplateLocaleGet(process *process.Process) interface{} {
// LocaleGet handle the find Template request
func LocaleGet(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
Expand All @@ -61,8 +67,8 @@ func TemplateLocaleGet(process *process.Process) interface{} {
return template.Locales()
}

// TemplateThemeGet handle the find Template request
func TemplateThemeGet(process *process.Process) interface{} {
// ThemeGet handle the find Template request
func ThemeGet(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
Expand All @@ -74,8 +80,8 @@ func TemplateThemeGet(process *process.Process) interface{} {
return template.Themes()
}

// TemplateBlockGet handle the find Template request
func TemplateBlockGet(process *process.Process) interface{} {
// BlockGet handle the find Template request
func BlockGet(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
Expand All @@ -93,8 +99,8 @@ func TemplateBlockGet(process *process.Process) interface{} {
return blocks
}

// TemplateBlockFind handle the find Template request
func TemplateBlockFind(process *process.Process) interface{} {
// BlockFind handle the find Template request
func BlockFind(process *process.Process) interface{} {
process.ValidateArgNums(3)

sui := get(process)
Expand All @@ -114,8 +120,8 @@ func TemplateBlockFind(process *process.Process) interface{} {
return block.Source()
}

// TemplateComponentGet handle the find Template request
func TemplateComponentGet(process *process.Process) interface{} {
// ComponentGet handle the find Template request
func ComponentGet(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
Expand All @@ -133,8 +139,8 @@ func TemplateComponentGet(process *process.Process) interface{} {
return components
}

// TemplateComponentFind handle the find Template request
func TemplateComponentFind(process *process.Process) interface{} {
// ComponentFind handle the find Template request
func ComponentFind(process *process.Process) interface{} {
process.ValidateArgNums(3)

sui := get(process)
Expand All @@ -153,6 +159,45 @@ func TemplateComponentFind(process *process.Process) interface{} {
return component.Source()
}

// PageTree handle the find Template request
func PageTree(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
templateID := process.ArgsString(1)

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

route := route(process, 2)
tree, err := tmpl.PageTree(route)
if err != nil {
exception.New(err.Error(), 500).Throw()
}
return tree
}

// PageGet handle the find Template request
func PageGet(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
templateID := process.ArgsString(1)

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

tree, err := tmpl.Pages()
if err != nil {
exception.New(err.Error(), 500).Throw()
}
return tree
}

// EditorRender handle the render page request
func EditorRender(process *process.Process) interface{} {
process.ValidateArgNums(3)
Expand Down
56 changes: 50 additions & 6 deletions sui/api/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestTemplateLocaleGet(t *testing.T) {
defer clean()

// test demo
p, err := process.Of("sui.template.locale.get", "demo", "tech-blue")
p, err := process.Of("sui.locale.get", "demo", "tech-blue")
if err != nil {
t.Fatal(err)
}
Expand All @@ -75,7 +75,7 @@ func TestTemplateThemeGet(t *testing.T) {
defer clean()

// test demo
p, err := process.Of("sui.template.theme.get", "demo", "tech-blue")
p, err := process.Of("sui.theme.get", "demo", "tech-blue")
if err != nil {
t.Fatal(err)
}
Expand All @@ -96,7 +96,7 @@ func TestTemplateBlockGet(t *testing.T) {
defer clean()

// test demo
p, err := process.Of("sui.template.block.get", "demo", "tech-blue")
p, err := process.Of("sui.block.get", "demo", "tech-blue")
if err != nil {
t.Fatal(err)
}
Expand All @@ -119,7 +119,7 @@ func TestTemplateBlockFind(t *testing.T) {
defer clean()

// test demo
p, err := process.Of("sui.template.block.find", "demo", "tech-blue", "ColumnsTwo")
p, err := process.Of("sui.block.find", "demo", "tech-blue", "ColumnsTwo")
if err != nil {
t.Fatal(err)
}
Expand All @@ -138,7 +138,7 @@ func TestTemplateComponentGet(t *testing.T) {
defer clean()

// test demo
p, err := process.Of("sui.template.component.get", "demo", "tech-blue")
p, err := process.Of("sui.component.get", "demo", "tech-blue")
if err != nil {
t.Fatal(err)
}
Expand All @@ -160,7 +160,7 @@ func TestTemplateComponentFind(t *testing.T) {
defer clean()

// test demo
p, err := process.Of("sui.template.component.find", "demo", "tech-blue", "Box")
p, err := process.Of("sui.component.find", "demo", "tech-blue", "Box")
if err != nil {
t.Fatal(err)
}
Expand All @@ -174,6 +174,50 @@ func TestTemplateComponentFind(t *testing.T) {
assert.Contains(t, res.(string), "window.component__Box=")
}

func TestTemplatePageTree(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.page.tree", "demo", "tech-blue")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

assert.IsType(t, []*core.PageTreeNode{}, res)
assert.Equal(t, 5, len(res.([]*core.PageTreeNode)))
assert.Equal(t, "error", res.([]*core.PageTreeNode)[0].Name)
assert.Equal(t, "index", res.([]*core.PageTreeNode)[1].Name)
}

func TestTemplatePageGet(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.page.get", "demo", "tech-blue", "/index/[invite]")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

pages := res.([]core.IPage)
assert.IsType(t, []core.IPage{}, pages)
assert.Equal(t, 8, len(pages))
for _, page := range pages {
assert.IsType(t, &local.Page{}, page)
}
}

func TestEditorRender(t *testing.T) {
load(t)
defer clean()
Expand Down
4 changes: 4 additions & 0 deletions sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type SUI interface {
// ITemplate is the interface for the ITemplate
type ITemplate interface {
Pages() ([]IPage, error)
PageTree(route string) ([]*PageTreeNode, error)
Page(route string) (IPage, error)

Blocks() ([]IBlock, error)
Expand All @@ -29,6 +30,9 @@ type ITemplate interface {
// IPage is the interface for the page
type IPage interface {
Load() error

Get() *Page

EditorRender(request *Request) (*ResponseEditor, error)
EditorPageSource() ResponseSource
EditorScriptSource() ResponseSource
Expand Down
5 changes: 5 additions & 0 deletions sui/core/page.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package core

// Get get the base info
func (page *Page) Get() *Page {
return page
}

// GetHTML get the html
func (page *Page) GetHTML() {}

Expand Down
10 changes: 10 additions & 0 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ type Page struct {
Document []byte `json:"-"`
}

// PageTreeNode is the struct for the page tree node
type PageTreeNode struct {
Name string `json:"name,omitempty"`
IsDir bool `json:"is_dir,omitempty"`
Children []*PageTreeNode `json:"children,omitempty"`
IPage IPage `json:"page,omitempty"`
Expand bool `json:"expand,omitempty"`
Active bool `json:"active,omitempty"`
}

// Component is the struct for the component
type Component struct {
ID string `json:"id"`
Expand Down
Loading

0 comments on commit eb49703

Please sign in to comment.