package value_object_crud import ( "git.sxidc.com/go-framework/baize/framework/binding" "git.sxidc.com/go-framework/baize/framework/core/api/request" "git.sxidc.com/go-framework/baize/framework/core/api/response" "git.sxidc.com/go-framework/baize/framework/core/domain" "git.sxidc.com/go-framework/baize/framework/core/domain/value_object" ) // Simple 值对象CRD的Bind参数 // I 为查询相关接口返回的Info类型 type Simple[I any] struct { // 使用的领域实体,注意是ValueObject类型 ValueObject value_object.ValueObject // 数据库schema Schema string // 创建使用的请求参数 CreateJsonBody request.Params // 删除使用的请求参数 DeleteJsonBody request.Params // 查询使用的请求参数,注意是Query类型 QueryQueryParams request.QueryRequestParams // 可选配置项,通过WithXXX配置 creatOptions *CreateOptions deleteOptions *DeleteOptions queryOptions *QueryOptions[I] } func (simple *Simple[I]) bind(binder *binding.Binder) { createOptions := simple.creatOptions deleteOptions := simple.deleteOptions queryOptions := simple.queryOptions tableName := domain.TableName(simple.Schema, simple.ValueObject) domainPath := domain.RelativeDomainPath(simple.ValueObject) // 创建 if !createOptions.disable { if !createOptions.needTx { binding.PostBind(binder, &binding.SimpleBindItem[any]{ Path: domainPath + "/create", SendResponseFunc: response.SendMsgResponse, RequestParams: simple.CreateJsonBody, Objects: []domain.Object{simple.ValueObject}, ServiceFunc: Create(tableName, createOptions.callbacks), }, createOptions.middlewares...) } else { binding.PostBind(binder, &binding.SimpleBindItem[any]{ Path: domainPath + "/create", SendResponseFunc: response.SendMsgResponse, RequestParams: simple.CreateJsonBody, Objects: []domain.Object{simple.ValueObject}, ServiceFunc: CreateTx(tableName, createOptions.callbacks), }, createOptions.middlewares...) } } // 删除 if !deleteOptions.disable { if !deleteOptions.needTx { binding.PostBind(binder, &binding.SimpleBindItem[any]{ Path: domainPath + "/delete", SendResponseFunc: response.SendMsgResponse, RequestParams: simple.DeleteJsonBody, BindRequestParamsFunc: request.BindJsonBody, Objects: []domain.Object{simple.ValueObject}, ServiceFunc: Delete(tableName, deleteOptions.callbacks), }, deleteOptions.middlewares...) } else { binding.PostBind(binder, &binding.SimpleBindItem[any]{ Path: domainPath + "/delete", SendResponseFunc: response.SendMsgResponse, RequestParams: simple.DeleteJsonBody, BindRequestParamsFunc: request.BindJsonBody, Objects: []domain.Object{simple.ValueObject}, ServiceFunc: DeleteTx(tableName, deleteOptions.callbacks), }, deleteOptions.middlewares...) } } // 查询 if !queryOptions.disable { binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{ Path: domainPath + "/query", SendResponseFunc: response.SendInfosResponse[I], RequestParams: simple.QueryQueryParams, Objects: []domain.Object{simple.ValueObject}, ServiceFunc: Query(tableName, queryOptions.callbacks, queryOptions.conditionFieldCallback), }, queryOptions.middlewares...) } } func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) { createOptions := new(CreateOptions) deleteOptions := new(DeleteOptions) queryOptions := new(QueryOptions[I]) for _, opt := range opts { switch o := opt.(type) { case CreateOption: o(createOptions) case DeleteOption: o(deleteOptions) case QueryOption[I]: o(queryOptions) default: continue } } simple.creatOptions = createOptions simple.deleteOptions = deleteOptions simple.queryOptions = queryOptions simple.bind(binder) } type CreateOption func(options *CreateOptions) type DeleteOption func(options *DeleteOptions) type QueryOption[I any] func(options *QueryOptions[I]) type CreateOptions struct { // 关闭创建 disable bool // 创建是否使用事务 needTx bool // 创建回调 callbacks *CreateCallbacks // 创建中间件 middlewares []binding.Middleware } type DeleteOptions struct { // 关闭删除 disable bool // 删除是否使用事务 needTx bool // 删除回调 callbacks *DeleteCallbacks // 删除中间件 middlewares []binding.Middleware } type QueryOptions[I any] struct { // 关闭查询 disable bool // 查询条件构造回调 conditionFieldCallback ConditionFieldCallback // 查询回调 callbacks *QueryCallbacks[I] // 查询中间件 middlewares []binding.Middleware } func WithDisableCreate() CreateOption { return func(options *CreateOptions) { options.disable = true } } func WithCreateTx() CreateOption { return func(options *CreateOptions) { options.needTx = true } } func WithCreateCallbacks(callbacks *CreateCallbacks) CreateOption { return func(options *CreateOptions) { options.callbacks = callbacks } } func WithCreateMiddlewares(middlewares ...binding.Middleware) CreateOption { return func(options *CreateOptions) { options.middlewares = middlewares } } func WithDisableDelete() DeleteOption { return func(options *DeleteOptions) { options.disable = true } } func WithDeleteTx() DeleteOption { return func(options *DeleteOptions) { options.needTx = true } } func WithDeleteCallbacks(callbacks *DeleteCallbacks) DeleteOption { return func(options *DeleteOptions) { options.callbacks = callbacks } } func WithDeleteMiddlewares(middlewares ...binding.Middleware) DeleteOption { return func(options *DeleteOptions) { options.middlewares = middlewares } } func WithDisableQuery[I any]() QueryOption[I] { return func(options *QueryOptions[I]) { options.disable = true } } func WithQueryConditionFieldCallback[I any](callback ConditionFieldCallback) QueryOption[I] { return func(options *QueryOptions[I]) { options.conditionFieldCallback = callback } } func WithQueryCallbacks[I any](callbacks *QueryCallbacks[I]) QueryOption[I] { return func(options *QueryOptions[I]) { options.callbacks = callbacks } } func WithQueryMiddlewares[I any](middlewares ...binding.Middleware) QueryOption[I] { return func(options *QueryOptions[I]) { options.middlewares = middlewares } }