|
|
@@ -1,18 +1,16 @@
|
|
|
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"
|
|
|
)
|
|
|
|
|
|
-func BindSimple[O any](binder *binding.Binder, crud *Simple[O]) {
|
|
|
- crud.bind(binder)
|
|
|
-}
|
|
|
-
|
|
|
// Simple 实体CRUD的Bind参数
|
|
|
-type Simple[O any] struct {
|
|
|
+// I 为查询相关接口返回的Info类型
|
|
|
+type Simple[I any] struct {
|
|
|
// 使用的领域实体,注意是ValueObject类型
|
|
|
ValueObject domain.ValueObject
|
|
|
|
|
|
@@ -30,78 +28,152 @@ type Simple[O any] struct {
|
|
|
// 创建使用的请求参数
|
|
|
CreateJsonBody request.Params
|
|
|
|
|
|
- // 创建是否使用事务
|
|
|
- CreateNeedTx bool
|
|
|
-
|
|
|
- // 创建回调
|
|
|
- CreateCallbacks *Callbacks[any]
|
|
|
-
|
|
|
// 删除使用的请求参数,注意是WithID类型
|
|
|
DeleteQueryParams request.WithID
|
|
|
|
|
|
- // 删除是否使用事务
|
|
|
- DeleteNeedTx bool
|
|
|
-
|
|
|
- // 删除回调
|
|
|
- DeleteCallbacks *Callbacks[any]
|
|
|
-
|
|
|
// 查询使用的请求参数,注意是Query类型
|
|
|
QueryParams request.Query
|
|
|
|
|
|
- // 查询条件构造回调
|
|
|
- QueryConditionFieldCallback ConditionFieldCallback
|
|
|
-
|
|
|
- // 查询回调
|
|
|
- QueryCallbacks *Callbacks[response.InfosData[O]]
|
|
|
+ options *Options[I]
|
|
|
}
|
|
|
|
|
|
-func (crud *Simple[O]) bind(binder *binding.Binder) {
|
|
|
- dbExecutor := binder.ChooseDBExecutor(crud.DBExecutorType)
|
|
|
+func (simple *Simple[I]) bind(binder *binding.Binder) {
|
|
|
+ dbExecutor := binder.ChooseDBExecutor(simple.DBExecutorType)
|
|
|
+ options := simple.options
|
|
|
|
|
|
// 创建
|
|
|
- if !crud.CreateNeedTx {
|
|
|
+ if !options.createNeedTx {
|
|
|
binding.PostBind(binder, &binding.SimpleBindItem[any]{
|
|
|
- Path: crud.DomainPath + "/create",
|
|
|
+ Path: simple.DomainPath + "/create",
|
|
|
ResponseFunc: response.SendMsgResponse,
|
|
|
- RequestParams: crud.CreateJsonBody,
|
|
|
- Objects: []domain.Object{crud.ValueObject},
|
|
|
- ServiceFunc: Create(crud.TableName, dbExecutor, crud.CreateCallbacks),
|
|
|
+ RequestParams: simple.CreateJsonBody,
|
|
|
+ Objects: []domain.Object{simple.ValueObject},
|
|
|
+ ServiceFunc: Create(simple.TableName, dbExecutor, options.createCallbacks),
|
|
|
})
|
|
|
} else {
|
|
|
binding.PostBind(binder, &binding.SimpleBindItem[any]{
|
|
|
- Path: crud.DomainPath + "/create",
|
|
|
+ Path: simple.DomainPath + "/create",
|
|
|
ResponseFunc: response.SendMsgResponse,
|
|
|
- RequestParams: crud.CreateJsonBody,
|
|
|
- Objects: []domain.Object{crud.ValueObject},
|
|
|
- ServiceFunc: CreateTx(crud.TableName, dbExecutor, crud.CreateCallbacks),
|
|
|
+ RequestParams: simple.CreateJsonBody,
|
|
|
+ Objects: []domain.Object{simple.ValueObject},
|
|
|
+ ServiceFunc: CreateTx(simple.TableName, dbExecutor, options.createCallbacks),
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// 删除班级
|
|
|
- if !crud.DeleteNeedTx {
|
|
|
+ if !options.deleteNeedTx {
|
|
|
binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
|
|
|
- Path: crud.DomainPath + "/:id/delete",
|
|
|
+ Path: simple.DomainPath + "/:id/delete",
|
|
|
ResponseFunc: response.SendMsgResponse,
|
|
|
- RequestParams: crud.DeleteQueryParams,
|
|
|
- Objects: []domain.Object{crud.ValueObject},
|
|
|
- ServiceFunc: Delete(crud.TableName, dbExecutor, crud.DeleteCallbacks),
|
|
|
+ RequestParams: simple.DeleteQueryParams,
|
|
|
+ Objects: []domain.Object{simple.ValueObject},
|
|
|
+ ServiceFunc: Delete(simple.TableName, dbExecutor, options.deleteCallbacks),
|
|
|
})
|
|
|
} else {
|
|
|
binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
|
|
|
- Path: crud.DomainPath + "/:id/delete",
|
|
|
+ Path: simple.DomainPath + "/:id/delete",
|
|
|
ResponseFunc: response.SendMsgResponse,
|
|
|
- RequestParams: crud.DeleteQueryParams,
|
|
|
- Objects: []domain.Object{crud.ValueObject},
|
|
|
- ServiceFunc: DeleteTx(crud.TableName, dbExecutor, crud.DeleteCallbacks),
|
|
|
+ RequestParams: simple.DeleteQueryParams,
|
|
|
+ Objects: []domain.Object{simple.ValueObject},
|
|
|
+ ServiceFunc: DeleteTx(simple.TableName, dbExecutor, options.deleteCallbacks),
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// 查询班级
|
|
|
- binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[O]]{
|
|
|
- Path: crud.DomainPath + "/query",
|
|
|
- ResponseFunc: response.SendInfosResponse[O],
|
|
|
- RequestParams: crud.QueryParams,
|
|
|
- Objects: []domain.Object{crud.ValueObject},
|
|
|
- ServiceFunc: Query(crud.TableName, dbExecutor, crud.QueryCallbacks, crud.QueryConditionFieldCallback),
|
|
|
+ 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
|
|
|
+ }
|
|
|
+}
|