package gwtools import ( "git.sxidc.com/go-framework/baize/framework/core/api" "git.sxidc.com/go-framework/baize/framework/gateway" "git.sxidc.com/go-tools/utils/template" "github.com/iancoleman/strcase" "net/http" ) func CRUD(builder *gateway.Builder, params *CommonCRUDParams, opts ...any) { createOptions := new(CreateOptions) deleteOptions := new(DeleteOptions) updateOptions := new(UpdateOptions) queryOptions := new(QueryOptions) getByIDOptions := new(GetByIDOptions) for _, opt := range opts { switch o := opt.(type) { case CreateOption: o(createOptions) case DeleteOption: o(deleteOptions) case UpdateOption: o(updateOptions) case QueryOption: o(queryOptions) case GetByIDOption: o(getByIDOptions) default: continue } } params.createOptions = createOptions params.deleteOptions = deleteOptions params.updateOptions = updateOptions params.queryOptions = queryOptions params.getByIDOptions = getByIDOptions params.crud(builder) } type CommonCRUDParams struct { // 除去后缀的服务URL,如http://localhost:8080/example/api/v1 ServiceVersionedUrl string // 领域名称 DomainCamelName string CreateMiddlewares []api.Handler DeleteMiddlewares []api.Handler UpdateMiddlewares []api.Handler QueryMiddlewares []api.Handler GetByIDMiddlewares []api.Handler // 可选配置项,通过WithXXX配置 createOptions *CreateOptions deleteOptions *DeleteOptions updateOptions *UpdateOptions queryOptions *QueryOptions getByIDOptions *GetByIDOptions } func (params *CommonCRUDParams) crud(builder *gateway.Builder) { createOptions := params.createOptions deleteOptions := params.deleteOptions updateOptions := params.updateOptions queryOptions := params.queryOptions getByIDOptions := params.getByIDOptions domainPath := "/" + strcase.ToLowerCamel(template.Id(params.DomainCamelName)) // 创建 if !createOptions.disableCreate { builder. Url(http.MethodPost, domainPath+"/create"). Post(&gateway.PostRequest{ Url: params.ServiceVersionedUrl + domainPath + "/create", }, createOptions.createCallback). Build(createOptions.createMiddlewares...) } // 删除 if !deleteOptions.disableDelete { builder. Url(http.MethodDelete, domainPath+"/delete"). Delete(&gateway.DeleteRequest{ Url: params.ServiceVersionedUrl + domainPath + "/delete", }, deleteOptions.deleteCallback). Build(deleteOptions.deleteMiddlewares...) } // 修改 if !updateOptions.disableUpdate { builder. Url(http.MethodPut, domainPath+"/update"). Put(&gateway.PutRequest{ Url: params.ServiceVersionedUrl + domainPath + "/update", }, updateOptions.updateCallback). Build(updateOptions.updateMiddlewares...) } // 查询 if !queryOptions.disableQuery { builder. Url(http.MethodGet, domainPath+"/query"). Get(&gateway.GetRequest{ Url: params.ServiceVersionedUrl + domainPath + "/query", }, queryOptions.queryCallback). Build(queryOptions.queryMiddlewares...) } // 通过ID获取 if !getByIDOptions.disableGetByID { builder. Url(http.MethodGet, domainPath+"/get"). Get(&gateway.GetRequest{ Url: params.ServiceVersionedUrl + domainPath + "/get", }, getByIDOptions.getByIDCallback). Build(getByIDOptions.getByIDMiddlewares...) } } 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 CreateOptions struct { // 关闭创建 disableCreate bool // 创建回调 createCallback gateway.RequestCallbackFunc // 创建中间件 createMiddlewares []api.Handler } type DeleteOptions struct { // 关闭删除 disableDelete bool // 删除回调 deleteCallback gateway.RequestCallbackFunc // 删除中间件 deleteMiddlewares []api.Handler } type UpdateOptions struct { // 关闭更新 disableUpdate bool // 更新回调 updateCallback gateway.RequestCallbackFunc // 更新中间件 updateMiddlewares []api.Handler } type QueryOptions struct { // 关闭查询 disableQuery bool // 查询回调 queryCallback gateway.RequestCallbackFunc // 查询中间件 queryMiddlewares []api.Handler } type GetByIDOptions struct { // 关闭根据ID查询 disableGetByID bool // 根据ID查询回调 getByIDCallback gateway.RequestCallbackFunc // 根据ID查询中间件 getByIDMiddlewares []api.Handler } func WithCreateCallbacks(callbacks gateway.RequestCallbackFunc) CreateOption { return func(options *CreateOptions) { options.createCallback = callbacks } } func WithCreateMiddlewares(middlewares []api.Handler) CreateOption { return func(options *CreateOptions) { options.createMiddlewares = middlewares } } func WithDisableDelete() DeleteOption { return func(options *DeleteOptions) { options.disableDelete = true } } func WithDeleteCallbacks(callbacks gateway.RequestCallbackFunc) DeleteOption { return func(options *DeleteOptions) { options.deleteCallback = callbacks } } func WithDeleteMiddlewares(middlewares []api.Handler) DeleteOption { return func(options *DeleteOptions) { options.deleteMiddlewares = middlewares } } func WithDisableUpdate() UpdateOption { return func(options *UpdateOptions) { options.disableUpdate = true } } func WithUpdateCallbacks(callbacks gateway.RequestCallbackFunc) UpdateOption { return func(options *UpdateOptions) { options.updateCallback = callbacks } } func WithUpdateMiddlewares(middlewares []api.Handler) UpdateOption { return func(options *UpdateOptions) { options.updateMiddlewares = middlewares } } func WithDisableQuery() QueryOption { return func(options *QueryOptions) { options.disableQuery = true } } func WithQueryCallbacks(callbacks gateway.RequestCallbackFunc) QueryOption { return func(options *QueryOptions) { options.queryCallback = callbacks } } func WithQueryMiddlewares(middlewares []api.Handler) QueryOption { return func(options *QueryOptions) { options.queryMiddlewares = middlewares } } func WithDisableGetByID() GetByIDOption { return func(options *GetByIDOptions) { options.disableGetByID = true } } func WithGetByIDCallbacks(callbacks gateway.RequestCallbackFunc) GetByIDOption { return func(options *GetByIDOptions) { options.getByIDCallback = callbacks } } func WithGetByIDMiddlewares(middlewares []api.Handler) GetByIDOption { return func(options *GetByIDOptions) { options.getByIDMiddlewares = middlewares } }