simple.go 4.3 KB

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