thumbup.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package http
  2. import (
  3. "go-common/app/service/main/thumbup/model"
  4. "go-common/library/ecode"
  5. bm "go-common/library/net/http/blademaster"
  6. "go-common/library/net/http/blademaster/binding"
  7. "go-common/library/net/metadata"
  8. )
  9. func like(c *bm.Context) {
  10. var (
  11. err error
  12. )
  13. v := new(struct {
  14. Business string `form:"business" validate:"required"`
  15. OriginID int64 `form:"origin_id" validate:"min=0"`
  16. MessageID int64 `form:"message_id" validate:"min=1,required"`
  17. Type int8 `form:"type" validate:"required"`
  18. Mid int64 `form:"mid" validate:"min=1,required"`
  19. UpMid int64 `form:"up_mid" validate:"omitempty,min=1"`
  20. })
  21. if err = c.Bind(v); err != nil {
  22. c.JSON(nil, ecode.RequestErr)
  23. return
  24. }
  25. c.JSON(likeSrv.Like(c, v.Business, v.Mid, v.OriginID, v.MessageID, v.Type, v.UpMid))
  26. }
  27. func hasLike(c *bm.Context) {
  28. v := new(struct {
  29. Business string `form:"business" validate:"required"`
  30. OriginID int64 `form:"origin_id" validate:"min=0"`
  31. MessageIDs []int64 `form:"message_ids,split" validate:"required"`
  32. Mid int64 `form:"mid" validate:"min=1,required"`
  33. })
  34. if err := c.Bind(v); err != nil {
  35. c.JSON(nil, ecode.RequestErr)
  36. return
  37. }
  38. res, _, err := likeSrv.HasLike(c, v.Business, v.Mid, v.MessageIDs)
  39. c.JSON(res, err)
  40. }
  41. func stats(c *bm.Context) {
  42. v := new(struct {
  43. Business string `form:"business" validate:"required"`
  44. OriginID int64 `form:"origin_id" validate:"min=0"`
  45. MessageIDs []int64 `form:"message_ids,split" validate:"required"`
  46. Mid int64 `form:"mid"`
  47. })
  48. if err := c.Bind(v); err != nil {
  49. c.JSON(nil, ecode.RequestErr)
  50. return
  51. }
  52. if v.Mid > 0 {
  53. c.JSON(likeSrv.StatsWithLike(c, v.Business, v.Mid, v.OriginID, v.MessageIDs))
  54. return
  55. }
  56. c.JSON(likeSrv.Stats(c, v.Business, v.OriginID, v.MessageIDs))
  57. }
  58. func userLikes(c *bm.Context) {
  59. var (
  60. err error
  61. data []*model.ItemLikeRecord
  62. )
  63. v := new(struct {
  64. Business string `form:"business" validate:"required"`
  65. Type string `form:"type" validate:"required"`
  66. Mid int64 `form:"mid" validate:"min=1,required"`
  67. Pn int `form:"pn" default:"1" validate:"omitempty,min=1"`
  68. Ps int `form:"ps" default:"20" validate:"omitempty,min=1"`
  69. })
  70. if err = c.Bind(v); err != nil {
  71. c.JSON(nil, ecode.RequestErr)
  72. return
  73. }
  74. if v.Type == "like" {
  75. data, err = likeSrv.UserLikes(c, v.Business, v.Mid, v.Pn, v.Ps)
  76. } else {
  77. data, err = likeSrv.UserDislikes(c, v.Business, v.Mid, v.Pn, v.Ps)
  78. }
  79. if data == nil {
  80. data = make([]*model.ItemLikeRecord, 0)
  81. }
  82. c.JSON(data, err)
  83. }
  84. func itemLikes(c *bm.Context) {
  85. var (
  86. err error
  87. data []*model.UserLikeRecord
  88. )
  89. v := new(struct {
  90. Business string `form:"business" validate:"required"`
  91. OriginID int64 `form:"origin_id" validate:"min=0"`
  92. MessageID int64 `form:"message_id" validate:"min=1,required"`
  93. Mid int64 `form:"mid" validate:"omitempty,min=1"`
  94. Type string `form:"type" validate:"required"`
  95. Pn int `form:"pn" default:"1" validate:"omitempty,min=1"`
  96. Ps int `form:"ps" default:"20" validate:"omitempty,min=1"`
  97. })
  98. if err = c.Bind(v); err != nil {
  99. c.JSON(nil, ecode.RequestErr)
  100. return
  101. }
  102. if v.Type == "like" {
  103. data, err = likeSrv.ItemLikes(c, v.Business, v.OriginID, v.MessageID, v.Pn, v.Ps, v.Mid)
  104. } else {
  105. data, err = likeSrv.ItemDislikes(c, v.Business, v.OriginID, v.MessageID, v.Pn, v.Ps, v.Mid)
  106. }
  107. if data == nil {
  108. data = make([]*model.UserLikeRecord, 0)
  109. }
  110. c.JSON(data, err)
  111. }
  112. func multiStats(c *bm.Context) {
  113. v := new(model.MultiBusiness)
  114. if err := c.BindWith(v, binding.JSON); err != nil {
  115. c.JSON(nil, ecode.RequestErr)
  116. return
  117. }
  118. c.JSON(likeSrv.MultiStatsWithLike(c, v))
  119. }
  120. func updateCount(c *bm.Context) {
  121. v := new(struct {
  122. Business string `form:"business" validate:"required"`
  123. OriginID int64 `form:"origin_id" validate:"min=0"`
  124. MessageID int64 `form:"message_id" validate:"min=1,required"`
  125. LikeChange int64 `form:"like_change"`
  126. DislikeChange int64 `form:"dislike_change"`
  127. Operator string `form:"operator" validate:"required"`
  128. })
  129. if err := c.Bind(v); err != nil {
  130. c.JSON(nil, ecode.RequestErr)
  131. return
  132. }
  133. ip := metadata.String(c, metadata.RemoteIP)
  134. c.JSON(nil, likeSrv.UpdateCount(c, v.Business, v.OriginID, v.MessageID, v.LikeChange, v.DislikeChange, ip, v.Operator))
  135. }
  136. func rawStats(c *bm.Context) {
  137. v := new(struct {
  138. Business string `form:"business" validate:"required"`
  139. OriginID int64 `form:"origin_id" validate:"min=0"`
  140. MessageID int64 `form:"message_id" validate:"min=1,required"`
  141. })
  142. if err := c.Bind(v); err != nil {
  143. c.JSON(nil, ecode.RequestErr)
  144. return
  145. }
  146. c.JSON(likeSrv.RawStats(c, v.Business, v.OriginID, v.MessageID))
  147. }
  148. func updateUpMids(c *bm.Context) {
  149. v := new(struct {
  150. Business string `json:"business" validate:"required"`
  151. Data []*model.UpMidsReq `json:"data" validate:"required,gte=1,lte=100"`
  152. })
  153. if err := c.BindWith(v, binding.JSON); err != nil {
  154. c.JSON(nil, ecode.RequestErr)
  155. return
  156. }
  157. c.JSON(nil, likeSrv.UpdateUpMids(c, v.Business, v.Data))
  158. }
  159. func itemHasLike(c *bm.Context) {
  160. v := new(struct {
  161. Business string `json:"business" validate:"required"`
  162. OriginID int64 `json:"origin_id" validate:"min=0"`
  163. MessageID int64 `json:"message_id" validate:"min=1"`
  164. Mids []int64 `json:"mids,split" validate:"required"`
  165. })
  166. if err := c.BindWith(v, binding.JSON); err != nil {
  167. c.JSON(nil, ecode.RequestErr)
  168. return
  169. }
  170. c.JSON(likeSrv.ItemHasLike(c, v.Business, v.OriginID, v.MessageID, v.Mids))
  171. }