simple.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // 删除使用的DTO,注意是WithID类型
  26. DeleteDTO request.WithID
  27. // 更新使用的DTO,注意是WithID类型
  28. UpdateDTO request.WithID
  29. // 查询使用的DTO,注意是Query类型
  30. QueryDTO request.Query
  31. // 根据ID查询使用的DTO,注意是WithID类型
  32. QueryByIDDTO request.WithID
  33. }
  34. func (crud *Simple[O]) bind(binder *binding.Binder) {
  35. dbExecutor := binder.ChooseDBExecutor(crud.DBExecutorType)
  36. // 创建班级
  37. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  38. Path: crud.DomainPath + "/create",
  39. ResponseFunc: response.SendIDResponse[string],
  40. DTO: crud.CreateDTO,
  41. Objects: []domain.Object{crud.Entity},
  42. ServiceFunc: CommonEntityCreate(crud.TableName, dbExecutor, nil),
  43. })
  44. // 删除班级
  45. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  46. Path: crud.DomainPath + "/:id/delete",
  47. ResponseFunc: response.SendMsgResponse,
  48. DTO: crud.DeleteDTO,
  49. Objects: []domain.Object{crud.Entity},
  50. ServiceFunc: CommonEntityDelete(crud.TableName, dbExecutor, nil),
  51. })
  52. // 修改班级
  53. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  54. Path: crud.DomainPath + "/update",
  55. ResponseFunc: response.SendMsgResponse,
  56. DTO: crud.UpdateDTO,
  57. Objects: []domain.Object{crud.Entity},
  58. ServiceFunc: CommonEntityUpdate(crud.TableName, dbExecutor, nil),
  59. })
  60. // 查询班级
  61. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[O]]{
  62. Path: crud.DomainPath + "/query",
  63. ResponseFunc: response.SendInfosResponse[O],
  64. DTO: crud.QueryDTO,
  65. Objects: []domain.Object{crud.Entity},
  66. ServiceFunc: CommonEntityQuery[O](crud.TableName, dbExecutor, nil, nil),
  67. })
  68. // 通过ID获取班级
  69. binding.GetBind(binder, &binding.SimpleBindItem[O]{
  70. Path: crud.DomainPath + "/get",
  71. ResponseFunc: response.SendInfoResponse[O],
  72. DTO: crud.QueryByIDDTO,
  73. Objects: []domain.Object{crud.Entity},
  74. ServiceFunc: CommonEntityQueryByID[O](crud.TableName, dbExecutor, nil),
  75. })
  76. }