favorite.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package http
  2. import (
  3. "time"
  4. "go-common/app/interface/main/esports/model"
  5. "go-common/library/ecode"
  6. bm "go-common/library/net/http/blademaster"
  7. )
  8. func addFav(c *bm.Context) {
  9. midStr, _ := c.Get("mid")
  10. mid := midStr.(int64)
  11. v := new(struct {
  12. Cid int64 `form:"cid" validate:"min=1"`
  13. })
  14. if err := c.Bind(v); err != nil {
  15. return
  16. }
  17. c.JSON(nil, switchCode(eSvc.AddFav(c, mid, v.Cid)))
  18. }
  19. func delFav(c *bm.Context) {
  20. midStr, _ := c.Get("mid")
  21. mid := midStr.(int64)
  22. v := new(struct {
  23. Cid int64 `form:"cid" validate:"min=1"`
  24. })
  25. if err := c.Bind(v); err != nil {
  26. return
  27. }
  28. c.JSON(nil, switchCode(eSvc.DelFav(c, mid, v.Cid)))
  29. }
  30. func listFav(c *bm.Context) {
  31. var (
  32. mid int64
  33. total int
  34. contest []*model.Contest
  35. err error
  36. )
  37. if midStr, ok := c.Get("mid"); ok {
  38. mid = midStr.(int64)
  39. }
  40. v := new(struct {
  41. VMID int64 `form:"vmid"`
  42. Pn int `form:"pn" default:"1" validate:"min=1"`
  43. Ps int `form:"ps" default:"5" validate:"min=1"`
  44. })
  45. if err = c.Bind(v); err != nil {
  46. return
  47. }
  48. if contest, total, err = eSvc.ListFav(c, mid, v.VMID, v.Pn, v.Ps); err != nil {
  49. c.JSON(nil, err)
  50. return
  51. }
  52. data := make(map[string]interface{}, 2)
  53. page := map[string]int{
  54. "num": v.Pn,
  55. "size": v.Ps,
  56. "total": total,
  57. }
  58. data["page"] = page
  59. data["list"] = contest
  60. c.JSON(data, nil)
  61. }
  62. func appListFav(c *bm.Context) {
  63. var (
  64. mid int64
  65. total int
  66. contest []*model.Contest
  67. err error
  68. )
  69. if midStr, ok := c.Get("mid"); ok {
  70. mid = midStr.(int64)
  71. }
  72. v := new(model.ParamFav)
  73. if err = c.Bind(v); err != nil {
  74. return
  75. }
  76. if mid == 0 && v.VMID == 0 {
  77. c.JSON(nil, ecode.RequestErr)
  78. return
  79. }
  80. if v.Stime != "" {
  81. if _, err = time.Parse("2006-01-02", v.Stime); err != nil {
  82. c.JSON(nil, ecode.RequestErr)
  83. return
  84. }
  85. }
  86. if v.Etime != "" {
  87. if _, err = time.Parse("2006-01-02", v.Etime); err != nil {
  88. c.JSON(nil, ecode.RequestErr)
  89. return
  90. }
  91. }
  92. if contest, total, err = eSvc.ListAppFav(c, mid, v); err != nil {
  93. c.JSON(nil, err)
  94. return
  95. }
  96. data := make(map[string]interface{}, 2)
  97. page := map[string]int{
  98. "num": v.Pn,
  99. "size": v.Ps,
  100. "total": total,
  101. }
  102. data["page"] = page
  103. data["list"] = contest
  104. c.JSON(data, nil)
  105. }
  106. func seasonFav(c *bm.Context) {
  107. var (
  108. mid int64
  109. total int
  110. seasons []*model.Season
  111. err error
  112. )
  113. if midStr, ok := c.Get("mid"); ok {
  114. mid = midStr.(int64)
  115. }
  116. v := new(model.ParamSeason)
  117. if err = c.Bind(v); err != nil {
  118. return
  119. }
  120. if mid == 0 && v.VMID == 0 {
  121. c.JSON(nil, ecode.RequestErr)
  122. return
  123. }
  124. if seasons, total, err = eSvc.SeasonFav(c, mid, v); err != nil {
  125. c.JSON(nil, err)
  126. return
  127. }
  128. data := make(map[string]interface{}, 2)
  129. page := map[string]int{
  130. "num": v.Pn,
  131. "size": v.Ps,
  132. "total": total,
  133. }
  134. data["page"] = page
  135. data["list"] = seasons
  136. c.JSON(data, nil)
  137. }
  138. func stimeFav(c *bm.Context) {
  139. var (
  140. mid int64
  141. total int
  142. stimes []string
  143. err error
  144. )
  145. if midStr, ok := c.Get("mid"); ok {
  146. mid = midStr.(int64)
  147. }
  148. v := new(model.ParamSeason)
  149. if err = c.Bind(v); err != nil {
  150. return
  151. }
  152. if mid == 0 && v.VMID == 0 {
  153. c.JSON(nil, ecode.RequestErr)
  154. return
  155. }
  156. if stimes, total, err = eSvc.StimeFav(c, mid, v); err != nil {
  157. c.JSON(nil, err)
  158. return
  159. }
  160. data := make(map[string]interface{}, 2)
  161. page := map[string]int{
  162. "num": v.Pn,
  163. "size": v.Ps,
  164. "total": total,
  165. }
  166. data["page"] = page
  167. data["list"] = stimes
  168. c.JSON(data, nil)
  169. }
  170. func switchCode(err error) error {
  171. if err == nil {
  172. return err
  173. }
  174. switch ecode.Cause(err) {
  175. case ecode.FavResourceOverflow:
  176. err = ecode.EsportsContestMaxCount
  177. case ecode.FavResourceAlreadyDel:
  178. err = ecode.EsportsContestFavDel
  179. case ecode.FavResourceExist:
  180. err = ecode.EsportsContestFavExist
  181. case ecode.FavFolderNotExist:
  182. err = ecode.EsportsContestNotExist
  183. }
  184. return err
  185. }