123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- 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配置
- globalOptions *GlobalOptions
- creatOptions *CreateOptions
- deleteOptions *DeleteOptions
- queryOptions *QueryOptions[I]
- }
- func (simple *Simple[I]) bind(binder *binding.Binder) {
- globalOptions := simple.globalOptions
- createOptions := simple.creatOptions
- deleteOptions := simple.deleteOptions
- queryOptions := simple.queryOptions
- tableName := domain.TableName(simple.Schema, simple.ValueObject)
- domainPath := domain.RelativeDomainPath(simple.ValueObject)
- // 创建
- if !createOptions.disable {
- createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...)
- 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.needTx),
- }, createMiddlewares...)
- }
- // 删除
- if !deleteOptions.disable {
- deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
- 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.needTx),
- }, deleteMiddlewares...)
- }
- // 查询
- if !queryOptions.disable {
- queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...)
- 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.orderBy, queryOptions.callbacks, queryOptions.conditionFieldCallback, queryOptions.formCustomConditionFunc),
- }, queryMiddlewares...)
- }
- }
- func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
- globalOptions := new(GlobalOptions)
- createOptions := new(CreateOptions)
- deleteOptions := new(DeleteOptions)
- queryOptions := new(QueryOptions[I])
- for _, opt := range opts {
- switch o := opt.(type) {
- case GlobalOption:
- o(globalOptions)
- case CreateOption:
- o(createOptions)
- case DeleteOption:
- o(deleteOptions)
- case QueryOption[I]:
- o(queryOptions)
- default:
- continue
- }
- }
- simple.globalOptions = globalOptions
- simple.creatOptions = createOptions
- simple.deleteOptions = deleteOptions
- simple.queryOptions = queryOptions
- simple.bind(binder)
- }
- type GlobalOption func(options *GlobalOptions)
- type CreateOption func(options *CreateOptions)
- type DeleteOption func(options *DeleteOptions)
- type QueryOption[I any] func(options *QueryOptions[I])
- type GlobalOptions struct {
- middlewares []binding.Middleware
- }
- 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
- // 自定义添加构造函数
- formCustomConditionFunc FormCustomConditionFunc
- // 查询回调
- callbacks *QueryCallbacks[I]
- // 查询中间件
- middlewares []binding.Middleware
- // OrderBy
- orderBy string
- }
- func WithGlobalMiddlewares(middlewares ...binding.Middleware) GlobalOption {
- return func(options *GlobalOptions) {
- options.middlewares = middlewares
- }
- }
- 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 WithQueryFormCustomConditionFunc[I any](formCustomConditionFunc FormCustomConditionFunc) QueryOption[I] {
- return func(options *QueryOptions[I]) {
- options.formCustomConditionFunc = formCustomConditionFunc
- }
- }
- 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
- }
- }
- func WithQueryOrderBy[I any](orderBy string) QueryOption[I] {
- return func(options *QueryOptions[I]) {
- options.orderBy = orderBy
- }
- }
|