+ insert & deletewhere & updatewhere

This commit is contained in:
Max 2021-10-01 18:19:42 +08:00
parent dfa40add64
commit dafbdbf168
5 changed files with 160 additions and 5 deletions

View file

@ -109,6 +109,98 @@ func TestProcessDelete(t *testing.T) {
capsule.Query().Table("service").Where("id", id).Delete()
}
func TestProcessInsert(t *testing.T) {
args := []interface{}{
"service",
[]string{"name", "short_name", "kind_id", "manu_id", "price_options"},
[][]interface{}{
{"I腾讯云主机I1", "高性能云主机", 3, 1, []string{"按月订阅"}},
{"I腾讯云主机I2", "高性能云主机", 3, 1, []string{"按月订阅"}},
},
}
process := gou.NewProcess("xiang.table.Insert", args...)
response := table.ProcessInsert(process)
assert.Nil(t, response)
// 清空数据
capsule.Query().Table("service").Where("name", "like", "I腾讯云主机I%").Delete()
}
func TestProcessDeleteWhere(t *testing.T) {
args := []interface{}{
"service",
map[string]interface{}{
"name": "腾讯黑岩云主机",
"short_name": "高性能云主机",
"kind_id": 3,
"manu_id": 1,
"price_options": []string{"按月订阅"},
},
}
process := gou.NewProcess("xiang.table.Save", args...)
response := table.ProcessSave(process)
assert.NotNil(t, response)
assert.True(t, any.Of(response).IsInt())
id := any.Of(response).CInt()
args = []interface{}{
"service",
gou.QueryParam{
Wheres: []gou.QueryWhere{
{Column: "id", Value: id},
},
},
}
process = gou.NewProcess("xiang.table.DeleteWhere", args...)
response = table.ProcessDeleteWhere(process)
assert.NotNil(t, response)
assert.True(t, any.Of(response).IsInt())
assert.Equal(t, any.Of(response).CInt(), 1)
// 清空数据
capsule.Query().Table("service").Where("id", id).Delete()
}
func TestProcessUpdateWhere(t *testing.T) {
args := []interface{}{
"service",
map[string]interface{}{
"name": "腾讯黑岩云主机",
"short_name": "高性能云主机",
"kind_id": 3,
"manu_id": 1,
"price_options": []string{"按月订阅"},
},
}
process := gou.NewProcess("xiang.table.Save", args...)
response := table.ProcessSave(process)
assert.NotNil(t, response)
assert.True(t, any.Of(response).IsInt())
id := any.Of(response).CInt()
args = []interface{}{
"service",
gou.QueryParam{
Wheres: []gou.QueryWhere{
{Column: "id", Value: id},
},
},
map[string]interface{}{
"name": "腾讯黑岩云主机UP",
},
}
process = gou.NewProcess("xiang.table.UpdateWhere", args...)
response = table.ProcessUpdateWhere(process)
assert.NotNil(t, response)
assert.True(t, any.Of(response).IsInt())
assert.Equal(t, any.Of(response).CInt(), 1)
// 清空数据
capsule.Query().Table("service").Where("id", id).Delete()
}
func TestProcessSetting(t *testing.T) {
args := []interface{}{"service", ""}
process := gou.NewProcess("xiang.table.Setting", args...)

4
go.mod
View file

@ -25,13 +25,13 @@ require (
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cobra v1.2.1 // indirect
github.com/stretchr/testify v1.7.0
github.com/yaoapp/gou v0.0.0-20211001062655-04dd63da82aa // indirect
github.com/yaoapp/gou v0.0.0-20211001101736-7de3f97a840a // indirect
github.com/yaoapp/kun v0.6.7
github.com/yaoapp/xun v0.5.2 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6 // indirect
golang.org/x/sys v0.0.0-20210930212924-f542c8878de8 // indirect
golang.org/x/sys v0.0.0-20211001092434-39dca1131b70 // indirect
google.golang.org/genproto v0.0.0-20210930144712-2e2e1008e8a3 // indirect
google.golang.org/grpc v1.41.0 // indirect
)

6
go.sum
View file

@ -382,6 +382,10 @@ github.com/yaoapp/gou v0.0.0-20210929143406-112eed6cdfd9 h1:rsZXATrMRQzlEraTB7Jo
github.com/yaoapp/gou v0.0.0-20210929143406-112eed6cdfd9/go.mod h1:vcNZXFRgGuZWzjtYH65ck1o6nzWYDpToQ09zHC9xNDE=
github.com/yaoapp/gou v0.0.0-20211001062655-04dd63da82aa h1:+FCfvC/JPDSpxLQ5rcNi5fClITeYO2p736rSLxKKhqQ=
github.com/yaoapp/gou v0.0.0-20211001062655-04dd63da82aa/go.mod h1:vcNZXFRgGuZWzjtYH65ck1o6nzWYDpToQ09zHC9xNDE=
github.com/yaoapp/gou v0.0.0-20211001095211-689d77d061f1 h1:/6UAXt82jTKlYq89mUj6izLI3HUYyALZmDKxk78NIds=
github.com/yaoapp/gou v0.0.0-20211001095211-689d77d061f1/go.mod h1:vcNZXFRgGuZWzjtYH65ck1o6nzWYDpToQ09zHC9xNDE=
github.com/yaoapp/gou v0.0.0-20211001101736-7de3f97a840a h1:E4eybsoNhNrfKzmnjH5z4s3KqK/e2GnROlvlC2l0h2A=
github.com/yaoapp/gou v0.0.0-20211001101736-7de3f97a840a/go.mod h1:vcNZXFRgGuZWzjtYH65ck1o6nzWYDpToQ09zHC9xNDE=
github.com/yaoapp/kun v0.6.1 h1:gbjP5wmAuJLe1RGZ2evH4rPCVWykf0iqbksaHikF/GI=
github.com/yaoapp/kun v0.6.1/go.mod h1:igsTcWDnzpp0HtRN7sBP+XOEN2tzoC5hk2MyoPFs3xA=
github.com/yaoapp/kun v0.6.2 h1:QbtdZpVIklRDGEvL5YYRf/eR05YFZVFk/8rX2vy2K0Q=
@ -598,6 +602,8 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYe
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210930212924-f542c8878de8 h1:g54bVHzt/eKaj59LDwiDyLRXbOdJSfwRxA1WYEJx/Yo=
golang.org/x/sys v0.0.0-20210930212924-f542c8878de8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211001092434-39dca1131b70 h1:pGleJoyD1yA5HfvuaksHxD0404gsEkNDerKsQ0N0y1s=
golang.org/x/sys v0.0.0-20211001092434-39dca1131b70/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View file

@ -13,6 +13,9 @@ func init() {
gou.RegisterProcessHandler("xiang.table.Find", ProcessFind)
gou.RegisterProcessHandler("xiang.table.Save", ProcessSave)
gou.RegisterProcessHandler("xiang.table.Delete", ProcessDelete)
gou.RegisterProcessHandler("xiang.table.Insert", ProcessInsert)
gou.RegisterProcessHandler("xiang.table.UpdateWhere", ProcessUpdateWhere)
gou.RegisterProcessHandler("xiang.table.DeleteWhere", ProcessDeleteWhere)
gou.RegisterProcessHandler("xiang.table.Setting", ProcessSetting)
}
@ -81,8 +84,62 @@ func ProcessDelete(process *gou.Process) interface{} {
return gou.NewProcess(api.Process, id).Run()
}
// ProcessDeleteWhere xiang.table.DeleteWhere
// 按条件批量删除数据, 请求成功返回删除行数
func ProcessDeleteWhere(process *gou.Process) interface{} {
process.ValidateArgNums(2)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["delete-where"].ValidateLoop("xiang.table.DeleteWhere")
if process.NumOfArgsIs(3) && api.IsAllow(process.Args[2]) {
return nil
}
// 批量删除
param := api.MergeDefaultQueryParam(process.ArgsQueryParams(1), 0)
if param.Limit == 0 { // 限定删除行
param.Limit = 10
}
return gou.NewProcess(api.Process, param).Run()
}
// ProcessUpdateWhere xiang.table.UpdateWhere
// 按条件批量更新数据, 请求成功返回更新行数
func ProcessUpdateWhere(process *gou.Process) interface{} {
process.ValidateArgNums(3)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["update-where"].ValidateLoop("xiang.table.UpdateWhere")
if process.NumOfArgsIs(4) && api.IsAllow(process.Args[3]) {
return nil
}
// 批量更新
param := api.MergeDefaultQueryParam(process.ArgsQueryParams(1), 0)
if param.Limit == 0 { // 限定删除行
param.Limit = 10
}
return gou.NewProcess(api.Process, param, process.Args[2]).Run()
}
// ProcessInsert xiang.table.Insert
// 插入多条数据记录,请求成功返回插入行数
func ProcessInsert(process *gou.Process) interface{} {
process.ValidateArgNums(3)
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["insert"].ValidateLoop("xiang.table.Insert")
if process.NumOfArgsIs(4) && api.IsAllow(process.Args[3]) {
return nil
}
return gou.NewProcess(api.Process, process.Args[1:]...).Run()
}
// ProcessSetting xiang.table.Setting
// 删除指定主键值的数据记录, 请求成功返回null
// 读取数据表格配置信息, 请求成功返回配置信息对象
func ProcessSetting(process *gou.Process) interface{} {
process.ValidateArgNums(2)
name := process.ArgsString(0)

View file

@ -49,7 +49,7 @@
"path": "/:name/insert",
"method": "POST",
"process": "xiang.table.Insert",
"in": ["$param.name", ":payload"],
"in": ["$param.name", "$payload.columns", "$payload.values"],
"out": {
"status": 200,
"type": "application/json"
@ -59,7 +59,7 @@
"path": "/:name/delete/where",
"method": "POST",
"process": "xiang.table.DeleteWhere",
"in": ["$param.name", ":query-param", ":payload"],
"in": ["$param.name", ":query-param"],
"out": {
"status": 200,
"type": "application/json"