simple.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package value_object_crud
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/binding"
  4. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  5. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  6. "git.sxidc.com/go-framework/baize/framework/core/domain"
  7. "git.sxidc.com/go-framework/baize/framework/core/domain/value_object"
  8. )
  9. // Simple 值对象CRD的Bind参数
  10. // I 为查询相关接口返回的Info类型
  11. type Simple[I any] struct {
  12. // 使用的领域实体,注意是ValueObject类型
  13. ValueObject value_object.ValueObject
  14. // 数据库schema
  15. Schema string
  16. // 创建使用的请求参数
  17. CreateJsonBody request.Params
  18. // 删除使用的请求参数
  19. DeleteJsonBody request.Params
  20. // 查询使用的请求参数,注意是Query类型
  21. QueryQueryParams request.QueryRequestParams
  22. // 可选配置项,通过WithXXX配置
  23. creatOptions *CreateOptions
  24. deleteOptions *DeleteOptions
  25. queryOptions *QueryOptions[I]
  26. }
  27. func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Middleware) {
  28. createOptions := simple.creatOptions
  29. deleteOptions := simple.deleteOptions
  30. queryOptions := simple.queryOptions
  31. tableName := domain.TableName(simple.Schema, simple.ValueObject)
  32. domainPath := domain.RelativeDomainPath(simple.ValueObject)
  33. // 创建
  34. if !createOptions.disable {
  35. createMiddlewares := append(middlewares, createOptions.middlewares...)
  36. if !createOptions.needTx {
  37. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  38. Path: domainPath + "/create",
  39. SendResponseFunc: response.SendMsgResponse,
  40. RequestParams: simple.CreateJsonBody,
  41. Objects: []domain.Object{simple.ValueObject},
  42. ServiceFunc: Create(tableName, createOptions.callbacks),
  43. }, createMiddlewares...)
  44. } else {
  45. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  46. Path: domainPath + "/create",
  47. SendResponseFunc: response.SendMsgResponse,
  48. RequestParams: simple.CreateJsonBody,
  49. Objects: []domain.Object{simple.ValueObject},
  50. ServiceFunc: CreateTx(tableName, createOptions.callbacks),
  51. }, createMiddlewares...)
  52. }
  53. }
  54. // 删除
  55. if !deleteOptions.disable {
  56. deleteMiddlewares := append(middlewares, deleteOptions.middlewares...)
  57. if !deleteOptions.needTx {
  58. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  59. Path: domainPath + "/delete",
  60. SendResponseFunc: response.SendMsgResponse,
  61. RequestParams: simple.DeleteJsonBody,
  62. BindRequestParamsFunc: request.BindJsonBody,
  63. Objects: []domain.Object{simple.ValueObject},
  64. ServiceFunc: Delete(tableName, deleteOptions.callbacks),
  65. }, deleteMiddlewares...)
  66. } else {
  67. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  68. Path: domainPath + "/delete",
  69. SendResponseFunc: response.SendMsgResponse,
  70. RequestParams: simple.DeleteJsonBody,
  71. BindRequestParamsFunc: request.BindJsonBody,
  72. Objects: []domain.Object{simple.ValueObject},
  73. ServiceFunc: DeleteTx(tableName, deleteOptions.callbacks),
  74. }, deleteMiddlewares...)
  75. }
  76. }
  77. // 查询
  78. if !queryOptions.disable {
  79. queryMiddlewares := append(middlewares, queryOptions.middlewares...)
  80. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
  81. Path: domainPath + "/query",
  82. SendResponseFunc: response.SendInfosResponse[I],
  83. RequestParams: simple.QueryQueryParams,
  84. Objects: []domain.Object{simple.ValueObject},
  85. ServiceFunc: Query(tableName, queryOptions.callbacks, queryOptions.conditionFieldCallback),
  86. }, queryMiddlewares...)
  87. }
  88. }
  89. func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
  90. createOptions := new(CreateOptions)
  91. deleteOptions := new(DeleteOptions)
  92. queryOptions := new(QueryOptions[I])
  93. for _, opt := range opts {
  94. switch o := opt.(type) {
  95. case CreateOption:
  96. o(createOptions)
  97. case DeleteOption:
  98. o(deleteOptions)
  99. case QueryOption[I]:
  100. o(queryOptions)
  101. default:
  102. continue
  103. }
  104. }
  105. simple.creatOptions = createOptions
  106. simple.deleteOptions = deleteOptions
  107. simple.queryOptions = queryOptions
  108. simple.bind(binder)
  109. }
  110. type CreateOption func(options *CreateOptions)
  111. type DeleteOption func(options *DeleteOptions)
  112. type QueryOption[I any] func(options *QueryOptions[I])
  113. type CreateOptions struct {
  114. // 关闭创建
  115. disable bool
  116. // 创建是否使用事务
  117. needTx bool
  118. // 创建回调
  119. callbacks *CreateCallbacks
  120. // 创建中间件
  121. middlewares []binding.Middleware
  122. }
  123. type DeleteOptions struct {
  124. // 关闭删除
  125. disable bool
  126. // 删除是否使用事务
  127. needTx bool
  128. // 删除回调
  129. callbacks *DeleteCallbacks
  130. // 删除中间件
  131. middlewares []binding.Middleware
  132. }
  133. type QueryOptions[I any] struct {
  134. // 关闭查询
  135. disable bool
  136. // 查询条件构造回调
  137. conditionFieldCallback ConditionFieldCallback
  138. // 查询回调
  139. callbacks *QueryCallbacks[I]
  140. // 查询中间件
  141. middlewares []binding.Middleware
  142. }
  143. func WithDisableCreate() CreateOption {
  144. return func(options *CreateOptions) {
  145. options.disable = true
  146. }
  147. }
  148. func WithCreateTx() CreateOption {
  149. return func(options *CreateOptions) {
  150. options.needTx = true
  151. }
  152. }
  153. func WithCreateCallbacks(callbacks *CreateCallbacks) CreateOption {
  154. return func(options *CreateOptions) {
  155. options.callbacks = callbacks
  156. }
  157. }
  158. func WithCreateMiddlewares(middlewares ...binding.Middleware) CreateOption {
  159. return func(options *CreateOptions) {
  160. options.middlewares = middlewares
  161. }
  162. }
  163. func WithDisableDelete() DeleteOption {
  164. return func(options *DeleteOptions) {
  165. options.disable = true
  166. }
  167. }
  168. func WithDeleteTx() DeleteOption {
  169. return func(options *DeleteOptions) {
  170. options.needTx = true
  171. }
  172. }
  173. func WithDeleteCallbacks(callbacks *DeleteCallbacks) DeleteOption {
  174. return func(options *DeleteOptions) {
  175. options.callbacks = callbacks
  176. }
  177. }
  178. func WithDeleteMiddlewares(middlewares ...binding.Middleware) DeleteOption {
  179. return func(options *DeleteOptions) {
  180. options.middlewares = middlewares
  181. }
  182. }
  183. func WithDisableQuery[I any]() QueryOption[I] {
  184. return func(options *QueryOptions[I]) {
  185. options.disable = true
  186. }
  187. }
  188. func WithQueryConditionFieldCallback[I any](callback ConditionFieldCallback) QueryOption[I] {
  189. return func(options *QueryOptions[I]) {
  190. options.conditionFieldCallback = callback
  191. }
  192. }
  193. func WithQueryCallbacks[I any](callbacks *QueryCallbacks[I]) QueryOption[I] {
  194. return func(options *QueryOptions[I]) {
  195. options.callbacks = callbacks
  196. }
  197. }
  198. func WithQueryMiddlewares[I any](middlewares ...binding.Middleware) QueryOption[I] {
  199. return func(options *QueryOptions[I]) {
  200. options.middlewares = middlewares
  201. }
  202. }