dao.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package favorite
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "go-common/app/interface/main/tv/conf"
  7. "go-common/app/interface/main/tv/model"
  8. favrpc "go-common/app/service/main/favorite/api/gorpc"
  9. favmdl "go-common/app/service/main/favorite/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/metadata"
  14. "github.com/pkg/errors"
  15. )
  16. // Dao is account dao.
  17. type Dao struct {
  18. favRPC *favrpc.Service // rpc
  19. conf *conf.Config
  20. client *bm.Client
  21. }
  22. // New account dao.
  23. func New(c *conf.Config) (d *Dao) {
  24. d = &Dao{
  25. favRPC: favrpc.New2(c.FavoriteRPC),
  26. conf: c,
  27. client: bm.NewClient(c.HTTPClient),
  28. }
  29. return
  30. }
  31. const (
  32. _FavBusiness = 2
  33. _DefaultFav = 0
  34. )
  35. // FavoriteV3 picks favorite info from rpc
  36. func (d *Dao) FavoriteV3(ctx context.Context, mid int64, pn int) (res *favmdl.Favorites, err error) {
  37. var ip = metadata.String(ctx, metadata.RemoteIP)
  38. arg := &favmdl.ArgFavs{
  39. Type: _FavBusiness,
  40. Mid: mid,
  41. Fid: _DefaultFav,
  42. Tv: 1,
  43. Pn: pn,
  44. Ps: d.conf.Cfg.FavPs,
  45. RealIP: ip,
  46. }
  47. if res, err = d.favRPC.Favorites(ctx, arg); err != nil {
  48. err = errors.Wrapf(err, "%v", arg)
  49. }
  50. return
  51. }
  52. // favAct adds/deletes favorite into/from the default folder
  53. func (d *Dao) favAct(ctx context.Context, mid int64, aid int64, host string) (err error) {
  54. var (
  55. ip = metadata.String(ctx, metadata.RemoteIP)
  56. params = url.Values{}
  57. res = model.RespFavAct{}
  58. )
  59. params.Set("mid", fmt.Sprintf("%d", mid))
  60. params.Set("aid", fmt.Sprintf("%d", aid))
  61. if err = d.client.Post(ctx, host, ip, params, &res); err != nil {
  62. log.Error("FavAdd Aid %d, Mid %d, Err %v", aid, mid, err)
  63. return
  64. }
  65. if res.Code != ecode.OK.Code() {
  66. err = errors.Wrap(ecode.Int(res.Code), fmt.Sprintf("Fav AID %d, Mid %d, API Error %s", aid, mid, res.Message))
  67. log.Error("FavAdd ERROR:%v, URL: %s", err, host+"?"+params.Encode())
  68. return
  69. }
  70. return
  71. }
  72. // FavAdd def.
  73. func (d *Dao) FavAdd(ctx context.Context, mid, aid int64) (err error) {
  74. host := d.conf.Host.FavAdd
  75. return d.favAct(ctx, mid, aid, host)
  76. }
  77. // FavDel deletes favorite from the default folder
  78. func (d *Dao) FavDel(ctx context.Context, mid int64, aid int64) (err error) {
  79. host := d.conf.Host.FavDel
  80. return d.favAct(ctx, mid, aid, host)
  81. }
  82. // InDefault returns whether the aid is in Default of Mid
  83. func (d *Dao) InDefault(ctx context.Context, mid int64, aid int64) (bool, error) {
  84. var ip = metadata.String(ctx, metadata.RemoteIP)
  85. arg := &favmdl.ArgInDefaultFolder{
  86. Type: _FavBusiness,
  87. Mid: mid,
  88. RealIP: ip,
  89. Oid: aid,
  90. }
  91. return d.favRPC.InDefault(ctx, arg)
  92. }