simple.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package value_object
  2. import (
  3. "git.sxidc.com/go-framework/baize/api"
  4. "git.sxidc.com/go-framework/baize/binding"
  5. "git.sxidc.com/go-framework/baize/binding/request"
  6. "git.sxidc.com/go-framework/baize/binding/response"
  7. "git.sxidc.com/go-framework/baize/domain"
  8. )
  9. // Simple 实体CRUD的Bind参数
  10. // I 为查询相关接口返回的Info类型
  11. type Simple[I any] struct {
  12. // 使用的领域实体,注意是ValueObject类型
  13. ValueObject domain.ValueObject
  14. // 表名
  15. TableName string
  16. // 选择要使用的数据库Executor
  17. // DBExecutorOperations operations 数据库操作
  18. // DBExecutorDataService data_service 数据服务
  19. DBExecutorType string
  20. // URL领域相对路径,如/class,后面会自动补充
  21. DomainPath string
  22. // 创建使用的请求参数
  23. CreateJsonBody request.Params
  24. // 删除使用的请求参数,注意是WithID类型
  25. DeleteQueryParams request.WithID
  26. // 查询使用的请求参数,注意是Query类型
  27. QueryParams request.Query
  28. // 可选配置项,通过WithXXX配置
  29. options *Options[I]
  30. }
  31. func (simple *Simple[I]) bind(binder *binding.Binder) {
  32. dbExecutor := binder.ChooseDBExecutor(simple.DBExecutorType)
  33. options := simple.options
  34. // 创建
  35. if !options.createNeedTx {
  36. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  37. Path: simple.DomainPath + "/create",
  38. ResponseFunc: response.SendMsgResponse,
  39. RequestParams: simple.CreateJsonBody,
  40. Objects: []domain.Object{simple.ValueObject},
  41. ServiceFunc: Create(simple.TableName, dbExecutor, options.createCallbacks),
  42. })
  43. } else {
  44. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  45. Path: simple.DomainPath + "/create",
  46. ResponseFunc: response.SendMsgResponse,
  47. RequestParams: simple.CreateJsonBody,
  48. Objects: []domain.Object{simple.ValueObject},
  49. ServiceFunc: CreateTx(simple.TableName, dbExecutor, options.createCallbacks),
  50. })
  51. }
  52. // 删除班级
  53. if !options.deleteNeedTx {
  54. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  55. Path: simple.DomainPath + "/:id/delete",
  56. ResponseFunc: response.SendMsgResponse,
  57. RequestParams: simple.DeleteQueryParams,
  58. Objects: []domain.Object{simple.ValueObject},
  59. ServiceFunc: Delete(simple.TableName, dbExecutor, options.deleteCallbacks),
  60. })
  61. } else {
  62. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  63. Path: simple.DomainPath + "/:id/delete",
  64. ResponseFunc: response.SendMsgResponse,
  65. RequestParams: simple.DeleteQueryParams,
  66. Objects: []domain.Object{simple.ValueObject},
  67. ServiceFunc: DeleteTx(simple.TableName, dbExecutor, options.deleteCallbacks),
  68. })
  69. }
  70. // 查询班级
  71. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
  72. Path: simple.DomainPath + "/query",
  73. ResponseFunc: response.SendInfosResponse[I],
  74. RequestParams: simple.QueryParams,
  75. Objects: []domain.Object{simple.ValueObject},
  76. ServiceFunc: Query(simple.TableName, dbExecutor, options.queryCallbacks, options.queryConditionFieldCallback),
  77. })
  78. }
  79. func BindSimple[I any](binder *binding.Binder, crud *Simple[I]) {
  80. crud.bind(binder)
  81. }
  82. type Option[I any] func(options *Options[I])
  83. type Options[I any] struct {
  84. // 创建是否使用事务
  85. createNeedTx bool
  86. // 创建回调
  87. createCallbacks *Callbacks[any]
  88. // 创建中间件
  89. createMiddlewares []api.Handler
  90. // 删除是否使用事务
  91. deleteNeedTx bool
  92. // 删除回调
  93. deleteCallbacks *Callbacks[any]
  94. // 删除中间件
  95. deleteMiddlewares []api.Handler
  96. // 查询条件构造回调
  97. queryConditionFieldCallback ConditionFieldCallback
  98. // 查询回调
  99. queryCallbacks *Callbacks[response.InfosData[I]]
  100. // 查询中间件
  101. queryMiddlewares []api.Handler
  102. }
  103. func WithCreateTx[I any]() Option[I] {
  104. return func(options *Options[I]) {
  105. options.createNeedTx = true
  106. }
  107. }
  108. func WithCreateCallbacks[I any](callbacks *Callbacks[any]) Option[I] {
  109. return func(options *Options[I]) {
  110. options.createCallbacks = callbacks
  111. }
  112. }
  113. func WithCreateMiddlewares[I any](middlewares []api.Handler) Option[I] {
  114. return func(options *Options[I]) {
  115. options.createMiddlewares = middlewares
  116. }
  117. }
  118. func WithDeleteTx[I any]() Option[I] {
  119. return func(options *Options[I]) {
  120. options.deleteNeedTx = true
  121. }
  122. }
  123. func WithDeleteCallbacks[I any](callbacks *Callbacks[any]) Option[I] {
  124. return func(options *Options[I]) {
  125. options.deleteCallbacks = callbacks
  126. }
  127. }
  128. func WithDeleteMiddlewares[I any](middlewares []api.Handler) Option[I] {
  129. return func(options *Options[I]) {
  130. options.deleteMiddlewares = middlewares
  131. }
  132. }
  133. func WithQueryConditionFieldCallback[I any](callback ConditionFieldCallback) Option[I] {
  134. return func(options *Options[I]) {
  135. options.queryConditionFieldCallback = callback
  136. }
  137. }
  138. func WithQueryCallbacks[I any](callbacks *Callbacks[response.InfosData[I]]) Option[I] {
  139. return func(options *Options[I]) {
  140. options.queryCallbacks = callbacks
  141. }
  142. }
  143. func WithQueryMiddlewares[I any](middlewares []api.Handler) Option[I] {
  144. return func(options *Options[I]) {
  145. options.queryMiddlewares = middlewares
  146. }
  147. }