package value_object_crud import ( "git.sxidc.com/go-framework/baize/framework/gateway" "git.sxidc.com/go-tools/utils/http_client" "git.sxidc.com/go-tools/utils/template" "github.com/iancoleman/strcase" ) func BindSimple(builder *gateway.Builder, params *Simple, opts ...any) { globalOptions := new(GlobalOptions) createOptions := new(CreateOptions) deleteOptions := new(DeleteOptions) queryOptions := new(QueryOptions) for _, opt := range opts { switch o := opt.(type) { case GlobalOption: o(globalOptions) case CreateOption: o(createOptions) case DeleteOption: o(deleteOptions) case QueryOption: o(queryOptions) default: continue } } params.globalOptions = globalOptions params.createOptions = createOptions params.deleteOptions = deleteOptions params.queryOptions = queryOptions params.bind(builder) } type Simple struct { // 除去后缀的服务URL,如http://localhost:8080/example/api/v1 ServiceVersionedUrl string // 服务缩写 ServiceShortName string // 领域名称 DomainCamelName string // 可选配置项,通过WithXXX配置 globalOptions *GlobalOptions createOptions *CreateOptions deleteOptions *DeleteOptions queryOptions *QueryOptions } func (simple *Simple) bind(builder *gateway.Builder) { globalOptions := simple.globalOptions createOptions := simple.createOptions deleteOptions := simple.deleteOptions queryOptions := simple.queryOptions domainPath := "/" + strcase.ToLowerCamel(template.Id(simple.DomainCamelName)) // 创建 if !createOptions.disable { createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...) builder.PostRouteWithTenantIDAndCreateUserID("/"+simple.ServiceShortName+domainPath+"/create", func(requestBuilder *gateway.RequestBuilder) { if createOptions.responseSuccessCallback != nil { requestBuilder.ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) { err := createOptions.responseSuccessCallback(c) if err != nil { requestBuilder.ResponseError(err) return } }) } if createOptions.responseErrorCallback != nil { requestBuilder.ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) { retErr := createOptions.responseErrorCallback(c, err) if retErr != nil { requestBuilder.ResponseError(retErr) return } }) } requestBuilder. Post( &gateway.PostRequest{ Url: simple.ServiceVersionedUrl + domainPath + "/create", }, gateway.WithBeforeRequestCallback( func(c *gateway.RequestBuilderContext) error { if createOptions.beforeBuilderCallback != nil { err := createOptions.beforeBuilderCallback(c) if err != nil { return err } } return nil }), gateway.WithRequestResponseCallback( func(c *gateway.RequestBuilderContext, response *http_client.Response) error { if createOptions.afterBuilderCallback != nil { err := createOptions.afterBuilderCallback(c) if err != nil { return err } } return nil })). Request() }, createMiddlewares...) } // 删除 if !deleteOptions.disable { deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...) builder.PostRoute("/"+simple.ServiceShortName+domainPath+"/delete", func(requestBuilder *gateway.RequestBuilder) { if deleteOptions.responseSuccessCallback != nil { requestBuilder.ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) { err := deleteOptions.responseSuccessCallback(c) if err != nil { requestBuilder.ResponseError(err) return } }) } if deleteOptions.responseErrorCallback != nil { requestBuilder.ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) { retErr := deleteOptions.responseErrorCallback(c, err) if retErr != nil { requestBuilder.ResponseError(retErr) return } }) } requestBuilder. Post( &gateway.PostRequest{ Url: simple.ServiceVersionedUrl + domainPath + "/delete", }, gateway.WithBeforeRequestCallback( func(c *gateway.RequestBuilderContext) error { if deleteOptions.beforeBuilderCallback != nil { err := deleteOptions.beforeBuilderCallback(c) if err != nil { return err } } return nil }), gateway.WithRequestResponseCallback( func(c *gateway.RequestBuilderContext, response *http_client.Response) error { if deleteOptions.afterBuilderCallback != nil { err := deleteOptions.afterBuilderCallback(c) if err != nil { return err } } return nil })). Request() }, deleteMiddlewares...) } // 查询 if !queryOptions.disable { queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...) builder.GetRouteWithTenantID("/"+simple.ServiceShortName+domainPath+"/query", func(requestBuilder *gateway.RequestBuilder) { if queryOptions.responseSuccessCallback != nil { requestBuilder.ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) { err := queryOptions.responseSuccessCallback(c) if err != nil { requestBuilder.ResponseError(err) return } }) } if queryOptions.responseErrorCallback != nil { requestBuilder.ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) { retErr := queryOptions.responseErrorCallback(c, err) if retErr != nil { requestBuilder.ResponseError(retErr) return } }) } requestBuilder. Get( &gateway.GetRequest{ Url: simple.ServiceVersionedUrl + domainPath + "/query", }, gateway.WithBeforeRequestCallback( func(c *gateway.RequestBuilderContext) error { if queryOptions.beforeBuilderCallback != nil { err := queryOptions.beforeBuilderCallback(c) if err != nil { return err } } return nil }), gateway.WithRequestResponseCallback( func(c *gateway.RequestBuilderContext, response *http_client.Response) error { if queryOptions.afterBuilderCallback != nil { err := queryOptions.afterBuilderCallback(c) if err != nil { return err } } return nil })). Request() }, queryMiddlewares...) } } type RequestBuilderCallback func(c *gateway.RequestBuilderContext) error type RequestBuilderErrorCallback func(c *gateway.RequestBuilderContext, err error) error type GlobalOption func(options *GlobalOptions) type CreateOption func(options *CreateOptions) type DeleteOption func(options *DeleteOptions) type QueryOption func(options *QueryOptions) type GlobalOptions struct { middlewares []gateway.Handler } type CreateOptions struct { // 关闭创建 disable bool // 创建请求前回调 beforeBuilderCallback RequestBuilderCallback // 创建请求后回调 afterBuilderCallback RequestBuilderCallback // responseSuccessCallback 成功响应回调 responseSuccessCallback RequestBuilderCallback // responseErrorCallback 失败响应回调 responseErrorCallback RequestBuilderErrorCallback // 创建中间件 middlewares []gateway.Handler } type DeleteOptions struct { // 关闭删除 disable bool // 删除请求前回调 beforeBuilderCallback RequestBuilderCallback // 删除请求后回调 afterBuilderCallback RequestBuilderCallback // responseSuccessCallback 成功响应回调 responseSuccessCallback RequestBuilderCallback // responseErrorCallback 失败响应回调 responseErrorCallback RequestBuilderErrorCallback // 删除中间件 middlewares []gateway.Handler } type QueryOptions struct { // 关闭查询 disable bool // 查询请求前回调 beforeBuilderCallback RequestBuilderCallback // 查询请求后回调 afterBuilderCallback RequestBuilderCallback // responseSuccessCallback 成功响应回调 responseSuccessCallback RequestBuilderCallback // responseErrorCallback 失败响应回调 responseErrorCallback RequestBuilderErrorCallback // 查询中间件 middlewares []gateway.Handler } func WithGlobalMiddlewares(middlewares ...gateway.Handler) GlobalOption { return func(options *GlobalOptions) { options.middlewares = middlewares } } func WithBeforeCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOption { return func(options *CreateOptions) { options.beforeBuilderCallback = callbacks } } func WithAfterCreateBuilderCallback(callbacks RequestBuilderCallback) CreateOption { return func(options *CreateOptions) { options.afterBuilderCallback = callbacks } } func WithCreateResponseSuccessCallback(responseSuccessCallback RequestBuilderCallback) CreateOption { return func(options *CreateOptions) { options.responseSuccessCallback = responseSuccessCallback } } func WithCreateResponseErrorCallback(responseErrorCallback RequestBuilderErrorCallback) CreateOption { return func(options *CreateOptions) { options.responseErrorCallback = responseErrorCallback } } func WithCreateMiddlewares(middlewares ...gateway.Handler) CreateOption { return func(options *CreateOptions) { options.middlewares = middlewares } } func WithDisableDelete() DeleteOption { return func(options *DeleteOptions) { options.disable = true } } func WithBeforeDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOption { return func(options *DeleteOptions) { options.beforeBuilderCallback = callbacks } } func WithAfterDeleteBuilderCallback(callbacks RequestBuilderCallback) DeleteOption { return func(options *DeleteOptions) { options.afterBuilderCallback = callbacks } } func WithDeleteResponseSuccessCallback(responseSuccessCallback RequestBuilderCallback) DeleteOption { return func(options *DeleteOptions) { options.responseSuccessCallback = responseSuccessCallback } } func WithDeleteResponseErrorCallback(responseErrorCallback RequestBuilderErrorCallback) DeleteOption { return func(options *DeleteOptions) { options.responseErrorCallback = responseErrorCallback } } func WithDeleteMiddlewares(middlewares ...gateway.Handler) DeleteOption { return func(options *DeleteOptions) { options.middlewares = middlewares } } func WithDisableQuery() QueryOption { return func(options *QueryOptions) { options.disable = true } } func WithBeforeQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption { return func(options *QueryOptions) { options.beforeBuilderCallback = callbacks } } func WithAfterQueryBuilderCallback(callbacks RequestBuilderCallback) QueryOption { return func(options *QueryOptions) { options.afterBuilderCallback = callbacks } } func WithQueryResponseSuccessCallback(responseSuccessCallback RequestBuilderCallback) QueryOption { return func(options *QueryOptions) { options.responseSuccessCallback = responseSuccessCallback } } func WithQueryResponseErrorCallback(responseErrorCallback RequestBuilderErrorCallback) QueryOption { return func(options *QueryOptions) { options.responseErrorCallback = responseErrorCallback } } func WithQueryMiddlewares(middlewares ...gateway.Handler) QueryOption { return func(options *QueryOptions) { options.middlewares = middlewares } }