dao.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package article
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-intl/conf"
  5. article "go-common/app/interface/openplatform/article/model"
  6. artrpc "go-common/app/interface/openplatform/article/rpc/client"
  7. "go-common/library/net/metadata"
  8. "github.com/pkg/errors"
  9. )
  10. // Dao is atticle dao
  11. type Dao struct {
  12. artRPC *artrpc.Service
  13. }
  14. // New initial tag dao
  15. func New(c *conf.Config) (d *Dao) {
  16. d = &Dao{
  17. artRPC: artrpc.New(c.ArticleRPC),
  18. }
  19. return
  20. }
  21. // UpArticles get article data from api.
  22. func (d *Dao) UpArticles(c context.Context, mid int64, pn, ps int) (ams []*article.Meta, count int, err error) {
  23. var (
  24. res *article.UpArtMetas
  25. ip = metadata.String(c, metadata.RemoteIP)
  26. )
  27. arg := &article.ArgUpArts{Mid: mid, Pn: pn, Ps: ps, RealIP: ip}
  28. if res, err = d.artRPC.UpArtMetas(c, arg); err != nil {
  29. err = errors.Wrapf(err, "%v", arg)
  30. return
  31. }
  32. if res != nil {
  33. ams = res.Articles
  34. count = res.Count
  35. }
  36. return
  37. }
  38. // Favorites get article data from api.
  39. func (d *Dao) Favorites(c context.Context, mid int64, pn, ps int) (res []*article.Favorite, err error) {
  40. ip := metadata.String(c, metadata.RemoteIP)
  41. arg := &article.ArgFav{Mid: mid, Pn: pn, Ps: ps, RealIP: ip}
  42. if res, err = d.artRPC.Favorites(c, arg); err != nil {
  43. err = errors.Wrapf(err, "%v", arg)
  44. }
  45. return
  46. }
  47. // Articles is.
  48. func (d *Dao) Articles(c context.Context, aids []int64) (res map[int64]*article.Meta, err error) {
  49. ip := metadata.String(c, metadata.RemoteIP)
  50. arg := &article.ArgAids{Aids: aids, RealIP: ip}
  51. if res, err = d.artRPC.ArticleMetas(c, arg); err != nil {
  52. err = errors.Wrapf(err, "%v", arg)
  53. }
  54. return
  55. }
  56. // UpLists is.
  57. func (d *Dao) UpLists(c context.Context, mid int64) (lists []*article.List, count int, err error) {
  58. var (
  59. res article.UpLists
  60. ip = metadata.String(c, metadata.RemoteIP)
  61. )
  62. arg := &article.ArgMid{Mid: mid, RealIP: ip}
  63. if res, err = d.artRPC.UpLists(c, arg); err != nil {
  64. err = errors.Wrapf(err, "%+v", arg)
  65. return
  66. }
  67. lists = res.Lists
  68. count = res.Total
  69. return
  70. }