| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package value_object
- import (
- "git.sxidc.com/go-framework/baize/api"
- "git.sxidc.com/go-framework/baize/binding"
- "git.sxidc.com/go-framework/baize/binding/request"
- "git.sxidc.com/go-framework/baize/binding/response"
- "git.sxidc.com/go-framework/baize/domain"
- )
- // Simple 实体CRUD的Bind参数
- // I 为查询相关接口返回的Info类型
- type Simple[I any] struct {
- // 使用的领域实体,注意是ValueObject类型
- ValueObject domain.ValueObject
- // 表名
- TableName string
- // 选择要使用的数据库Executor
- // DBExecutorOperations operations 数据库操作
- // DBExecutorDataService data_service 数据服务
- DBExecutorType string
- // URL领域相对路径,如/class,后面会自动补充
- DomainPath string
- // 创建使用的请求参数
- CreateJsonBody request.Params
- // 删除使用的请求参数,注意是WithID类型
- DeleteQueryParams request.WithID
- // 查询使用的请求参数,注意是Query类型
- QueryParams request.Query
- // 可选配置项,通过WithXXX配置
- options *Options[I]
- }
- func (simple *Simple[I]) bind(binder *binding.Binder) {
- dbExecutor := binder.ChooseDBExecutor(simple.DBExecutorType)
- options := simple.options
- // 创建
- if !options.createNeedTx {
- binding.PostBind(binder, &binding.SimpleBindItem[any]{
- Path: simple.DomainPath + "/create",
- ResponseFunc: response.SendMsgResponse,
- RequestParams: simple.CreateJsonBody,
- Objects: []domain.Object{simple.ValueObject},
- ServiceFunc: Create(simple.TableName, dbExecutor, options.createCallbacks),
- })
- } else {
- binding.PostBind(binder, &binding.SimpleBindItem[any]{
- Path: simple.DomainPath + "/create",
- ResponseFunc: response.SendMsgResponse,
- RequestParams: simple.CreateJsonBody,
- Objects: []domain.Object{simple.ValueObject},
- ServiceFunc: CreateTx(simple.TableName, dbExecutor, options.createCallbacks),
- })
- }
- // 删除班级
- if !options.deleteNeedTx {
- binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
- Path: simple.DomainPath + "/:id/delete",
- ResponseFunc: response.SendMsgResponse,
- RequestParams: simple.DeleteQueryParams,
- Objects: []domain.Object{simple.ValueObject},
- ServiceFunc: Delete(simple.TableName, dbExecutor, options.deleteCallbacks),
- })
- } else {
- binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
- Path: simple.DomainPath + "/:id/delete",
- ResponseFunc: response.SendMsgResponse,
- RequestParams: simple.DeleteQueryParams,
- Objects: []domain.Object{simple.ValueObject},
- ServiceFunc: DeleteTx(simple.TableName, dbExecutor, options.deleteCallbacks),
- })
- }
- // 查询班级
- binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
- Path: simple.DomainPath + "/query",
- ResponseFunc: response.SendInfosResponse[I],
- RequestParams: simple.QueryParams,
- Objects: []domain.Object{simple.ValueObject},
- ServiceFunc: Query(simple.TableName, dbExecutor, options.queryCallbacks, options.queryConditionFieldCallback),
- })
- }
- func BindSimple[I any](binder *binding.Binder, crud *Simple[I]) {
- crud.bind(binder)
- }
- type Option[I any] func(options *Options[I])
- type Options[I any] struct {
- // 创建是否使用事务
- createNeedTx bool
- // 创建回调
- createCallbacks *Callbacks[any]
- // 创建中间件
- createMiddlewares []api.Handler
- // 删除是否使用事务
- deleteNeedTx bool
- // 删除回调
- deleteCallbacks *Callbacks[any]
- // 删除中间件
- deleteMiddlewares []api.Handler
- // 查询条件构造回调
- queryConditionFieldCallback ConditionFieldCallback
- // 查询回调
- queryCallbacks *Callbacks[response.InfosData[I]]
- // 查询中间件
- queryMiddlewares []api.Handler
- }
- func WithCreateTx[I any]() Option[I] {
- return func(options *Options[I]) {
- options.createNeedTx = true
- }
- }
- func WithCreateCallbacks[I any](callbacks *Callbacks[any]) Option[I] {
- return func(options *Options[I]) {
- options.createCallbacks = callbacks
- }
- }
- func WithCreateMiddlewares[I any](middlewares []api.Handler) Option[I] {
- return func(options *Options[I]) {
- options.createMiddlewares = middlewares
- }
- }
- func WithDeleteTx[I any]() Option[I] {
- return func(options *Options[I]) {
- options.deleteNeedTx = true
- }
- }
- func WithDeleteCallbacks[I any](callbacks *Callbacks[any]) Option[I] {
- return func(options *Options[I]) {
- options.deleteCallbacks = callbacks
- }
- }
- func WithDeleteMiddlewares[I any](middlewares []api.Handler) Option[I] {
- return func(options *Options[I]) {
- options.deleteMiddlewares = middlewares
- }
- }
- func WithQueryConditionFieldCallback[I any](callback ConditionFieldCallback) Option[I] {
- return func(options *Options[I]) {
- options.queryConditionFieldCallback = callback
- }
- }
- func WithQueryCallbacks[I any](callbacks *Callbacks[response.InfosData[I]]) Option[I] {
- return func(options *Options[I]) {
- options.queryCallbacks = callbacks
- }
- }
- func WithQueryMiddlewares[I any](middlewares []api.Handler) Option[I] {
- return func(options *Options[I]) {
- options.queryMiddlewares = middlewares
- }
- }
|