+ Save Hook
This commit is contained in:
parent
77047aa3e4
commit
70e50bc162
7 changed files with 84 additions and 6 deletions
|
|
@ -20,5 +20,5 @@ func check(t *testing.T) {
|
|||
for key := range gou.Flows {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
assert.Equal(t, 14, len(keys))
|
||||
assert.Equal(t, 16, len(keys))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,14 +84,27 @@ func ProcessFind(process *gou.Process) interface{} {
|
|||
// ProcessSave xiang.table.Save
|
||||
// 保存单条记录。如数据记录中包含主键字段则更新,不包含主键字段则创建记录;返回创建或更新的记录主键值
|
||||
func ProcessSave(process *gou.Process) interface{} {
|
||||
process.ValidateArgNums(2)
|
||||
|
||||
// 读取表格名称
|
||||
process.ValidateArgNums(1)
|
||||
name := process.ArgsString(0)
|
||||
table := Select(name)
|
||||
api := table.APIs["save"].ValidateLoop("xiang.table.save")
|
||||
if process.NumOfArgsIs(3) && api.IsAllow(process.Args[2]) {
|
||||
return nil
|
||||
}
|
||||
return gou.NewProcess(api.Process, process.Args[1]).Run()
|
||||
|
||||
// Before Hook
|
||||
process.Args = table.Before(table.Hooks.BeforeSave, process.Args)
|
||||
|
||||
// 参数处理
|
||||
process.ValidateArgNums(2)
|
||||
|
||||
// 查询数据
|
||||
response := gou.NewProcess(api.Process, process.Args[1]).Run()
|
||||
|
||||
// After Hook
|
||||
return table.After(table.Hooks.AfterSave, response)
|
||||
}
|
||||
|
||||
// ProcessDelete xiang.table.Delete
|
||||
|
|
|
|||
|
|
@ -106,8 +106,7 @@ func TestTableProcessSave(t *testing.T) {
|
|||
"price_options": []string{"按月订阅"},
|
||||
},
|
||||
}
|
||||
process := gou.NewProcess("xiang.table.Save", args...)
|
||||
response := ProcessSave(process)
|
||||
response := gou.NewProcess("xiang.table.Save", args...).Run()
|
||||
assert.NotNil(t, response)
|
||||
assert.True(t, any.Of(response).IsInt())
|
||||
|
||||
|
|
@ -117,6 +116,20 @@ func TestTableProcessSave(t *testing.T) {
|
|||
capsule.Query().Table("service").Where("id", id).Delete()
|
||||
}
|
||||
|
||||
func TestTableProcessSaveWithHook(t *testing.T) {
|
||||
args := []interface{}{"hooks.save"}
|
||||
response := gou.NewProcess("xiang.table.Save", args...).Run()
|
||||
assert.NotNil(t, response)
|
||||
res := any.Of(response).Map()
|
||||
assert.True(t, any.Of(res.Get("id")).IsInt())
|
||||
assert.Equal(t, float64(100), res.Get("after"))
|
||||
|
||||
id := any.Of(res.Get("id")).CInt()
|
||||
|
||||
// 清空数据
|
||||
capsule.Query().Table("service").Where("id", id).Delete()
|
||||
}
|
||||
|
||||
func TestTableProcessDelete(t *testing.T) {
|
||||
args := []interface{}{
|
||||
"service",
|
||||
|
|
|
|||
|
|
@ -48,5 +48,5 @@ func check(t *testing.T) {
|
|||
for key := range Tables {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
assert.Equal(t, 6, len(keys))
|
||||
assert.Equal(t, 7, len(keys))
|
||||
}
|
||||
|
|
|
|||
7
tests/flows/hooks/after_save.flow.json
Normal file
7
tests/flows/hooks/after_save.flow.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"label": "After:Save",
|
||||
"version": "1.0.0",
|
||||
"description": "After:Save",
|
||||
"nodes": [],
|
||||
"output": { "id": "{{$in.0}}", "after": 100 }
|
||||
}
|
||||
15
tests/flows/hooks/before_save.flow.json
Normal file
15
tests/flows/hooks/before_save.flow.json
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"label": "Before:Save",
|
||||
"version": "1.0.0",
|
||||
"description": "Before:Save",
|
||||
"nodes": [],
|
||||
"output": [
|
||||
{
|
||||
"name": "腾讯黑岩云主机",
|
||||
"short_name": "高性能云主机",
|
||||
"kind_id": 3,
|
||||
"manu_id": 1,
|
||||
"price_options": "按月订阅"
|
||||
}
|
||||
]
|
||||
}
|
||||
30
tests/tables/hooks/save.tab.json
Normal file
30
tests/tables/hooks/save.tab.json
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"name": "云服务库",
|
||||
"version": "1.0.0",
|
||||
"decription": "云服务库",
|
||||
"bind": { "model": "service" },
|
||||
"hooks": {
|
||||
"before:save": "flows.hooks.before_save",
|
||||
"after:save": "flows.hooks.after_save"
|
||||
},
|
||||
"apis": {},
|
||||
"columns": {},
|
||||
"filters": {},
|
||||
"list": {
|
||||
"primary": "id",
|
||||
"layout": {
|
||||
"columns": [{ "name": "ID", "width": 6 }],
|
||||
"filters": []
|
||||
},
|
||||
"actions": {}
|
||||
},
|
||||
"edit": {
|
||||
"primary": "id",
|
||||
"layout": {
|
||||
"fieldset": [{ "columns": [{ "name": "ID", "width": 6 }] }]
|
||||
},
|
||||
"actions": { "cancel": {} }
|
||||
},
|
||||
"insert": {},
|
||||
"view": {}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue