[add] form widget upload api and process
This commit is contained in:
parent
b84c64100c
commit
9d1dea4073
9 changed files with 110 additions and 2 deletions
|
|
@ -22,6 +22,11 @@ var processActionDefaults = map[string]*action.Process{
|
||||||
Guard: "bearer-jwt",
|
Guard: "bearer-jwt",
|
||||||
Default: []interface{}{nil, nil, nil},
|
Default: []interface{}{nil, nil, nil},
|
||||||
},
|
},
|
||||||
|
"Upload": {
|
||||||
|
Name: "yao.form.Upload",
|
||||||
|
Guard: "bearer-jwt",
|
||||||
|
Default: []interface{}{nil, nil, nil},
|
||||||
|
},
|
||||||
"Find": {
|
"Find": {
|
||||||
Name: "yao.form.Find",
|
Name: "yao.form.Find",
|
||||||
Guard: "bearer-jwt",
|
Guard: "bearer-jwt",
|
||||||
|
|
@ -60,6 +65,10 @@ func (act *ActionDSL) SetDefaultProcess() {
|
||||||
Merge(processActionDefaults["Component"]).
|
Merge(processActionDefaults["Component"]).
|
||||||
SetHandler(processHandler)
|
SetHandler(processHandler)
|
||||||
|
|
||||||
|
act.Upload = action.ProcessOf(act.Upload).
|
||||||
|
Merge(processActionDefaults["Upload"]).
|
||||||
|
SetHandler(processHandler)
|
||||||
|
|
||||||
act.Find = action.ProcessOf(act.Find).
|
act.Find = action.ProcessOf(act.Find).
|
||||||
WithBefore(act.BeforeFind).WithAfter(act.AfterFind).
|
WithBefore(act.BeforeFind).WithAfter(act.AfterFind).
|
||||||
Merge(processActionDefaults["Find"]).
|
Merge(processActionDefaults["Find"]).
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,8 @@ func (form *DSL) getAction(path string) (*action.Process, error) {
|
||||||
return form.Action.Setting, nil
|
return form.Action.Setting, nil
|
||||||
case "/api/__yao/form/:id/component/:xpath/:method":
|
case "/api/__yao/form/:id/component/:xpath/:method":
|
||||||
return form.Action.Component, nil
|
return form.Action.Component, nil
|
||||||
|
case "/api/__yao/form/:id/upload/:xpath/:method":
|
||||||
|
return form.Action.Upload, nil
|
||||||
case "/api/__yao/form/:id/find/:primary":
|
case "/api/__yao/form/:id/find/:primary":
|
||||||
return form.Action.Find, nil
|
return form.Action.Find, nil
|
||||||
case "/api/__yao/form/:id/save":
|
case "/api/__yao/form/:id/save":
|
||||||
|
|
@ -114,6 +116,18 @@ func exportAPI() error {
|
||||||
}
|
}
|
||||||
http.Paths = append(http.Paths, path)
|
http.Paths = append(http.Paths, path)
|
||||||
|
|
||||||
|
// POST /api/__yao/table/:id/upload/:xpath/:method -> Default process: yao.form.Upload $param.id $param.xpath $param.method $file.file
|
||||||
|
path = gou.Path{
|
||||||
|
Label: "Upload",
|
||||||
|
Description: "Upload",
|
||||||
|
Path: "/:id/upload/:xpath/:method",
|
||||||
|
Method: "POST",
|
||||||
|
Process: "yao.form.Upload",
|
||||||
|
In: []string{"$param.id", "$param.xpath", "$param.method", "$file.file"},
|
||||||
|
Out: gou.Out{Status: 200, Type: "application/json"},
|
||||||
|
}
|
||||||
|
http.Paths = append(http.Paths, path)
|
||||||
|
|
||||||
// POST /api/__yao/form/:id/save -> Default process: yao.form.Save $param.id :payload
|
// POST /api/__yao/form/:id/save -> Default process: yao.form.Save $param.id :payload
|
||||||
path = gou.Path{
|
path = gou.Path{
|
||||||
Label: "Save",
|
Label: "Save",
|
||||||
|
|
|
||||||
|
|
@ -240,6 +240,11 @@ func (dsl *DSL) Xgen() (map[string]interface{}, error) {
|
||||||
setting["config"] = dsl.Config
|
setting["config"] = dsl.Config
|
||||||
for _, cProp := range dsl.CProps {
|
for _, cProp := range dsl.CProps {
|
||||||
err := cProp.Replace(setting, func(cProp component.CloudPropsDSL) interface{} {
|
err := cProp.Replace(setting, func(cProp component.CloudPropsDSL) interface{} {
|
||||||
|
|
||||||
|
if cProp.Type == "Upload" {
|
||||||
|
return fmt.Sprintf("/api/__yao/form/%s%s", dsl.ID, cProp.UploadPath())
|
||||||
|
}
|
||||||
|
|
||||||
return map[string]interface{}{
|
return map[string]interface{}{
|
||||||
"api": fmt.Sprintf("/api/__yao/form/%s%s", dsl.ID, cProp.Path()),
|
"api": fmt.Sprintf("/api/__yao/form/%s%s", dsl.ID, cProp.Path()),
|
||||||
"params": cProp.Query,
|
"params": cProp.Query,
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
|
"github.com/yaoapp/yao/fs"
|
||||||
"github.com/yaoapp/yao/i18n"
|
"github.com/yaoapp/yao/i18n"
|
||||||
"github.com/yaoapp/yao/model"
|
"github.com/yaoapp/yao/model"
|
||||||
"github.com/yaoapp/yao/runtime"
|
"github.com/yaoapp/yao/runtime"
|
||||||
|
|
@ -36,6 +37,12 @@ func prepare(t *testing.T, language ...string) {
|
||||||
i18n.Load(config.Conf)
|
i18n.Load(config.Conf)
|
||||||
share.DBConnect(config.Conf.DB) // removed later
|
share.DBConnect(config.Conf.DB) // removed later
|
||||||
|
|
||||||
|
// load fs
|
||||||
|
err = fs.Load(config.Conf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
// load scripts
|
// load scripts
|
||||||
err = script.Load(config.Conf)
|
err = script.Load(config.Conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ func exportProcess() {
|
||||||
gou.RegisterProcessHandler("yao.form.setting", processSetting)
|
gou.RegisterProcessHandler("yao.form.setting", processSetting)
|
||||||
gou.RegisterProcessHandler("yao.form.xgen", processXgen)
|
gou.RegisterProcessHandler("yao.form.xgen", processXgen)
|
||||||
gou.RegisterProcessHandler("yao.form.component", processComponent)
|
gou.RegisterProcessHandler("yao.form.component", processComponent)
|
||||||
|
gou.RegisterProcessHandler("yao.form.upload", processUpload)
|
||||||
gou.RegisterProcessHandler("yao.form.find", processFind)
|
gou.RegisterProcessHandler("yao.form.find", processFind)
|
||||||
gou.RegisterProcessHandler("yao.form.save", processSave)
|
gou.RegisterProcessHandler("yao.form.save", processSave)
|
||||||
gou.RegisterProcessHandler("yao.form.create", processCreate)
|
gou.RegisterProcessHandler("yao.form.create", processCreate)
|
||||||
|
|
@ -59,6 +60,35 @@ func processComponent(process *gou.Process) interface{} {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func processUpload(process *gou.Process) interface{} {
|
||||||
|
|
||||||
|
process.ValidateArgNums(4)
|
||||||
|
form := MustGet(process)
|
||||||
|
xpath := process.ArgsString(1)
|
||||||
|
method := process.ArgsString(2)
|
||||||
|
key := fmt.Sprintf("%s.$%s", xpath, method)
|
||||||
|
|
||||||
|
// get cloud props
|
||||||
|
cProp, has := form.CProps[key]
|
||||||
|
if !has {
|
||||||
|
exception.New("%s does not exist", 400, key).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
// $file.file
|
||||||
|
tmpfile, ok := process.Args[3].(gou.UploadFile)
|
||||||
|
if !ok {
|
||||||
|
exception.New("parameters error: %v", 400, process.Args[3]).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
// execute upload
|
||||||
|
res, err := cProp.ExecUpload(process, tmpfile)
|
||||||
|
if err != nil {
|
||||||
|
exception.New(err.Error(), 500).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
func processSetting(process *gou.Process) interface{} {
|
func processSetting(process *gou.Process) interface{} {
|
||||||
form := MustGet(process)
|
form := MustGet(process)
|
||||||
process.Args = append(process.Args, process.Args[0]) // formle name
|
process.Args = append(process.Args, process.Args[0]) // formle name
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package form
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
@ -156,6 +157,27 @@ func TestProcessComponent(t *testing.T) {
|
||||||
assert.Equal(t, "checked", pets[1]["status"])
|
assert.Equal(t, "checked", pets[1]["status"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProcessUpload(t *testing.T) {
|
||||||
|
load(t)
|
||||||
|
clear(t)
|
||||||
|
testData(t)
|
||||||
|
args := []interface{}{
|
||||||
|
"pet",
|
||||||
|
"fields.form.相关图片.edit.props",
|
||||||
|
"api",
|
||||||
|
gou.UploadFile{TempFile: tempFile(t)},
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := gou.NewProcess("yao.form.Upload", args...).Exec()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
file, ok := res.(string)
|
||||||
|
assert.True(t, ok)
|
||||||
|
assert.NotEmpty(t, file)
|
||||||
|
}
|
||||||
|
|
||||||
func TestProcessComponentError(t *testing.T) {
|
func TestProcessComponentError(t *testing.T) {
|
||||||
load(t)
|
load(t)
|
||||||
clear(t)
|
clear(t)
|
||||||
|
|
@ -182,6 +204,7 @@ func TestProcessSetting(t *testing.T) {
|
||||||
|
|
||||||
data := any.Of(res).MapStr().Dot()
|
data := any.Of(res).MapStr().Dot()
|
||||||
assert.Equal(t, "/api/__yao/form/pet/component/fields.form."+url.QueryEscape("状态")+".edit.props.xProps/remote", data.Get("fields.form.状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/form/pet/component/fields.form."+url.QueryEscape("状态")+".edit.props.xProps/remote", data.Get("fields.form.状态.edit.props.xProps.remote.api"))
|
||||||
|
assert.Equal(t, "/api/__yao/form/pet/upload/fields.form."+url.QueryEscape("相关图片")+".edit.props/api", data.Get("fields.form.相关图片.edit.props.api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessXgen(t *testing.T) {
|
func TestProcessXgen(t *testing.T) {
|
||||||
|
|
@ -196,6 +219,7 @@ func TestProcessXgen(t *testing.T) {
|
||||||
|
|
||||||
data := any.Of(res).MapStr().Dot()
|
data := any.Of(res).MapStr().Dot()
|
||||||
assert.Equal(t, "/api/__yao/form/pet/component/fields.form."+url.QueryEscape("状态")+".edit.props.xProps/remote", data.Get("fields.form.状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/form/pet/component/fields.form."+url.QueryEscape("状态")+".edit.props.xProps/remote", data.Get("fields.form.状态.edit.props.xProps.remote.api"))
|
||||||
|
assert.Equal(t, "/api/__yao/form/pet/upload/fields.form."+url.QueryEscape("相关图片")+".edit.props/api", data.Get("fields.form.相关图片.edit.props.api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func load(t *testing.T) {
|
func load(t *testing.T) {
|
||||||
|
|
@ -222,6 +246,21 @@ func testData(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tempFile(t *testing.T) string {
|
||||||
|
file, err := os.CreateTemp("", "unit-test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
_, err = file.Write([]byte("HELLO"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return file.Name()
|
||||||
|
}
|
||||||
|
|
||||||
func clear(t *testing.T) {
|
func clear(t *testing.T) {
|
||||||
for _, m := range gou.Models {
|
for _, m := range gou.Models {
|
||||||
err := m.DropTable()
|
err := m.DropTable()
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ type ActionDSL struct {
|
||||||
Bind *BindActionDSL `json:"bind,omitempty"`
|
Bind *BindActionDSL `json:"bind,omitempty"`
|
||||||
Setting *action.Process `json:"setting,omitempty"`
|
Setting *action.Process `json:"setting,omitempty"`
|
||||||
Component *action.Process `json:"component,omitempty"`
|
Component *action.Process `json:"component,omitempty"`
|
||||||
|
Upload *action.Process `json:"upload,omitempty"`
|
||||||
Find *action.Process `json:"find,omitempty"`
|
Find *action.Process `json:"find,omitempty"`
|
||||||
Save *action.Process `json:"save,omitempty"`
|
Save *action.Process `json:"save,omitempty"`
|
||||||
Update *action.Process `json:"update,omitempty"`
|
Update *action.Process `json:"update,omitempty"`
|
||||||
|
|
|
||||||
|
|
@ -154,10 +154,10 @@ func exportAPI() error {
|
||||||
}
|
}
|
||||||
http.Paths = append(http.Paths, path)
|
http.Paths = append(http.Paths, path)
|
||||||
|
|
||||||
// POST /api/__yao/table/:id/upload/:xpath/:method -> Default process: yao.table.Component $param.id $param.xpath $param.method $file.file
|
// POST /api/__yao/table/:id/upload/:xpath/:method -> Default process: yao.table.Upload $param.id $param.xpath $param.method $file.file
|
||||||
path = gou.Path{
|
path = gou.Path{
|
||||||
Label: "Upload",
|
Label: "Upload",
|
||||||
Description: "Component",
|
Description: "Upload",
|
||||||
Path: "/:id/upload/:xpath/:method",
|
Path: "/:id/upload/:xpath/:method",
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
Process: "yao.table.Upload",
|
Process: "yao.table.Upload",
|
||||||
|
|
|
||||||
|
|
@ -383,6 +383,7 @@ func TestProcessSetting(t *testing.T) {
|
||||||
assert.Equal(t, "查看详情2", data.Get("header.preset.import.actions[1].title"))
|
assert.Equal(t, "查看详情2", data.Get("header.preset.import.actions[1].title"))
|
||||||
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".view.props.xProps/remote", data.Get("fields.table.入院状态.view.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".view.props.xProps/remote", data.Get("fields.table.入院状态.view.props.xProps.remote.api"))
|
||||||
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".edit.props.xProps/remote", data.Get("fields.table.入院状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".edit.props.xProps/remote", data.Get("fields.table.入院状态.edit.props.xProps.remote.api"))
|
||||||
|
assert.Equal(t, "/api/__yao/table/pet/upload/fields.table."+url.QueryEscape("相关图片")+".edit.props/api", data.Get("fields.table.相关图片.edit.props.api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessXgen(t *testing.T) {
|
func TestProcessXgen(t *testing.T) {
|
||||||
|
|
@ -401,6 +402,8 @@ func TestProcessXgen(t *testing.T) {
|
||||||
assert.Equal(t, "查看详情2", data.Get("header.preset.import.actions[1].title"))
|
assert.Equal(t, "查看详情2", data.Get("header.preset.import.actions[1].title"))
|
||||||
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".view.props.xProps/remote", data.Get("fields.table.入院状态.view.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".view.props.xProps/remote", data.Get("fields.table.入院状态.view.props.xProps.remote.api"))
|
||||||
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".edit.props.xProps/remote", data.Get("fields.table.入院状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".edit.props.xProps/remote", data.Get("fields.table.入院状态.edit.props.xProps.remote.api"))
|
||||||
|
assert.Equal(t, "/api/__yao/table/pet/upload/fields.table."+url.QueryEscape("相关图片")+".edit.props/api", data.Get("fields.table.相关图片.edit.props.api"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func load(t *testing.T) {
|
func load(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue