simple.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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) {
  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. if !createOptions.needTx {
  36. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  37. Path: domainPath + "/create",
  38. SendResponseFunc: response.SendMsgResponse,
  39. RequestParams: simple.CreateJsonBody,
  40. Objects: []domain.Object{simple.ValueObject},
  41. ServiceFunc: Create(tableName, createOptions.callbacks),
  42. }, createOptions.middlewares...)
  43. } else {
  44. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  45. Path: domainPath + "/create",
  46. SendResponseFunc: response.SendMsgResponse,
  47. RequestParams: simple.CreateJsonBody,
  48. Objects: []domain.Object{simple.ValueObject},
  49. ServiceFunc: CreateTx(tableName, createOptions.callbacks),
  50. }, createOptions.middlewares...)
  51. }
  52. }
  53. // 删除
  54. if !deleteOptions.disable {
  55. if !deleteOptions.needTx {
  56. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  57. Path: domainPath + "/delete",
  58. SendResponseFunc: response.SendMsgResponse,
  59. RequestParams: simple.DeleteJsonBody,
  60. BindRequestParamsFunc: request.BindJsonBody,
  61. Objects: []domain.Object{simple.ValueObject},
  62. ServiceFunc: Delete(tableName, deleteOptions.callbacks),
  63. }, deleteOptions.middlewares...)
  64. } else {
  65. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  66. Path: domainPath + "/delete",
  67. SendResponseFunc: response.SendMsgResponse,
  68. RequestParams: simple.DeleteJsonBody,
  69. BindRequestParamsFunc: request.BindJsonBody,
  70. Objects: []domain.Object{simple.ValueObject},
  71. ServiceFunc: DeleteTx(tableName, deleteOptions.callbacks),
  72. }, deleteOptions.middlewares...)
  73. }
  74. }
  75. // 查询
  76. if !queryOptions.disable {
  77. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
  78. Path: domainPath + "/query",
  79. SendResponseFunc: response.SendInfosResponse[I],
  80. RequestParams: simple.QueryQueryParams,
  81. Objects: []domain.Object{simple.ValueObject},
  82. ServiceFunc: Query(tableName, queryOptions.callbacks, queryOptions.conditionFieldCallback),
  83. }, queryOptions.middlewares...)
  84. }
  85. }
  86. func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
  87. createOptions := new(CreateOptions)
  88. deleteOptions := new(DeleteOptions)
  89. queryOptions := new(QueryOptions[I])
  90. for _, opt := range opts {
  91. switch o := opt.(type) {
  92. case CreateOption:
  93. o(createOptions)
  94. case DeleteOption:
  95. o(deleteOptions)
  96. case QueryOption[I]:
  97. o(queryOptions)
  98. default:
  99. continue
  100. }
  101. }
  102. simple.creatOptions = createOptions
  103. simple.deleteOptions = deleteOptions
  104. simple.queryOptions = queryOptions
  105. simple.bind(binder)
  106. }
  107. type CreateOption func(options *CreateOptions)
  108. type DeleteOption func(options *DeleteOptions)
  109. type QueryOption[I any] func(options *QueryOptions[I])
  110. type CreateOptions struct {
  111. // 关闭创建
  112. disable bool
  113. // 创建是否使用事务
  114. needTx bool
  115. // 创建回调
  116. callbacks *CreateCallbacks
  117. // 创建中间件
  118. middlewares []binding.Middleware
  119. }
  120. type DeleteOptions struct {
  121. // 关闭删除
  122. disable bool
  123. // 删除是否使用事务
  124. needTx bool
  125. // 删除回调
  126. callbacks *DeleteCallbacks
  127. // 删除中间件
  128. middlewares []binding.Middleware
  129. }
  130. type QueryOptions[I any] struct {
  131. // 关闭查询
  132. disable bool
  133. // 查询条件构造回调
  134. conditionFieldCallback ConditionFieldCallback
  135. // 查询回调
  136. callbacks *QueryCallbacks[I]
  137. // 查询中间件
  138. middlewares []binding.Middleware
  139. }
  140. func WithDisableCreate() CreateOption {
  141. return func(options *CreateOptions) {
  142. options.disable = true
  143. }
  144. }
  145. func WithCreateTx() CreateOption {
  146. return func(options *CreateOptions) {
  147. options.needTx = true
  148. }
  149. }
  150. func WithCreateCallbacks(callbacks *CreateCallbacks) CreateOption {
  151. return func(options *CreateOptions) {
  152. options.callbacks = callbacks
  153. }
  154. }
  155. func WithCreateMiddlewares(middlewares []binding.Middleware) CreateOption {
  156. return func(options *CreateOptions) {
  157. options.middlewares = middlewares
  158. }
  159. }
  160. func WithDisableDelete() DeleteOption {
  161. return func(options *DeleteOptions) {
  162. options.disable = true
  163. }
  164. }
  165. func WithDeleteTx() DeleteOption {
  166. return func(options *DeleteOptions) {
  167. options.needTx = true
  168. }
  169. }
  170. func WithDeleteCallbacks(callbacks *DeleteCallbacks) DeleteOption {
  171. return func(options *DeleteOptions) {
  172. options.callbacks = callbacks
  173. }
  174. }
  175. func WithDeleteMiddlewares(middlewares []binding.Middleware) DeleteOption {
  176. return func(options *DeleteOptions) {
  177. options.middlewares = middlewares
  178. }
  179. }
  180. func WithDisableQuery[I any]() QueryOption[I] {
  181. return func(options *QueryOptions[I]) {
  182. options.disable = true
  183. }
  184. }
  185. func WithQueryConditionFieldCallback[I any](callback ConditionFieldCallback) QueryOption[I] {
  186. return func(options *QueryOptions[I]) {
  187. options.conditionFieldCallback = callback
  188. }
  189. }
  190. func WithQueryCallbacks[I any](callbacks *QueryCallbacks[I]) QueryOption[I] {
  191. return func(options *QueryOptions[I]) {
  192. options.callbacks = callbacks
  193. }
  194. }
  195. func WithQueryMiddlewares[I any](middlewares []binding.Middleware) QueryOption[I] {
  196. return func(options *QueryOptions[I]) {
  197. options.middlewares = middlewares
  198. }
  199. }