diff --git a/sui/api/api.go b/sui/api/api.go index c300e14d..c82bf64d 100644 --- a/sui/api/api.go +++ b/sui/api/api.go @@ -11,7 +11,13 @@ var dsl = []byte(` "group": "__yao/sui/v1", "paths": [ { - "path": "/template/:id", + "path": "/:id/template/find/:template_id", + "method": "GET", + "process": "sui.Template.Find", + "in": ["$param.id", "$param.template_id"], + "out": { "status": 200, "type": "application/json" } + },{ + "path": "/:id/template/get", "method": "GET", "process": "sui.Template.Get", "in": ["$param.id"], diff --git a/sui/api/process.go b/sui/api/process.go index 22a8e4b4..cbf5b892 100644 --- a/sui/api/process.go +++ b/sui/api/process.go @@ -8,13 +8,15 @@ import ( func init() { process.RegisterGroup("sui", map[string]process.Handler{ - "template.get": TemplateGet, + "template.get": TemplateGet, + "template.find": TemplateFind, }) } // TemplateGet handle the get Template request // Process sui..templates func TemplateGet(process *process.Process) interface{} { + process.ValidateArgNums(1) sui := get(process) templates, err := sui.GetTemplates() if err != nil { @@ -23,6 +25,18 @@ func TemplateGet(process *process.Process) interface{} { return templates } +// TemplateFind handle the find Template request +func TemplateFind(process *process.Process) interface{} { + process.ValidateArgNums(2) + sui := get(process) + template, err := sui.GetTemplate(process.ArgsString(1)) + if err != nil { + exception.New(err.Error(), 500).Throw() + } + + return template +} + // get the sui func get(process *process.Process) core.SUI { sui, has := core.SUIs[process.ArgsString(0)] diff --git a/sui/api/process_test.go b/sui/api/process_test.go index d25a781c..cbd2ae64 100644 --- a/sui/api/process_test.go +++ b/sui/api/process_test.go @@ -7,9 +7,10 @@ import ( "github.com/yaoapp/gou/process" "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/sui/core" + "github.com/yaoapp/yao/sui/storages/local" ) -func TestTemplates(t *testing.T) { +func TestTemplateGet(t *testing.T) { load(t) defer clean() @@ -28,6 +29,25 @@ func TestTemplates(t *testing.T) { assert.Equal(t, 3, len(res.([]core.ITemplate))) } +func TestTemplateFind(t *testing.T) { + load(t) + defer clean() + + // test demo + p, err := process.Of("sui.template.find", "demo", "tech-blue") + if err != nil { + t.Fatal(err) + } + + res, err := p.Exec() + if err != nil { + t.Fatal(err) + } + + assert.IsType(t, &local.Template{}, res) + assert.Equal(t, "tech-blue", res.(*local.Template).ID) +} + func load(t *testing.T) { prepare(t) err := Load(config.Conf)