dao.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package upper
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/app-feed/conf"
  6. article "go-common/app/interface/openplatform/article/model"
  7. feed "go-common/app/service/main/feed/model"
  8. feedrpc "go-common/app/service/main/feed/rpc/client"
  9. "go-common/library/cache/redis"
  10. "go-common/library/ecode"
  11. "go-common/library/net/metadata"
  12. "github.com/pkg/errors"
  13. )
  14. // Dao is feed dao.
  15. type Dao struct {
  16. // rpc
  17. feedRPC *feedrpc.Service
  18. // redis
  19. redis *redis.Pool
  20. expireRds int32
  21. }
  22. // New new a archive dao.
  23. func New(c *conf.Config) (d *Dao) {
  24. d = &Dao{
  25. // rpc
  26. feedRPC: feedrpc.New(c.FeedRPC),
  27. // redis init
  28. redis: redis.NewPool(c.Redis.Upper.Config),
  29. expireRds: int32(time.Duration(c.Redis.Upper.ExpireUpper) / time.Second),
  30. }
  31. return
  32. }
  33. // Ping check redis connection
  34. func (d *Dao) Ping(c context.Context) (err error) {
  35. conn := d.redis.Get(c)
  36. _, err = conn.Do("SET", "PING", "PONG")
  37. conn.Close()
  38. return
  39. }
  40. func (d *Dao) Feed(c context.Context, mid int64, pn, ps int) (fs []*feed.Feed, err error) {
  41. ip := metadata.String(c, metadata.RemoteIP)
  42. arg := &feed.ArgFeed{Mid: mid, Pn: pn, Ps: ps, RealIP: ip}
  43. if fs, err = d.feedRPC.AppFeed(c, arg); err != nil {
  44. if err == ecode.NothingFound {
  45. err = nil
  46. return
  47. }
  48. err = errors.Wrapf(err, "%v", arg)
  49. }
  50. return
  51. }
  52. func (d *Dao) ArchiveFeed(c context.Context, mid int64, pn, ps int) (fs []*feed.Feed, err error) {
  53. ip := metadata.String(c, metadata.RemoteIP)
  54. arg := &feed.ArgFeed{Mid: mid, Pn: pn, Ps: ps, RealIP: ip}
  55. if fs, err = d.feedRPC.ArchiveFeed(c, arg); err != nil {
  56. if err == ecode.NothingFound {
  57. err = nil
  58. return
  59. }
  60. err = errors.Wrapf(err, "%v", arg)
  61. }
  62. return
  63. }
  64. func (d *Dao) BangumiFeed(c context.Context, mid int64, pn, ps int) (fs []*feed.Feed, err error) {
  65. ip := metadata.String(c, metadata.RemoteIP)
  66. arg := &feed.ArgFeed{Mid: mid, Pn: pn, Ps: ps, RealIP: ip}
  67. if fs, err = d.feedRPC.BangumiFeed(c, arg); err != nil {
  68. if err == ecode.NothingFound {
  69. err = nil
  70. return
  71. }
  72. err = errors.Wrapf(err, "%v", arg)
  73. }
  74. return
  75. }
  76. func (d *Dao) Recent(c context.Context, mid, aid int64) (fs []*feed.Feed, err error) {
  77. ip := metadata.String(c, metadata.RemoteIP)
  78. arg := &feed.ArgFold{Mid: mid, Aid: aid, RealIP: ip}
  79. if fs, err = d.feedRPC.Fold(c, arg); err != nil {
  80. if err == ecode.NothingFound {
  81. err = nil
  82. return
  83. }
  84. err = errors.Wrapf(err, "%v", arg)
  85. }
  86. return
  87. }
  88. func (d *Dao) AppUnreadCount(c context.Context, mid int64, withoutBangumi bool) (unread int, err error) {
  89. ip := metadata.String(c, metadata.RemoteIP)
  90. arg := &feed.ArgUnreadCount{Mid: mid, WithoutBangumi: withoutBangumi, RealIP: ip}
  91. if unread, err = d.feedRPC.AppUnreadCount(c, arg); err != nil {
  92. err = errors.Wrapf(err, "%v", arg)
  93. }
  94. return
  95. }
  96. func (d *Dao) ArticleFeed(c context.Context, mid int64, pn, ps int) (fs []*article.Meta, err error) {
  97. ip := metadata.String(c, metadata.RemoteIP)
  98. arg := &feed.ArgFeed{Mid: mid, Pn: pn, Ps: ps, RealIP: ip}
  99. if fs, err = d.feedRPC.ArticleFeed(c, arg); err != nil {
  100. if err == ecode.NothingFound {
  101. err = nil
  102. return
  103. }
  104. err = errors.Wrapf(err, "%v", arg)
  105. }
  106. return
  107. }
  108. func (d *Dao) ArticleUnreadCount(c context.Context, mid int64) (unread int, err error) {
  109. ip := metadata.String(c, metadata.RemoteIP)
  110. arg := &feed.ArgMid{Mid: mid, RealIP: ip}
  111. if unread, err = d.feedRPC.ArticleUnreadCount(c, arg); err != nil {
  112. err = errors.Wrapf(err, "%v", arg)
  113. }
  114. return
  115. }