api.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package favorite
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/library/ecode"
  7. "go-common/library/net/metadata"
  8. "github.com/pkg/errors"
  9. )
  10. // IsFavDefault faorite count
  11. func (d *Dao) IsFavDefault(c context.Context, mid, aid int64) (is bool, err error) {
  12. ip := metadata.String(c, metadata.RemoteIP)
  13. params := url.Values{}
  14. params.Set("mid", strconv.FormatInt(mid, 10))
  15. params.Set("aid", strconv.FormatInt(aid, 10))
  16. var res struct {
  17. Code int `json:"code"`
  18. Data struct {
  19. Default bool `json:"default"`
  20. } `json:"data"`
  21. }
  22. if err = d.client.Get(c, d.isFavDef, ip, params, &res); err != nil {
  23. return
  24. }
  25. if res.Code != ecode.OK.Code() {
  26. err = errors.Wrap(ecode.Int(res.Code), d.isFavDef+"?"+params.Encode())
  27. return
  28. }
  29. is = res.Data.Default
  30. return
  31. }
  32. // IsFav is
  33. func (d *Dao) IsFav(c context.Context, mid, aid int64) (is bool, err error) {
  34. ip := metadata.String(c, metadata.RemoteIP)
  35. params := url.Values{}
  36. params.Set("mid", strconv.FormatInt(mid, 10))
  37. params.Set("aid", strconv.FormatInt(aid, 10))
  38. var res struct {
  39. Code int `json:"code"`
  40. Data struct {
  41. Favorite bool `json:"favoured"`
  42. } `json:"data"`
  43. }
  44. if err = d.client.Get(c, d.isFav, ip, params, &res); err != nil {
  45. return
  46. }
  47. if res.Code != ecode.OK.Code() {
  48. err = errors.Wrap(ecode.Int(res.Code), d.isFav+"?"+params.Encode())
  49. return
  50. }
  51. is = res.Data.Favorite
  52. return
  53. }
  54. // AddFav add fav video
  55. func (d *Dao) AddFav(c context.Context, mid, aid int64) (err error) {
  56. ip := metadata.String(c, metadata.RemoteIP)
  57. params := url.Values{}
  58. params.Set("mid", strconv.FormatInt(mid, 10))
  59. params.Set("aid", strconv.FormatInt(aid, 10))
  60. var res struct {
  61. Code int `json:"code"`
  62. }
  63. if err = d.client.Post(c, d.addFav, ip, params, &res); err != nil {
  64. return
  65. }
  66. if res.Code != ecode.OK.Code() {
  67. err = errors.Wrap(ecode.Int(res.Code), d.addFav+"?"+params.Encode())
  68. return
  69. }
  70. return
  71. }