simple.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package entity_crud
  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. // 删除使用的DTO,注意是WithID类型
  28. DeleteDTO request.WithID
  29. // 删除是否使用事务
  30. DeleteNeedTx bool
  31. // 更新使用的DTO,注意是WithID类型
  32. UpdateDTO request.WithID
  33. // 根性是否使用事务
  34. UpdateNeedTx bool
  35. // 查询使用的DTO,注意是Query类型
  36. QueryDTO request.Query
  37. // 根据ID查询使用的DTO,注意是WithID类型
  38. QueryByIDDTO request.WithID
  39. }
  40. func (crud *Simple[O]) bind(binder *binding.Binder) {
  41. dbExecutor := binder.ChooseDBExecutor(crud.DBExecutorType)
  42. // 创建班级
  43. if !crud.CreateNeedTx {
  44. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  45. Path: crud.DomainPath + "/create",
  46. ResponseFunc: response.SendIDResponse[string],
  47. DTO: crud.CreateDTO,
  48. Objects: []domain.Object{crud.Entity},
  49. ServiceFunc: CommonEntityCreate(crud.TableName, dbExecutor, nil),
  50. })
  51. } else {
  52. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  53. Path: crud.DomainPath + "/create",
  54. ResponseFunc: response.SendIDResponse[string],
  55. DTO: crud.CreateDTO,
  56. Objects: []domain.Object{crud.Entity},
  57. ServiceFunc: CommonEntityCreateTx(crud.TableName, dbExecutor, nil),
  58. })
  59. }
  60. // 删除班级
  61. if !crud.DeleteNeedTx {
  62. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  63. Path: crud.DomainPath + "/:id/delete",
  64. ResponseFunc: response.SendMsgResponse,
  65. DTO: crud.DeleteDTO,
  66. Objects: []domain.Object{crud.Entity},
  67. ServiceFunc: CommonEntityDelete(crud.TableName, dbExecutor, nil),
  68. })
  69. } else {
  70. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  71. Path: crud.DomainPath + "/:id/delete",
  72. ResponseFunc: response.SendMsgResponse,
  73. DTO: crud.DeleteDTO,
  74. Objects: []domain.Object{crud.Entity},
  75. ServiceFunc: CommonEntityDeleteTx(crud.TableName, dbExecutor, nil),
  76. })
  77. }
  78. // 修改班级
  79. if !crud.UpdateNeedTx {
  80. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  81. Path: crud.DomainPath + "/update",
  82. ResponseFunc: response.SendMsgResponse,
  83. DTO: crud.UpdateDTO,
  84. Objects: []domain.Object{crud.Entity},
  85. ServiceFunc: CommonEntityUpdate(crud.TableName, dbExecutor, nil),
  86. })
  87. } else {
  88. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  89. Path: crud.DomainPath + "/update",
  90. ResponseFunc: response.SendMsgResponse,
  91. DTO: crud.UpdateDTO,
  92. Objects: []domain.Object{crud.Entity},
  93. ServiceFunc: CommonEntityUpdateTx(crud.TableName, dbExecutor, nil),
  94. })
  95. }
  96. // 查询班级
  97. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[O]]{
  98. Path: crud.DomainPath + "/query",
  99. ResponseFunc: response.SendInfosResponse[O],
  100. DTO: crud.QueryDTO,
  101. Objects: []domain.Object{crud.Entity},
  102. ServiceFunc: CommonEntityQuery[O](crud.TableName, dbExecutor, nil, nil),
  103. })
  104. // 通过ID获取班级
  105. binding.GetBind(binder, &binding.SimpleBindItem[O]{
  106. Path: crud.DomainPath + "/get",
  107. ResponseFunc: response.SendInfoResponse[O],
  108. DTO: crud.QueryByIDDTO,
  109. Objects: []domain.Object{crud.Entity},
  110. ServiceFunc: CommonEntityQueryByID[O](crud.TableName, dbExecutor, nil),
  111. })
  112. }