From 0e44d5689faa673aa8abbeb03694ae95c316b733 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 1 Jan 2022 20:14:46 +0800 Subject: [PATCH] + Table Select API --- table/api.go | 2 +- table/process.go | 14 ++++++++++++++ table/process_test.go | 31 +++++++++++++++++++++++++++++++ table/table.go | 3 ++- tests/tables/service.json | 5 ++--- xiang/apis/table.http.json | 16 ++++++++++++++++ 6 files changed, 66 insertions(+), 5 deletions(-) diff --git a/table/api.go b/table/api.go index 4ae931d3..68fce173 100644 --- a/table/api.go +++ b/table/api.go @@ -44,7 +44,7 @@ func apiFindDefault(model *gou.Model, withs map[string]gou.With) share.API { } } -// apiFindDefault 接口默认值 +// apiDefault 接口默认值 func apiDefault(model *gou.Model, name string, process string) share.API { return share.API{ Name: name, diff --git a/table/process.go b/table/process.go index cd1a450a..5c636ec3 100644 --- a/table/process.go +++ b/table/process.go @@ -12,6 +12,7 @@ func init() { // 注册处理器 gou.RegisterProcessHandler("xiang.table.Search", ProcessSearch) gou.RegisterProcessHandler("xiang.table.Find", ProcessFind) + gou.RegisterProcessHandler("xiang.table.Select", ProcessSelect) gou.RegisterProcessHandler("xiang.table.Save", ProcessSave) gou.RegisterProcessHandler("xiang.table.Delete", ProcessDelete) gou.RegisterProcessHandler("xiang.table.Insert", ProcessInsert) @@ -268,3 +269,16 @@ func ProcessQuickSave(process *gou.Process) interface{} { return gou.NewProcess(api.Process, args...).Run() } + +// ProcessSelect xiang.table.Select +// 单表数据查询,一般用于下拉菜单检索 +func ProcessSelect(process *gou.Process) interface{} { + process.ValidateArgNums(1) + name := process.ArgsString(0) + table := Select(name) + api := table.APIs["select"].ValidateLoop("xiang.table.select") + if process.NumOfArgsIs(5) && api.IsAllow(process.Args[4]) { + return nil + } + return gou.NewProcess(api.Process, process.Args[1:]...).Run() +} diff --git a/table/process_test.go b/table/process_test.go index 1658306d..a9bcb15c 100644 --- a/table/process_test.go +++ b/table/process_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/yaoapp/gou" "github.com/yaoapp/kun/any" + "github.com/yaoapp/kun/maps" "github.com/yaoapp/xiang/config" "github.com/yaoapp/xiang/model" "github.com/yaoapp/xiang/share" @@ -358,3 +359,33 @@ func TestTableProcessQuickSave(t *testing.T) { // 清空数据 capsule.Query().Table("service").WhereIn("id", []int{id, newID[0]}).Delete() } + +func TestTableProcessSelect(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 := ProcessSave(process) + assert.NotNil(t, response) + assert.True(t, any.Of(response).IsInt()) + + id := any.Of(response).CInt() + + args = []interface{}{"service", "腾讯", "name", "id"} + + data := gou.NewProcess("xiang.table.select", args...).Run().([]maps.MapStrAny) + assert.Greater(t, len(data), 1) + for _, row := range data { + assert.Contains(t, row.Get("name"), "腾讯") + } + + // 清空数据 + capsule.Query().Table("service").WhereIn("id", []int{id}).Delete() +} diff --git a/table/table.go b/table/table.go index b924dcf7..03a1472d 100644 --- a/table/table.go +++ b/table/table.go @@ -140,7 +140,8 @@ func getDefaultAPIs(bind Bind) map[string]share.API { "delete-where": apiDefaultWhere(model, bind.Withs, "delete-where", "DeleteWhere"), "update-in": apiDefault(model, "update-in", "UpdateWhere"), "update-where": apiDefaultWhere(model, bind.Withs, "update-where", "UpdateWhere"), - "quicksave": apiDefault(model, "quicksave", "EachSaveAfterDelete"), + "quicksave": apiDefault(model, "quicksave", "EachSaveAfterDelete"), // 批量保存 + "select": apiDefault(model, "select", "SelectOption"), // 选择 } return apis diff --git a/tests/tables/service.json b/tests/tables/service.json index 211f3b54..9f3d9611 100644 --- a/tests/tables/service.json +++ b/tests/tables/service.json @@ -18,9 +18,8 @@ } }, "apis": { - "save": { - "guard": "-" - }, + "save": { "guard": "-" }, + "select": { "guard": "-" }, "search": { "process": "models.service.Paginate", "guard": "-", diff --git a/xiang/apis/table.http.json b/xiang/apis/table.http.json index 2fde952f..a7529433 100644 --- a/xiang/apis/table.http.json +++ b/xiang/apis/table.http.json @@ -25,6 +25,22 @@ "type": "application/json" } }, + { + "path": "/:name/select", + "method": "GET", + "process": "xiang.table.Select", + "in": [ + "$param.name", + "$query.keyword", + "$query.name", + "$query.value", + "$query.limit" + ], + "out": { + "status": 200, + "type": "application/json" + } + }, { "path": "/:name/save", "method": "POST",