generate.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package dto
  2. import (
  3. vd "github.com/bytedance/go-tagexpr/v2/validator"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/go-admin-team/go-admin-core/sdk/api"
  7. )
  8. type ObjectById struct {
  9. Id int `uri:"id"`
  10. Ids []int `json:"ids"`
  11. }
  12. func (s *ObjectById) Bind(ctx *gin.Context) error {
  13. var err error
  14. log := api.GetRequestLogger(ctx)
  15. err = ctx.ShouldBindUri(s)
  16. if err != nil {
  17. log.Warnf("ShouldBindUri error: %s", err.Error())
  18. return err
  19. }
  20. if ctx.Request.Method == http.MethodDelete {
  21. err = ctx.ShouldBind(&s)
  22. if err != nil {
  23. log.Warnf("ShouldBind error: %s", err.Error())
  24. return err
  25. }
  26. if len(s.Ids) > 0 {
  27. return nil
  28. }
  29. if s.Ids == nil {
  30. s.Ids = make([]int, 0)
  31. }
  32. if s.Id != 0 {
  33. s.Ids = append(s.Ids, s.Id)
  34. }
  35. }
  36. if err = vd.Validate(s); err != nil {
  37. log.Errorf("Validate error: %s", err.Error())
  38. return err
  39. }
  40. return err
  41. }
  42. func (s *ObjectById) GetId() interface{} {
  43. if len(s.Ids) > 0 {
  44. s.Ids = append(s.Ids, s.Id)
  45. return s.Ids
  46. }
  47. return s.Id
  48. }
  49. type ObjectGetReq struct {
  50. Id int `uri:"id"`
  51. }
  52. func (s *ObjectGetReq) Bind(ctx *gin.Context) error {
  53. var err error
  54. log := api.GetRequestLogger(ctx)
  55. err = ctx.ShouldBindUri(s)
  56. if err != nil {
  57. log.Warnf("ShouldBindUri error: %s", err.Error())
  58. return err
  59. }
  60. if err = vd.Validate(s); err != nil {
  61. log.Errorf("Validate error: %s", err.Error())
  62. return err
  63. }
  64. return err
  65. }
  66. func (s *ObjectGetReq) GetId() interface{} {
  67. return s.Id
  68. }
  69. type ObjectDeleteReq struct {
  70. Ids []int `json:"ids"`
  71. }
  72. func (s *ObjectDeleteReq) Bind(ctx *gin.Context) error {
  73. var err error
  74. log := api.GetRequestLogger(ctx)
  75. err = ctx.ShouldBind(&s)
  76. if err != nil {
  77. log.Warnf("ShouldBind error: %s", err.Error())
  78. return err
  79. }
  80. if len(s.Ids) > 0 {
  81. return nil
  82. }
  83. if s.Ids == nil {
  84. s.Ids = make([]int, 0)
  85. }
  86. if err = vd.Validate(s); err != nil {
  87. log.Errorf("Validate error: %s", err.Error())
  88. return err
  89. }
  90. return err
  91. }
  92. func (s *ObjectDeleteReq) GetId() interface{} {
  93. return s.Ids
  94. }