+ API 载入默认数据
This commit is contained in:
parent
298aa85552
commit
d87086a575
2 changed files with 129 additions and 22 deletions
127
table/api.go
127
table/api.go
|
|
@ -1,5 +1,130 @@
|
|||
package table
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/yaoapp/gou"
|
||||
)
|
||||
|
||||
// loadAPIs 加载数据管理 API
|
||||
func (tab *Table) loadAPIs() {
|
||||
func (table *Table) loadAPIs() {
|
||||
if table.Bind.Model == "" {
|
||||
return
|
||||
}
|
||||
defaults := getDefaultAPIs(table.Bind)
|
||||
defaults["setting"] = apiDefaultSetting(table)
|
||||
|
||||
for name := range table.APIs {
|
||||
if _, has := defaults[name]; !has {
|
||||
delete(table.APIs, name)
|
||||
continue
|
||||
}
|
||||
|
||||
api := defaults[name]
|
||||
api.Name = name
|
||||
if table.APIs[name].Process == "" {
|
||||
api.Process = table.APIs[name].Process
|
||||
}
|
||||
|
||||
if table.APIs[name].Guard == "" {
|
||||
api.Guard = table.APIs[name].Guard
|
||||
}
|
||||
|
||||
if table.APIs[name].Default == nil {
|
||||
api.Default = table.APIs[name].Default
|
||||
}
|
||||
defaults[name] = api
|
||||
}
|
||||
|
||||
table.APIs = defaults
|
||||
}
|
||||
|
||||
// getDefaultAPIs 读取数据模型绑定的APIs
|
||||
func getDefaultAPIs(bind Bind) map[string]API {
|
||||
name := bind.Model
|
||||
model := gou.Select(name)
|
||||
apis := map[string]API{
|
||||
"search": apiSearchDefault(model, bind.Withs),
|
||||
"find": apiFindDefault(model, bind.Withs),
|
||||
"save": apiDefault(model, "save", "Save"),
|
||||
"delete": apiDefault(model, "delete", "Delete"),
|
||||
"insert": apiDefault(model, "insert", "Insert"),
|
||||
"delete-in": apiDefault(model, "delete-in", "DeleteWhere"),
|
||||
"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"),
|
||||
}
|
||||
|
||||
return apis
|
||||
}
|
||||
|
||||
// apiSearchDefault search 接口默认值
|
||||
func apiSearchDefault(model *gou.Model, withs map[string]gou.With) API {
|
||||
query := gou.QueryParam{}
|
||||
if model.MetaData.Option.Timestamps {
|
||||
query.Orders = []gou.QueryOrder{
|
||||
{Column: "created_at", Option: "desc"},
|
||||
}
|
||||
}
|
||||
|
||||
if withs != nil {
|
||||
query.Withs = withs
|
||||
}
|
||||
|
||||
return API{
|
||||
Name: "search",
|
||||
Guard: "bearer-jwt",
|
||||
Process: fmt.Sprintf("models.%s.Paginate", model.Name),
|
||||
Default: []interface{}{query, 1, 20},
|
||||
}
|
||||
}
|
||||
|
||||
// apiFindDefault find 接口默认值
|
||||
func apiFindDefault(model *gou.Model, withs map[string]gou.With) API {
|
||||
|
||||
query := gou.QueryParam{}
|
||||
if withs != nil {
|
||||
query.Withs = withs
|
||||
}
|
||||
|
||||
return API{
|
||||
Name: "find",
|
||||
Guard: "bearer-jwt",
|
||||
Process: fmt.Sprintf("models.%s.Find", model.Name),
|
||||
Default: []interface{}{nil, query},
|
||||
}
|
||||
}
|
||||
|
||||
// apiFindDefault 接口默认值
|
||||
func apiDefault(model *gou.Model, name string, process string) API {
|
||||
return API{
|
||||
Name: name,
|
||||
Guard: "bearer-jwt",
|
||||
Process: fmt.Sprintf("models.%s.%s", model.Name, process),
|
||||
}
|
||||
}
|
||||
|
||||
// apiFindDefault 接口默认值
|
||||
func apiDefaultWhere(model *gou.Model, withs map[string]gou.With, name string, process string) API {
|
||||
|
||||
query := gou.QueryParam{}
|
||||
if withs != nil {
|
||||
query.Withs = withs
|
||||
}
|
||||
|
||||
return API{
|
||||
Name: name,
|
||||
Guard: "bearer-jwt",
|
||||
Process: fmt.Sprintf("models.%s.%s", model.Name, process),
|
||||
Default: []interface{}{query},
|
||||
}
|
||||
}
|
||||
|
||||
// apiDefaultSetting 接口默认值
|
||||
func apiDefaultSetting(table *Table) API {
|
||||
return API{
|
||||
Name: "setting",
|
||||
Guard: "bearer-jwt",
|
||||
Process: fmt.Sprintf("tables.%s.Setting", table.Table),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ type Table struct {
|
|||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Bind Bind `json:"bind,omitempty"`
|
||||
APIs APIs `json:"apis,omitempty"`
|
||||
APIs map[string]API `json:"apis,omitempty"`
|
||||
Columns map[string]Column `json:"columns,omitempty"`
|
||||
Filters map[string]Filter `json:"filters,omitempty"`
|
||||
List Page `json:"list,omitempty"`
|
||||
|
|
@ -24,28 +24,10 @@ type Bind struct {
|
|||
Withs map[string]gou.With `json:"withs,omitempty"`
|
||||
}
|
||||
|
||||
// APIs API 配置数据结构
|
||||
type APIs struct {
|
||||
Search API `json:"search,omitempty"`
|
||||
Find API `json:"find,omitempty"`
|
||||
Save API `json:"save,omitempty"`
|
||||
Delete API `json:"delete,omitempty"`
|
||||
Insert API `json:"insert,omitempty"`
|
||||
DeleteWhere API `json:"delete-where,omitempty"`
|
||||
DeleteIn API `json:"delete-in,omitempty"`
|
||||
UpdateWhere API `json:"update-where,omitempty"`
|
||||
UpdateIn API `json:"update-in,omitempty"`
|
||||
ImportUpload API `json:"import-upload,omitempty"`
|
||||
ImportPreview API `json:"import-preview,omitempty"`
|
||||
ImportSync API `json:"import-sync,omitempty"`
|
||||
ImportAsync API `json:"import-async,omitempty"`
|
||||
ImportAsyncTasks API `json:"import-async-tasks,omitempty"`
|
||||
ImportAsyncTasksID API `json:"import-async-tasks-id,omitempty"`
|
||||
Setting API `json:"setting,omitempty"`
|
||||
}
|
||||
|
||||
// API API 配置数据结构
|
||||
type API struct {
|
||||
Name string `json:"-"`
|
||||
Source string `json:"-"`
|
||||
Process string `json:"process,omitempty"`
|
||||
Guard string `json:"guard,omitempty"`
|
||||
Default []interface{} `json:"default,omitempty"`
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue