simple.go 7.2 KB

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