123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- package entity_crud
- import (
- "git.sxidc.com/go-framework/baize/framework/core/api/response"
- "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"
- "net/http"
- )
- 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 GlobalOption:
- o(globalOptions)
- case CreateOption:
- o(createOptions)
- case DeleteOption:
- o(deleteOptions)
- case UpdateOption:
- o(updateOptions)
- case QueryOption:
- o(queryOptions)
- case GetByIDOption:
- o(getByIDOptions)
- default:
- continue
- }
- }
- params.globalOptions = globalOptions
- params.createOptions = createOptions
- params.deleteOptions = deleteOptions
- params.updateOptions = updateOptions
- params.queryOptions = queryOptions
- params.getByIDOptions = getByIDOptions
- 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
- updateOptions *UpdateOptions
- queryOptions *QueryOptions
- getByIDOptions *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(simple.DomainCamelName))
- // 创建
- if !createOptions.disable {
- createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...)
- builder.PostRouteWithTenantIDAndCreateUserInfo("/"+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 {
- return createOptions.beforeBuilderCallback(c)
- }
- 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
- })).
- ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) {
- response.SendIDResponse(c.ApiContext(), http.StatusOK, "", err)
- }).
- Request()
- }, createMiddlewares...)
- }
- // 删除
- if !deleteOptions.disable {
- deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
- builder.DeleteRouteWithDeleteUserInfo("/"+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.
- Delete(
- &gateway.DeleteRequest{
- 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 !updateOptions.disable {
- updateMiddlewares := append(globalOptions.middlewares, updateOptions.middlewares...)
- builder.PutRouteWithUpdateUserInfo("/"+simple.ServiceShortName+domainPath+"/update",
- func(requestBuilder *gateway.RequestBuilder) {
- if updateOptions.responseSuccessCallback != nil {
- requestBuilder.ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) {
- err := updateOptions.responseSuccessCallback(c)
- if err != nil {
- requestBuilder.ResponseError(err)
- return
- }
- })
- }
- if updateOptions.responseErrorCallback != nil {
- requestBuilder.ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) {
- retErr := updateOptions.responseErrorCallback(c, err)
- if retErr != nil {
- requestBuilder.ResponseError(retErr)
- return
- }
- })
- }
- requestBuilder.
- Put(
- &gateway.PutRequest{
- Url: simple.ServiceVersionedUrl + domainPath + "/update",
- },
- gateway.WithBeforeRequestCallback(func(c *gateway.RequestBuilderContext) error {
- if updateOptions.beforeBuilderCallback != nil {
- err := updateOptions.beforeBuilderCallback(c)
- if err != nil {
- return err
- }
- }
- return nil
- }),
- gateway.WithRequestResponseCallback(
- func(c *gateway.RequestBuilderContext, response *http_client.Response) error {
- if updateOptions.afterBuilderCallback != nil {
- err := updateOptions.afterBuilderCallback(c)
- if err != nil {
- return err
- }
- }
- return nil
- })).
- Request()
- }, updateMiddlewares...)
- }
- // 查询
- 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...)
- }
- // 通过ID获取
- if !getByIDOptions.disable {
- getByIDMiddlewares := append(globalOptions.middlewares, getByIDOptions.middlewares...)
- builder.GetRoute("/"+simple.ServiceShortName+domainPath+"/get",
- func(requestBuilder *gateway.RequestBuilder) {
- if getByIDOptions.responseSuccessCallback != nil {
- requestBuilder.ResponseSuccessCallback(func(c *gateway.RequestBuilderContext) {
- err := getByIDOptions.responseSuccessCallback(c)
- if err != nil {
- requestBuilder.ResponseError(err)
- return
- }
- })
- }
- if getByIDOptions.responseErrorCallback != nil {
- requestBuilder.ResponseErrorCallback(func(c *gateway.RequestBuilderContext, err error) {
- retErr := getByIDOptions.responseErrorCallback(c, err)
- if retErr != nil {
- requestBuilder.ResponseError(retErr)
- return
- }
- })
- }
- requestBuilder.
- Get(
- &gateway.GetRequest{
- Url: simple.ServiceVersionedUrl + domainPath + "/get",
- },
- gateway.WithBeforeRequestCallback(
- func(c *gateway.RequestBuilderContext) error {
- if getByIDOptions.beforeBuilderCallback != nil {
- err := getByIDOptions.beforeBuilderCallback(c)
- if err != nil {
- return err
- }
- }
- return nil
- }),
- gateway.WithRequestResponseCallback(
- func(c *gateway.RequestBuilderContext, response *http_client.Response) error {
- if getByIDOptions.afterBuilderCallback != nil {
- err := getByIDOptions.afterBuilderCallback(c)
- if err != nil {
- return err
- }
- }
- return nil
- })).
- Request()
- }, getByIDMiddlewares...)
- }
- }
- 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 UpdateOption func(options *UpdateOptions)
- type QueryOption func(options *QueryOptions)
- type GetByIDOption func(options *GetByIDOptions)
- 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 UpdateOptions 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
- }
- type GetByIDOptions struct {
- // 关闭根据ID查询
- disable bool
- // 根据ID请求前回调
- beforeBuilderCallback RequestBuilderCallback
- // 根据ID请求后回调
- afterBuilderCallback RequestBuilderCallback
- // responseSuccessCallback 成功响应回调
- responseSuccessCallback RequestBuilderCallback
- // responseErrorCallback 错误响应回调
- responseErrorCallback RequestBuilderErrorCallback
- // 根据ID查询中间件
- middlewares []gateway.Handler
- }
- func WithGlobalMiddlewares(middlewares ...gateway.Handler) GlobalOption {
- return func(options *GlobalOptions) {
- options.middlewares = middlewares
- }
- }
- func WithDisableCreate() CreateOption {
- return func(options *CreateOptions) {
- options.disable = true
- }
- }
- 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 WithDisableUpdate() UpdateOption {
- return func(options *UpdateOptions) {
- options.disable = true
- }
- }
- func WithBeforeUpdateBuilderCallback(callbacks RequestBuilderCallback) UpdateOption {
- return func(options *UpdateOptions) {
- options.beforeBuilderCallback = callbacks
- }
- }
- func WithAfterUpdateBuilderCallback(callbacks RequestBuilderCallback) UpdateOption {
- return func(options *UpdateOptions) {
- options.afterBuilderCallback = callbacks
- }
- }
- func WithUpdateResponseSuccessCallback(responseSuccessCallback RequestBuilderCallback) UpdateOption {
- return func(options *UpdateOptions) {
- options.responseSuccessCallback = responseSuccessCallback
- }
- }
- func WithUpdateResponseErrorCallback(responseErrorCallback RequestBuilderErrorCallback) UpdateOption {
- return func(options *UpdateOptions) {
- options.responseErrorCallback = responseErrorCallback
- }
- }
- func WithUpdateMiddlewares(middlewares ...gateway.Handler) UpdateOption {
- return func(options *UpdateOptions) {
- 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
- }
- }
- func WithDisableGetByID() GetByIDOption {
- return func(options *GetByIDOptions) {
- options.disable = true
- }
- }
- func WithBeforeGetByIDBuilderCallback(callbacks RequestBuilderCallback) GetByIDOption {
- return func(options *GetByIDOptions) {
- options.beforeBuilderCallback = callbacks
- }
- }
- func WithAfterGetByIDBuilderCallback(callbacks RequestBuilderCallback) GetByIDOption {
- return func(options *GetByIDOptions) {
- options.afterBuilderCallback = callbacks
- }
- }
- func WithGetByIDResponseSuccessCallback(responseSuccessCallback RequestBuilderCallback) GetByIDOption {
- return func(options *GetByIDOptions) {
- options.responseSuccessCallback = responseSuccessCallback
- }
- }
- func WithGetByIDResponseErrorCallback(responseErrorCallback RequestBuilderErrorCallback) GetByIDOption {
- return func(options *GetByIDOptions) {
- options.responseErrorCallback = responseErrorCallback
- }
- }
- func WithGetByIDMiddlewares(middlewares ...gateway.Handler) GetByIDOption {
- return func(options *GetByIDOptions) {
- options.middlewares = middlewares
- }
- }
|