[add] SUI tempate API

This commit is contained in:
Max 2023-09-27 19:13:25 +08:00
parent ab7ace8a24
commit 9bf92c8c1b
3 changed files with 43 additions and 3 deletions

View file

@ -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"],

View file

@ -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.<ID>.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)]

View file

@ -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)