simple.go 6.7 KB

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