|
|
@@ -1,4 +1,4 @@
|
|
|
-package gwtools
|
|
|
+package entity_crud
|
|
|
|
|
|
import (
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/api"
|
|
|
@@ -9,27 +9,27 @@ import (
|
|
|
"net/http"
|
|
|
)
|
|
|
|
|
|
-func EntityCRUD(builder *gateway.Builder, params *EntityCRUDParams, opts ...any) {
|
|
|
- globalOptions := new(EntityCRUDGlobalOptions)
|
|
|
- createOptions := new(EntityCRUDCreateOptions)
|
|
|
- deleteOptions := new(EntityCRUDDeleteOptions)
|
|
|
- updateOptions := new(EntityCRUDUpdateOptions)
|
|
|
- queryOptions := new(EntityCRUDQueryOptions)
|
|
|
- getByIDOptions := new(EntityCRUDGetByIDOptions)
|
|
|
+func BindSimple(builder *gateway.Builder, params *Simple, opts ...any) {
|
|
|
+ globalOptions := new(GlobalOptions)
|
|
|
+ createOptions := new(CreateOptions)
|
|
|
+ deleteOptions := new(DeleteOptions)
|
|
|
+ updateOptions := new(UpdateOptions)
|
|
|
+ queryOptions := new(QueryOptions)
|
|
|
+ getByIDOptions := new(GetByIDOptions)
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
switch o := opt.(type) {
|
|
|
- case EntityCRUDGlobalOption:
|
|
|
+ case GlobalOption:
|
|
|
o(globalOptions)
|
|
|
- case EntityCRUDCreateOption:
|
|
|
+ case CreateOption:
|
|
|
o(createOptions)
|
|
|
- case EntityCRUDDeleteOption:
|
|
|
+ case DeleteOption:
|
|
|
o(deleteOptions)
|
|
|
- case EntityCRUDUpdateOption:
|
|
|
+ case UpdateOption:
|
|
|
o(updateOptions)
|
|
|
- case EntityCRUDQueryOption:
|
|
|
+ case QueryOption:
|
|
|
o(queryOptions)
|
|
|
- case EntityCRUDGetByIDOption:
|
|
|
+ case GetByIDOption:
|
|
|
o(getByIDOptions)
|
|
|
default:
|
|
|
continue
|
|
|
@@ -43,10 +43,10 @@ func EntityCRUD(builder *gateway.Builder, params *EntityCRUDParams, opts ...any)
|
|
|
params.queryOptions = queryOptions
|
|
|
params.getByIDOptions = getByIDOptions
|
|
|
|
|
|
- params.crud(builder)
|
|
|
+ params.bind(builder)
|
|
|
}
|
|
|
|
|
|
-type EntityCRUDParams struct {
|
|
|
+type Simple struct {
|
|
|
// 除去后缀的服务URL,如http://localhost:8080/example/api/v1
|
|
|
ServiceVersionedUrl string
|
|
|
|
|
|
@@ -54,23 +54,23 @@ type EntityCRUDParams struct {
|
|
|
DomainCamelName string
|
|
|
|
|
|
// 可选配置项,通过WithXXX配置
|
|
|
- globalOptions *EntityCRUDGlobalOptions
|
|
|
- createOptions *EntityCRUDCreateOptions
|
|
|
- deleteOptions *EntityCRUDDeleteOptions
|
|
|
- updateOptions *EntityCRUDUpdateOptions
|
|
|
- queryOptions *EntityCRUDQueryOptions
|
|
|
- getByIDOptions *EntityCRUDGetByIDOptions
|
|
|
+ globalOptions *GlobalOptions
|
|
|
+ createOptions *CreateOptions
|
|
|
+ deleteOptions *DeleteOptions
|
|
|
+ updateOptions *UpdateOptions
|
|
|
+ queryOptions *QueryOptions
|
|
|
+ getByIDOptions *GetByIDOptions
|
|
|
}
|
|
|
|
|
|
-func (params *EntityCRUDParams) crud(builder *gateway.Builder) {
|
|
|
- globalOptions := params.globalOptions
|
|
|
- createOptions := params.createOptions
|
|
|
- deleteOptions := params.deleteOptions
|
|
|
- updateOptions := params.updateOptions
|
|
|
- queryOptions := params.queryOptions
|
|
|
- getByIDOptions := params.getByIDOptions
|
|
|
+func (simple *Simple) bind(builder *gateway.Builder) {
|
|
|
+ globalOptions := simple.globalOptions
|
|
|
+ createOptions := simple.createOptions
|
|
|
+ deleteOptions := simple.deleteOptions
|
|
|
+ updateOptions := simple.updateOptions
|
|
|
+ queryOptions := simple.queryOptions
|
|
|
+ getByIDOptions := simple.getByIDOptions
|
|
|
|
|
|
- domainPath := "/" + strcase.ToLowerCamel(template.Id(params.DomainCamelName))
|
|
|
+ domainPath := "/" + strcase.ToLowerCamel(template.Id(simple.DomainCamelName))
|
|
|
|
|
|
// 创建
|
|
|
if !createOptions.disable {
|
|
|
@@ -92,7 +92,7 @@ func (params *EntityCRUDParams) crud(builder *gateway.Builder) {
|
|
|
}
|
|
|
|
|
|
copyBuilder.
|
|
|
- Post(gateway.NewPostRequest(params.ServiceVersionedUrl+domainPath+"/create",
|
|
|
+ Post(gateway.NewPostRequest(simple.ServiceVersionedUrl+domainPath+"/create",
|
|
|
gateway.PostRequestWithBodyForm(func(c *api.Context, historyRequests []gateway.BuilderRequest, resultMap map[string]any) (any, error) {
|
|
|
_, err := gateway.FormJsonBodyWithTenantIDAndUserIDFunc("tenantId", "createUserId")(c, historyRequests, resultMap)
|
|
|
if err != nil {
|
|
|
@@ -152,7 +152,7 @@ func (params *EntityCRUDParams) crud(builder *gateway.Builder) {
|
|
|
}
|
|
|
|
|
|
copyBuilder.
|
|
|
- Delete(gateway.NewDeleteRequest(params.ServiceVersionedUrl+domainPath+"/delete", queryParamsFormOption), deleteRequestCallback).
|
|
|
+ Delete(gateway.NewDeleteRequest(simple.ServiceVersionedUrl+domainPath+"/delete", queryParamsFormOption), deleteRequestCallback).
|
|
|
Build(deleteMiddlewares...)
|
|
|
}
|
|
|
|
|
|
@@ -175,7 +175,7 @@ func (params *EntityCRUDParams) crud(builder *gateway.Builder) {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- copyBuilder.Put(gateway.NewPutRequest(params.ServiceVersionedUrl+domainPath+"/update",
|
|
|
+ copyBuilder.Put(gateway.NewPutRequest(simple.ServiceVersionedUrl+domainPath+"/update",
|
|
|
gateway.PutRequestWithBodyForm(func(c *api.Context, historyRequests []gateway.BuilderRequest, resultMap map[string]any) (any, error) {
|
|
|
_, err := gateway.FormJsonBodyWithTenantIDAndUserIDFunc("", "updateUserId")(c, historyRequests, resultMap)
|
|
|
if err != nil {
|
|
|
@@ -220,7 +220,7 @@ func (params *EntityCRUDParams) crud(builder *gateway.Builder) {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- copyBuilder.Get(gateway.NewGetRequest(params.ServiceVersionedUrl+domainPath+"/query",
|
|
|
+ copyBuilder.Get(gateway.NewGetRequest(simple.ServiceVersionedUrl+domainPath+"/query",
|
|
|
gateway.GetRequestWithQueryParamsForm(func(c *api.Context, historyRequests []gateway.BuilderRequest, resultMap map[string]any) (map[string]string, error) {
|
|
|
_, err := gateway.FormQueryParamsWithTenantIDAndUserIDFunc("tenantId", "")(c, historyRequests, resultMap)
|
|
|
if err != nil {
|
|
|
@@ -274,210 +274,210 @@ func (params *EntityCRUDParams) crud(builder *gateway.Builder) {
|
|
|
}
|
|
|
|
|
|
copyBuilder.
|
|
|
- Get(gateway.NewGetRequest(params.ServiceVersionedUrl+domainPath+"/get", formQueryParamsOption), getRequestCallback).
|
|
|
+ Get(gateway.NewGetRequest(simple.ServiceVersionedUrl+domainPath+"/get", formQueryParamsOption), getRequestCallback).
|
|
|
Build(getByIDMiddlewares...)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-type EntityCRUDBuilderCallback func(builder *gateway.Builder, c *api.Context, historyRequests []gateway.BuilderRequest, resultMap map[string]any) (*gateway.Builder, error)
|
|
|
+type BuilderCallback func(builder *gateway.Builder, c *api.Context, historyRequests []gateway.BuilderRequest, resultMap map[string]any) (*gateway.Builder, error)
|
|
|
|
|
|
-type EntityCRUDGlobalOption func(options *EntityCRUDGlobalOptions)
|
|
|
-type EntityCRUDCreateOption func(options *EntityCRUDCreateOptions)
|
|
|
-type EntityCRUDDeleteOption func(options *EntityCRUDDeleteOptions)
|
|
|
-type EntityCRUDUpdateOption func(options *EntityCRUDUpdateOptions)
|
|
|
-type EntityCRUDQueryOption func(options *EntityCRUDQueryOptions)
|
|
|
-type EntityCRUDGetByIDOption func(options *EntityCRUDGetByIDOptions)
|
|
|
+type GlobalOption func(options *GlobalOptions)
|
|
|
+type CreateOption func(options *CreateOptions)
|
|
|
+type DeleteOption func(options *DeleteOptions)
|
|
|
+type UpdateOption func(options *UpdateOptions)
|
|
|
+type QueryOption func(options *QueryOptions)
|
|
|
+type GetByIDOption func(options *GetByIDOptions)
|
|
|
|
|
|
-type EntityCRUDGlobalOptions struct {
|
|
|
+type GlobalOptions struct {
|
|
|
middlewares []api.Handler
|
|
|
}
|
|
|
|
|
|
-type EntityCRUDCreateOptions struct {
|
|
|
+type CreateOptions struct {
|
|
|
// 关闭创建
|
|
|
disable bool
|
|
|
|
|
|
// 创建请求前回调
|
|
|
- beforeBuilderCallback EntityCRUDBuilderCallback
|
|
|
+ beforeBuilderCallback BuilderCallback
|
|
|
|
|
|
// 创建请求后回调
|
|
|
- afterBuilderCallback EntityCRUDBuilderCallback
|
|
|
+ afterBuilderCallback BuilderCallback
|
|
|
|
|
|
// 创建中间件
|
|
|
middlewares []api.Handler
|
|
|
}
|
|
|
|
|
|
-type EntityCRUDDeleteOptions struct {
|
|
|
+type DeleteOptions struct {
|
|
|
// 关闭删除
|
|
|
disable bool
|
|
|
|
|
|
// 删除请求前回调
|
|
|
- beforeBuilderCallback EntityCRUDBuilderCallback
|
|
|
+ beforeBuilderCallback BuilderCallback
|
|
|
|
|
|
// 删除请求后回调
|
|
|
- afterBuilderCallback EntityCRUDBuilderCallback
|
|
|
+ afterBuilderCallback BuilderCallback
|
|
|
|
|
|
// 删除中间件
|
|
|
middlewares []api.Handler
|
|
|
}
|
|
|
|
|
|
-type EntityCRUDUpdateOptions struct {
|
|
|
+type UpdateOptions struct {
|
|
|
// 关闭更新
|
|
|
disable bool
|
|
|
|
|
|
// 更新请求前回调
|
|
|
- beforeBuilderCallback EntityCRUDBuilderCallback
|
|
|
+ beforeBuilderCallback BuilderCallback
|
|
|
|
|
|
// 更新请求后回调
|
|
|
- afterBuilderCallback EntityCRUDBuilderCallback
|
|
|
+ afterBuilderCallback BuilderCallback
|
|
|
|
|
|
// 更新中间件
|
|
|
middlewares []api.Handler
|
|
|
}
|
|
|
|
|
|
-type EntityCRUDQueryOptions struct {
|
|
|
+type QueryOptions struct {
|
|
|
// 关闭查询
|
|
|
disable bool
|
|
|
|
|
|
// 查询请求前回调
|
|
|
- beforeBuilderCallback EntityCRUDBuilderCallback
|
|
|
+ beforeBuilderCallback BuilderCallback
|
|
|
|
|
|
// 查询请求后回调
|
|
|
- afterBuilderCallback EntityCRUDBuilderCallback
|
|
|
+ afterBuilderCallback BuilderCallback
|
|
|
|
|
|
// 查询中间件
|
|
|
middlewares []api.Handler
|
|
|
}
|
|
|
|
|
|
-type EntityCRUDGetByIDOptions struct {
|
|
|
+type GetByIDOptions struct {
|
|
|
// 关闭根据ID查询
|
|
|
disable bool
|
|
|
|
|
|
// 根据ID请求前回调
|
|
|
- beforeBuilderCallback EntityCRUDBuilderCallback
|
|
|
+ beforeBuilderCallback BuilderCallback
|
|
|
|
|
|
// 根据ID请求后回调
|
|
|
- afterBuilderCallback EntityCRUDBuilderCallback
|
|
|
+ afterBuilderCallback BuilderCallback
|
|
|
|
|
|
// 根据ID查询中间件
|
|
|
middlewares []api.Handler
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDGlobalMiddlewares(middlewares ...api.Handler) EntityCRUDGlobalOption {
|
|
|
- return func(options *EntityCRUDGlobalOptions) {
|
|
|
+func WithGlobalMiddlewares(middlewares ...api.Handler) GlobalOption {
|
|
|
+ return func(options *GlobalOptions) {
|
|
|
options.middlewares = middlewares
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDBeforeCreateBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDCreateOption {
|
|
|
- return func(options *EntityCRUDCreateOptions) {
|
|
|
+func WithBeforeCreateBuilderCallback(callbacks BuilderCallback) CreateOption {
|
|
|
+ return func(options *CreateOptions) {
|
|
|
options.beforeBuilderCallback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDAfterCreateBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDCreateOption {
|
|
|
- return func(options *EntityCRUDCreateOptions) {
|
|
|
+func WithAfterCreateBuilderCallback(callbacks BuilderCallback) CreateOption {
|
|
|
+ return func(options *CreateOptions) {
|
|
|
options.afterBuilderCallback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDCreateMiddlewares(middlewares []api.Handler) EntityCRUDCreateOption {
|
|
|
- return func(options *EntityCRUDCreateOptions) {
|
|
|
+func WithCreateMiddlewares(middlewares []api.Handler) CreateOption {
|
|
|
+ return func(options *CreateOptions) {
|
|
|
options.middlewares = middlewares
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDDisableDelete() EntityCRUDDeleteOption {
|
|
|
- return func(options *EntityCRUDDeleteOptions) {
|
|
|
+func WithDisableDelete() DeleteOption {
|
|
|
+ return func(options *DeleteOptions) {
|
|
|
options.disable = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDBeforeDeleteBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDDeleteOption {
|
|
|
- return func(options *EntityCRUDDeleteOptions) {
|
|
|
+func WithBeforeDeleteBuilderCallback(callbacks BuilderCallback) DeleteOption {
|
|
|
+ return func(options *DeleteOptions) {
|
|
|
options.beforeBuilderCallback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDAfterDeleteBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDDeleteOption {
|
|
|
- return func(options *EntityCRUDDeleteOptions) {
|
|
|
+func WithAfterDeleteBuilderCallback(callbacks BuilderCallback) DeleteOption {
|
|
|
+ return func(options *DeleteOptions) {
|
|
|
options.afterBuilderCallback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDDeleteMiddlewares(middlewares []api.Handler) EntityCRUDDeleteOption {
|
|
|
- return func(options *EntityCRUDDeleteOptions) {
|
|
|
+func WithDeleteMiddlewares(middlewares []api.Handler) DeleteOption {
|
|
|
+ return func(options *DeleteOptions) {
|
|
|
options.middlewares = middlewares
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDDisableUpdate() EntityCRUDUpdateOption {
|
|
|
- return func(options *EntityCRUDUpdateOptions) {
|
|
|
+func WithDisableUpdate() UpdateOption {
|
|
|
+ return func(options *UpdateOptions) {
|
|
|
options.disable = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDBeforeUpdateBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDUpdateOption {
|
|
|
- return func(options *EntityCRUDUpdateOptions) {
|
|
|
+func WithBeforeUpdateBuilderCallback(callbacks BuilderCallback) UpdateOption {
|
|
|
+ return func(options *UpdateOptions) {
|
|
|
options.beforeBuilderCallback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDAfterUpdateBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDUpdateOption {
|
|
|
- return func(options *EntityCRUDUpdateOptions) {
|
|
|
+func WithAfterUpdateBuilderCallback(callbacks BuilderCallback) UpdateOption {
|
|
|
+ return func(options *UpdateOptions) {
|
|
|
options.afterBuilderCallback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDUpdateMiddlewares(middlewares []api.Handler) EntityCRUDUpdateOption {
|
|
|
- return func(options *EntityCRUDUpdateOptions) {
|
|
|
+func WithUpdateMiddlewares(middlewares []api.Handler) UpdateOption {
|
|
|
+ return func(options *UpdateOptions) {
|
|
|
options.middlewares = middlewares
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDDisableQuery() EntityCRUDQueryOption {
|
|
|
- return func(options *EntityCRUDQueryOptions) {
|
|
|
+func WithDisableQuery() QueryOption {
|
|
|
+ return func(options *QueryOptions) {
|
|
|
options.disable = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDBeforeQueryBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDQueryOption {
|
|
|
- return func(options *EntityCRUDQueryOptions) {
|
|
|
+func WithBeforeQueryBuilderCallback(callbacks BuilderCallback) QueryOption {
|
|
|
+ return func(options *QueryOptions) {
|
|
|
options.beforeBuilderCallback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDAfterQueryBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDQueryOption {
|
|
|
- return func(options *EntityCRUDQueryOptions) {
|
|
|
+func WithAfterQueryBuilderCallback(callbacks BuilderCallback) QueryOption {
|
|
|
+ return func(options *QueryOptions) {
|
|
|
options.afterBuilderCallback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDQueryMiddlewares(middlewares []api.Handler) EntityCRUDQueryOption {
|
|
|
- return func(options *EntityCRUDQueryOptions) {
|
|
|
+func WithQueryMiddlewares(middlewares []api.Handler) QueryOption {
|
|
|
+ return func(options *QueryOptions) {
|
|
|
options.middlewares = middlewares
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDDisableGetByID() EntityCRUDGetByIDOption {
|
|
|
- return func(options *EntityCRUDGetByIDOptions) {
|
|
|
+func WithDisableGetByID() GetByIDOption {
|
|
|
+ return func(options *GetByIDOptions) {
|
|
|
options.disable = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDBeforeGetByIDBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDGetByIDOption {
|
|
|
- return func(options *EntityCRUDGetByIDOptions) {
|
|
|
+func WithBeforeGetByIDBuilderCallback(callbacks BuilderCallback) GetByIDOption {
|
|
|
+ return func(options *GetByIDOptions) {
|
|
|
options.beforeBuilderCallback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDAfterGetByIDBuilderCallback(callbacks EntityCRUDBuilderCallback) EntityCRUDGetByIDOption {
|
|
|
- return func(options *EntityCRUDGetByIDOptions) {
|
|
|
+func WithAfterGetByIDBuilderCallback(callbacks BuilderCallback) GetByIDOption {
|
|
|
+ return func(options *GetByIDOptions) {
|
|
|
options.afterBuilderCallback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func WithEntityCRUDGetByIDMiddlewares(middlewares []api.Handler) EntityCRUDGetByIDOption {
|
|
|
- return func(options *EntityCRUDGetByIDOptions) {
|
|
|
+func WithGetByIDMiddlewares(middlewares []api.Handler) GetByIDOption {
|
|
|
+ return func(options *GetByIDOptions) {
|
|
|
options.middlewares = middlewares
|
|
|
}
|
|
|
}
|