simple.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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, globalOptions.scopeFields...),
  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. scopeFields []string
  102. }
  103. type CreateOptions struct {
  104. // 关闭创建
  105. disable bool
  106. // 创建是否使用事务
  107. needTx bool
  108. // 创建回调
  109. callbacks *CreateCallbacks
  110. // 创建中间件
  111. middlewares []binding.Middleware
  112. }
  113. type DeleteOptions struct {
  114. // 关闭删除
  115. disable bool
  116. // 删除是否使用事务
  117. needTx bool
  118. // 删除回调
  119. callbacks *DeleteCallbacks
  120. // 删除中间件
  121. middlewares []binding.Middleware
  122. }
  123. type QueryOptions[I any] struct {
  124. // 关闭查询
  125. disable bool
  126. // 查询条件构造回调
  127. conditionFieldCallback ConditionFieldCallback
  128. // 自定义添加构造函数
  129. formCustomConditionFunc FormCustomConditionFunc
  130. // 查询回调
  131. callbacks *QueryCallbacks[I]
  132. // 查询中间件
  133. middlewares []binding.Middleware
  134. // OrderBy
  135. orderBy string
  136. }
  137. func WithGlobalMiddlewares(middlewares ...binding.Middleware) GlobalOption {
  138. return func(options *GlobalOptions) {
  139. options.middlewares = middlewares
  140. }
  141. }
  142. func WithGlobalScopeFields(scopeFields ...string) GlobalOption {
  143. return func(options *GlobalOptions) {
  144. options.scopeFields = scopeFields
  145. }
  146. }
  147. func WithDisableCreate() CreateOption {
  148. return func(options *CreateOptions) {
  149. options.disable = true
  150. }
  151. }
  152. func WithCreateTx() CreateOption {
  153. return func(options *CreateOptions) {
  154. options.needTx = true
  155. }
  156. }
  157. func WithCreateCallbacks(callbacks *CreateCallbacks) CreateOption {
  158. return func(options *CreateOptions) {
  159. options.callbacks = callbacks
  160. }
  161. }
  162. func WithCreateMiddlewares(middlewares ...binding.Middleware) CreateOption {
  163. return func(options *CreateOptions) {
  164. options.middlewares = middlewares
  165. }
  166. }
  167. func WithDisableDelete() DeleteOption {
  168. return func(options *DeleteOptions) {
  169. options.disable = true
  170. }
  171. }
  172. func WithDeleteTx() DeleteOption {
  173. return func(options *DeleteOptions) {
  174. options.needTx = true
  175. }
  176. }
  177. func WithDeleteCallbacks(callbacks *DeleteCallbacks) DeleteOption {
  178. return func(options *DeleteOptions) {
  179. options.callbacks = callbacks
  180. }
  181. }
  182. func WithDeleteMiddlewares(middlewares ...binding.Middleware) DeleteOption {
  183. return func(options *DeleteOptions) {
  184. options.middlewares = middlewares
  185. }
  186. }
  187. func WithDisableQuery[I any]() QueryOption[I] {
  188. return func(options *QueryOptions[I]) {
  189. options.disable = true
  190. }
  191. }
  192. func WithQueryConditionFieldCallback[I any](callback ConditionFieldCallback) QueryOption[I] {
  193. return func(options *QueryOptions[I]) {
  194. options.conditionFieldCallback = callback
  195. }
  196. }
  197. func WithQueryFormCustomConditionFunc[I any](formCustomConditionFunc FormCustomConditionFunc) QueryOption[I] {
  198. return func(options *QueryOptions[I]) {
  199. options.formCustomConditionFunc = formCustomConditionFunc
  200. }
  201. }
  202. func WithQueryCallbacks[I any](callbacks *QueryCallbacks[I]) QueryOption[I] {
  203. return func(options *QueryOptions[I]) {
  204. options.callbacks = callbacks
  205. }
  206. }
  207. func WithQueryMiddlewares[I any](middlewares ...binding.Middleware) QueryOption[I] {
  208. return func(options *QueryOptions[I]) {
  209. options.middlewares = middlewares
  210. }
  211. }
  212. func WithQueryOrderBy[I any](orderBy string) QueryOption[I] {
  213. return func(options *QueryOptions[I]) {
  214. options.orderBy = orderBy
  215. }
  216. }