simple.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package entity
  2. import (
  3. "git.sxidc.com/go-framework/baize/api"
  4. "git.sxidc.com/go-framework/baize/binding"
  5. "git.sxidc.com/go-framework/baize/binding/request"
  6. "git.sxidc.com/go-framework/baize/binding/response"
  7. "git.sxidc.com/go-framework/baize/domain"
  8. )
  9. func BindSimple[O any](binder *binding.Binder, crud *Simple[O]) {
  10. crud.bind(binder)
  11. }
  12. // Simple 实体CRUD的Bind参数
  13. type Simple[O any] struct {
  14. // 使用的领域实体,注意是Entity类型
  15. Entity domain.Entity
  16. // 选择要使用的数据库Executor
  17. // DBExecutorOperations operations 数据库操作
  18. // DBExecutorDataService data_service 数据服务
  19. DBExecutorType string
  20. // 创建使用的请求参数
  21. CreateJsonBody request.Params
  22. // 删除使用的请求参数,注意是WithID类型
  23. DeleteQueryParams request.WithID
  24. // 更新使用的请求参数,注意是WithID类型
  25. UpdateJsonBody request.WithID
  26. // 查询使用的请求参数,注意是Query类型
  27. QueryParams request.Query
  28. // 根据ID查询使用的请求参数,注意是WithID类型
  29. GetByIDQueryParams request.WithID
  30. // 表名
  31. tableName string
  32. // URL领域相对路径,如/class,后面会自动补充
  33. domainPath string
  34. // 创建是否使用事务
  35. createNeedTx bool
  36. // 创建回调
  37. createCallbacks *Callbacks[string]
  38. // 创建中间件
  39. createMiddlewares []api.Handler
  40. // 删除是否使用事务
  41. deleteNeedTx bool
  42. // 删除回调
  43. deleteCallbacks *Callbacks[any]
  44. // 删除中间件
  45. deleteMiddlewares []api.Handler
  46. // 更新是否使用事务
  47. updateNeedTx bool
  48. // 更新回调
  49. updateCallbacks *Callbacks[any]
  50. // 更新中间件
  51. updateMiddlewares []api.Handler
  52. // 查询条件构造回调
  53. queryConditionFieldCallback ConditionFieldCallback
  54. // 查询回调
  55. queryCallbacks *Callbacks[response.InfosData[O]]
  56. // 查询中间件
  57. queryMiddlewares []api.Handler
  58. // 根据ID查询回调
  59. getByIDCallbacks *Callbacks[O]
  60. // 根据ID查询中间件
  61. getByIDMiddlewares []api.Handler
  62. }
  63. func (crud *Simple[O]) bind(binder *binding.Binder) {
  64. dbExecutor := binder.ChooseDBExecutor(crud.DBExecutorType)
  65. // 创建
  66. if !crud.CreateNeedTx {
  67. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  68. Path: crud.DomainPath + "/create",
  69. ResponseFunc: response.SendIDResponse[string],
  70. RequestParams: crud.CreateJsonBody,
  71. Objects: []domain.Object{crud.Entity},
  72. ServiceFunc: Create(crud.TableName, dbExecutor, crud.CreateCallbacks),
  73. })
  74. } else {
  75. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  76. Path: crud.DomainPath + "/create",
  77. ResponseFunc: response.SendIDResponse[string],
  78. RequestParams: crud.CreateJsonBody,
  79. Objects: []domain.Object{crud.Entity},
  80. ServiceFunc: CreateTx(crud.TableName, dbExecutor, crud.CreateCallbacks),
  81. })
  82. }
  83. // 删除
  84. if !crud.DeleteNeedTx {
  85. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  86. Path: crud.DomainPath + "/:id/delete",
  87. ResponseFunc: response.SendMsgResponse,
  88. RequestParams: crud.DeleteQueryParams,
  89. Objects: []domain.Object{crud.Entity},
  90. ServiceFunc: Delete(crud.TableName, dbExecutor, crud.DeleteCallbacks),
  91. })
  92. } else {
  93. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  94. Path: crud.DomainPath + "/:id/delete",
  95. ResponseFunc: response.SendMsgResponse,
  96. RequestParams: crud.DeleteQueryParams,
  97. Objects: []domain.Object{crud.Entity},
  98. ServiceFunc: DeleteTx(crud.TableName, dbExecutor, crud.DeleteCallbacks),
  99. })
  100. }
  101. // 修改
  102. if !crud.UpdateNeedTx {
  103. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  104. Path: crud.DomainPath + "/update",
  105. ResponseFunc: response.SendMsgResponse,
  106. RequestParams: crud.UpdateJsonBody,
  107. Objects: []domain.Object{crud.Entity},
  108. ServiceFunc: Update(crud.TableName, dbExecutor, crud.UpdateCallbacks),
  109. })
  110. } else {
  111. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  112. Path: crud.DomainPath + "/update",
  113. ResponseFunc: response.SendMsgResponse,
  114. RequestParams: crud.UpdateJsonBody,
  115. Objects: []domain.Object{crud.Entity},
  116. ServiceFunc: UpdateTx(crud.TableName, dbExecutor, crud.UpdateCallbacks),
  117. })
  118. }
  119. // 查询
  120. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[O]]{
  121. Path: crud.DomainPath + "/query",
  122. ResponseFunc: response.SendInfosResponse[O],
  123. RequestParams: crud.QueryParams,
  124. Objects: []domain.Object{crud.Entity},
  125. ServiceFunc: Query[O](crud.TableName, dbExecutor, crud.QueryCallbacks, crud.QueryConditionFieldCallback),
  126. })
  127. // 通过ID获取
  128. binding.GetBind(binder, &binding.SimpleBindItem[O]{
  129. Path: crud.DomainPath + "/get",
  130. ResponseFunc: response.SendInfoResponse[O],
  131. RequestParams: crud.GetByIDQueryParams,
  132. Objects: []domain.Object{crud.Entity},
  133. ServiceFunc: GetByID[O](crud.TableName, dbExecutor, crud.GetByIDCallbacks),
  134. })
  135. }
  136. type Option[O any] func(simple Simple[O])