|
|
@@ -1,13 +1,18 @@
|
|
|
package gwtools
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/api"
|
|
|
"git.sxidc.com/go-framework/baize/framework/gateway"
|
|
|
"git.sxidc.com/go-tools/utils/template"
|
|
|
+ "git.sxidc.com/service-supports/fserr"
|
|
|
"github.com/iancoleman/strcase"
|
|
|
"net/http"
|
|
|
)
|
|
|
|
|
|
+type GetTenantIDFunc func(c *api.Context) (string, error)
|
|
|
+type GetUserIDFunc func(c *api.Context) (string, error)
|
|
|
+
|
|
|
func CRUD(builder *gateway.Builder, params *CRUDParams, opts ...any) {
|
|
|
createOptions := new(CreateOptions)
|
|
|
deleteOptions := new(DeleteOptions)
|
|
|
@@ -48,12 +53,6 @@ type CRUDParams struct {
|
|
|
// 领域名称
|
|
|
DomainCamelName string
|
|
|
|
|
|
- CreateMiddlewares []api.Handler
|
|
|
- DeleteMiddlewares []api.Handler
|
|
|
- UpdateMiddlewares []api.Handler
|
|
|
- QueryMiddlewares []api.Handler
|
|
|
- GetByIDMiddlewares []api.Handler
|
|
|
-
|
|
|
// 可选配置项,通过WithXXX配置
|
|
|
createOptions *CreateOptions
|
|
|
deleteOptions *DeleteOptions
|
|
|
@@ -72,53 +71,59 @@ func (params *CRUDParams) crud(builder *gateway.Builder) {
|
|
|
domainPath := "/" + strcase.ToLowerCamel(template.Id(params.DomainCamelName))
|
|
|
|
|
|
// 创建
|
|
|
- if !createOptions.disableCreate {
|
|
|
+ if !createOptions.disable {
|
|
|
builder.
|
|
|
Url(http.MethodPost, domainPath+"/create").
|
|
|
Post(&gateway.PostRequest{
|
|
|
Url: params.ServiceVersionedUrl + domainPath + "/create",
|
|
|
- }, createOptions.createCallback).
|
|
|
- Build(createOptions.createMiddlewares...)
|
|
|
+ Body: addBodyTenantIDAndUserID("tenantId", "createUserId",
|
|
|
+ params.createOptions.getTenantIDFunc, params.createOptions.getUserIDFunc),
|
|
|
+ }, createOptions.callback).
|
|
|
+ Build(createOptions.middlewares...)
|
|
|
}
|
|
|
|
|
|
// 删除
|
|
|
- if !deleteOptions.disableDelete {
|
|
|
+ if !deleteOptions.disable {
|
|
|
builder.
|
|
|
Url(http.MethodDelete, domainPath+"/delete").
|
|
|
Delete(&gateway.DeleteRequest{
|
|
|
Url: params.ServiceVersionedUrl + domainPath + "/delete",
|
|
|
- }, deleteOptions.deleteCallback).
|
|
|
- Build(deleteOptions.deleteMiddlewares...)
|
|
|
+ }, deleteOptions.callback).
|
|
|
+ Build(deleteOptions.middlewares...)
|
|
|
}
|
|
|
|
|
|
// 修改
|
|
|
- if !updateOptions.disableUpdate {
|
|
|
+ if !updateOptions.disable {
|
|
|
builder.
|
|
|
Url(http.MethodPut, domainPath+"/update").
|
|
|
Put(&gateway.PutRequest{
|
|
|
Url: params.ServiceVersionedUrl + domainPath + "/update",
|
|
|
- }, updateOptions.updateCallback).
|
|
|
- Build(updateOptions.updateMiddlewares...)
|
|
|
+ Body: addBodyTenantIDAndUserID("", "updateUserId",
|
|
|
+ nil, params.updateOptions.getUserIDFunc),
|
|
|
+ }, updateOptions.callback).
|
|
|
+ Build(updateOptions.middlewares...)
|
|
|
}
|
|
|
|
|
|
// 查询
|
|
|
- if !queryOptions.disableQuery {
|
|
|
+ if !queryOptions.disable {
|
|
|
builder.
|
|
|
Url(http.MethodGet, domainPath+"/query").
|
|
|
Get(&gateway.GetRequest{
|
|
|
Url: params.ServiceVersionedUrl + domainPath + "/query",
|
|
|
- }, queryOptions.queryCallback).
|
|
|
- Build(queryOptions.queryMiddlewares...)
|
|
|
+ QueryParams: addQueryParamsTenantIDAndUserID("tenantId", "",
|
|
|
+ params.queryOptions.getTenantIDFunc, nil),
|
|
|
+ }, queryOptions.callback).
|
|
|
+ Build(queryOptions.middlewares...)
|
|
|
}
|
|
|
|
|
|
// 通过ID获取
|
|
|
- if !getByIDOptions.disableGetByID {
|
|
|
+ if !getByIDOptions.disable {
|
|
|
builder.
|
|
|
Url(http.MethodGet, domainPath+"/get").
|
|
|
Get(&gateway.GetRequest{
|
|
|
Url: params.ServiceVersionedUrl + domainPath + "/get",
|
|
|
- }, getByIDOptions.getByIDCallback).
|
|
|
- Build(getByIDOptions.getByIDMiddlewares...)
|
|
|
+ }, getByIDOptions.callback).
|
|
|
+ Build(getByIDOptions.middlewares...)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -130,139 +135,276 @@ type GetByIDOption func(options *GetByIDOptions)
|
|
|
|
|
|
type CreateOptions struct {
|
|
|
// 关闭创建
|
|
|
- disableCreate bool
|
|
|
+ disable bool
|
|
|
|
|
|
// 创建回调
|
|
|
- createCallback gateway.RequestCallbackFunc
|
|
|
+ callback gateway.RequestCallbackFunc
|
|
|
|
|
|
// 创建中间件
|
|
|
- createMiddlewares []api.Handler
|
|
|
+ middlewares []api.Handler
|
|
|
+
|
|
|
+ // 获取租户ID的接口
|
|
|
+ getTenantIDFunc GetTenantIDFunc
|
|
|
+
|
|
|
+ // 获取用户ID的接口
|
|
|
+ getUserIDFunc GetUserIDFunc
|
|
|
}
|
|
|
|
|
|
type DeleteOptions struct {
|
|
|
// 关闭删除
|
|
|
- disableDelete bool
|
|
|
+ disable bool
|
|
|
|
|
|
// 删除回调
|
|
|
- deleteCallback gateway.RequestCallbackFunc
|
|
|
+ callback gateway.RequestCallbackFunc
|
|
|
|
|
|
// 删除中间件
|
|
|
- deleteMiddlewares []api.Handler
|
|
|
+ middlewares []api.Handler
|
|
|
}
|
|
|
|
|
|
type UpdateOptions struct {
|
|
|
// 关闭更新
|
|
|
- disableUpdate bool
|
|
|
+ disable bool
|
|
|
|
|
|
// 更新回调
|
|
|
- updateCallback gateway.RequestCallbackFunc
|
|
|
+ callback gateway.RequestCallbackFunc
|
|
|
|
|
|
// 更新中间件
|
|
|
- updateMiddlewares []api.Handler
|
|
|
+ middlewares []api.Handler
|
|
|
+
|
|
|
+ // 获取用户ID的接口
|
|
|
+ getUserIDFunc GetUserIDFunc
|
|
|
}
|
|
|
|
|
|
type QueryOptions struct {
|
|
|
// 关闭查询
|
|
|
- disableQuery bool
|
|
|
+ disable bool
|
|
|
|
|
|
// 查询回调
|
|
|
- queryCallback gateway.RequestCallbackFunc
|
|
|
+ callback gateway.RequestCallbackFunc
|
|
|
|
|
|
// 查询中间件
|
|
|
- queryMiddlewares []api.Handler
|
|
|
+ middlewares []api.Handler
|
|
|
+
|
|
|
+ // 获取租户ID的接口
|
|
|
+ getTenantIDFunc GetTenantIDFunc
|
|
|
}
|
|
|
|
|
|
type GetByIDOptions struct {
|
|
|
// 关闭根据ID查询
|
|
|
- disableGetByID bool
|
|
|
+ disable bool
|
|
|
|
|
|
// 根据ID查询回调
|
|
|
- getByIDCallback gateway.RequestCallbackFunc
|
|
|
+ callback gateway.RequestCallbackFunc
|
|
|
|
|
|
// 根据ID查询中间件
|
|
|
- getByIDMiddlewares []api.Handler
|
|
|
+ middlewares []api.Handler
|
|
|
}
|
|
|
|
|
|
func WithCreateCallbacks(callbacks gateway.RequestCallbackFunc) CreateOption {
|
|
|
return func(options *CreateOptions) {
|
|
|
- options.createCallback = callbacks
|
|
|
+ options.callback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithCreateMiddlewares(middlewares []api.Handler) CreateOption {
|
|
|
return func(options *CreateOptions) {
|
|
|
- options.createMiddlewares = middlewares
|
|
|
+ options.middlewares = middlewares
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithCreateGetTenantIDFunc(getTenantIDFunc GetTenantIDFunc) CreateOption {
|
|
|
+ return func(options *CreateOptions) {
|
|
|
+ options.getTenantIDFunc = getTenantIDFunc
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithCreateGetUserIDFunc(getUserIDFunc GetUserIDFunc) CreateOption {
|
|
|
+ return func(options *CreateOptions) {
|
|
|
+ options.getUserIDFunc = getUserIDFunc
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithDisableDelete() DeleteOption {
|
|
|
return func(options *DeleteOptions) {
|
|
|
- options.disableDelete = true
|
|
|
+ options.disable = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithDeleteCallbacks(callbacks gateway.RequestCallbackFunc) DeleteOption {
|
|
|
return func(options *DeleteOptions) {
|
|
|
- options.deleteCallback = callbacks
|
|
|
+ options.callback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithDeleteMiddlewares(middlewares []api.Handler) DeleteOption {
|
|
|
return func(options *DeleteOptions) {
|
|
|
- options.deleteMiddlewares = middlewares
|
|
|
+ options.middlewares = middlewares
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithDisableUpdate() UpdateOption {
|
|
|
return func(options *UpdateOptions) {
|
|
|
- options.disableUpdate = true
|
|
|
+ options.disable = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithUpdateCallbacks(callbacks gateway.RequestCallbackFunc) UpdateOption {
|
|
|
return func(options *UpdateOptions) {
|
|
|
- options.updateCallback = callbacks
|
|
|
+ options.callback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithUpdateMiddlewares(middlewares []api.Handler) UpdateOption {
|
|
|
return func(options *UpdateOptions) {
|
|
|
- options.updateMiddlewares = middlewares
|
|
|
+ options.middlewares = middlewares
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithUpdateGetUserIDFunc(getUserIDFunc GetUserIDFunc) UpdateOption {
|
|
|
+ return func(options *UpdateOptions) {
|
|
|
+ options.getUserIDFunc = getUserIDFunc
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithDisableQuery() QueryOption {
|
|
|
return func(options *QueryOptions) {
|
|
|
- options.disableQuery = true
|
|
|
+ options.disable = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithQueryCallbacks(callbacks gateway.RequestCallbackFunc) QueryOption {
|
|
|
return func(options *QueryOptions) {
|
|
|
- options.queryCallback = callbacks
|
|
|
+ options.callback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithQueryMiddlewares(middlewares []api.Handler) QueryOption {
|
|
|
return func(options *QueryOptions) {
|
|
|
- options.queryMiddlewares = middlewares
|
|
|
+ options.middlewares = middlewares
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithQueryGetTenantIDFunc(getTenantIDFunc GetTenantIDFunc) QueryOption {
|
|
|
+ return func(options *QueryOptions) {
|
|
|
+ options.getTenantIDFunc = getTenantIDFunc
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithDisableGetByID() GetByIDOption {
|
|
|
return func(options *GetByIDOptions) {
|
|
|
- options.disableGetByID = true
|
|
|
+ options.disable = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithGetByIDCallbacks(callbacks gateway.RequestCallbackFunc) GetByIDOption {
|
|
|
return func(options *GetByIDOptions) {
|
|
|
- options.getByIDCallback = callbacks
|
|
|
+ options.callback = callbacks
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithGetByIDMiddlewares(middlewares []api.Handler) GetByIDOption {
|
|
|
return func(options *GetByIDOptions) {
|
|
|
- options.getByIDMiddlewares = middlewares
|
|
|
+ options.middlewares = middlewares
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func addBodyTenantIDAndUserID(tenantIDFieldName string, userIDFieldName string,
|
|
|
+ getTenantIDFunc GetTenantIDFunc, getUserIDFunc GetUserIDFunc) gateway.FormBodyFunc {
|
|
|
+ return func(c *api.Context, historyRequests []gateway.BuilderRequest, customResultMap map[string]any) (any, error) {
|
|
|
+ body, err := gateway.DefaultFormBodyFunc(c, historyRequests, customResultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if getTenantIDFunc == nil && getUserIDFunc == nil {
|
|
|
+ return body, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ bodyBytes, ok := body.([]byte)
|
|
|
+ if !ok {
|
|
|
+ return nil, fserr.New("body不是json")
|
|
|
+ }
|
|
|
+
|
|
|
+ bodyMap := make(map[string]any)
|
|
|
+ err = json.Unmarshal(bodyBytes, &bodyMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if getTenantIDFunc != nil {
|
|
|
+ _, ok := bodyMap[tenantIDFieldName]
|
|
|
+ if !ok {
|
|
|
+ tenantID, err := getTenantIDFunc(c)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ bodyMap[tenantIDFieldName] = tenantID
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if getUserIDFunc != nil {
|
|
|
+ _, ok := bodyMap[userIDFieldName]
|
|
|
+ if !ok {
|
|
|
+ userID, err := getUserIDFunc(c)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ bodyMap[userIDFieldName] = userID
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ newBody, err := json.Marshal(bodyMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = c.ReplaceBody(newBody)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return newBody, nil
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func addQueryParamsTenantIDAndUserID(tenantIDFieldName string, userIDFieldName string,
|
|
|
+ getTenantIDFunc GetTenantIDFunc, getUserIDFunc GetUserIDFunc) gateway.FormQueryParamsFunc {
|
|
|
+ return func(c *api.Context, historyRequests []gateway.BuilderRequest, customResultMap map[string]any) (map[string]string, error) {
|
|
|
+ queryParams, err := gateway.DefaultFormQueryParamsFunc(c, historyRequests, customResultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if getTenantIDFunc == nil && getUserIDFunc == nil {
|
|
|
+ return queryParams, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if getTenantIDFunc != nil {
|
|
|
+ _, ok := queryParams[tenantIDFieldName]
|
|
|
+ if !ok {
|
|
|
+ tenantID, err := getTenantIDFunc(c)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ queryParams[tenantIDFieldName] = tenantID
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if getUserIDFunc != nil {
|
|
|
+ _, ok := queryParams[userIDFieldName]
|
|
|
+ if !ok {
|
|
|
+ userID, err := getUserIDFunc(c)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ queryParams[userIDFieldName] = userID
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return queryParams, nil
|
|
|
}
|
|
|
}
|