cond.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package http
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "go-common/app/service/main/antispam/model"
  7. "go-common/app/service/main/antispam/service"
  8. "go-common/app/service/main/antispam/util"
  9. )
  10. // Condition .
  11. type Condition struct {
  12. *util.Pagination
  13. Tag string
  14. Tags []string
  15. Content string
  16. Contents []string
  17. Area string
  18. Search string
  19. State string
  20. HitCounts string
  21. Order, OrderBy string
  22. LimitType, LimitScope string
  23. StartTime, EndTime *time.Time
  24. }
  25. // ToServiceCond .
  26. func ToServiceCond(cond *Condition) *service.Condition {
  27. if cond == nil {
  28. return nil
  29. }
  30. res := &service.Condition{
  31. Pagination: cond.Pagination,
  32. Area: cond.Area,
  33. Order: cond.Order,
  34. OrderBy: cond.OrderBy,
  35. Tags: cond.Tags,
  36. Contents: cond.Contents,
  37. Search: cond.Search,
  38. State: cond.State,
  39. HitCounts: cond.HitCounts,
  40. LimitType: cond.LimitType,
  41. LimitScope: cond.LimitScope,
  42. StartTime: cond.StartTime,
  43. EndTime: cond.EndTime,
  44. }
  45. // TODO: how to handler it graceful ?
  46. if cond.Tag != "" {
  47. res.Tags = []string{cond.Tag}
  48. }
  49. if cond.Content != "" {
  50. res.Contents = []string{cond.Content}
  51. }
  52. // history reasons
  53. if res.OrderBy == "show_up_counts" {
  54. res.OrderBy = "hit_counts"
  55. }
  56. return res
  57. }
  58. // Valid .
  59. func (c *Condition) Valid() error {
  60. if c.Pagination != nil {
  61. if c.CurPage == 0 {
  62. c.CurPage = 1
  63. }
  64. if c.PerPage == 0 {
  65. c.PerPage = 20
  66. }
  67. }
  68. c.Search, c.Order = strings.TrimSpace(c.Search), strings.TrimSpace(c.Order)
  69. if c.Order == "" {
  70. c.Order = model.OrderASC
  71. } else {
  72. c.Order = strings.ToUpper(c.Order)
  73. }
  74. if c.Order != model.OrderASC && c.Order != model.OrderDESC {
  75. return fmt.Errorf("Order by should be 'ASC' or 'DESC' but got(%s)", c.Order)
  76. }
  77. return nil
  78. }