getuserpostcollectionlistlogic.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package logic
  2. import (
  3. "context"
  4. "slowwild/internal/constants"
  5. "slowwild/internal/errorx"
  6. "slowwild/internal/model"
  7. "slowwild/internal/svc"
  8. "strconv"
  9. "git.banshen.xyz/huangguangrong/slow_wild_protobuff/slowwild/slowwildserver"
  10. "strings"
  11. "github.com/spf13/cast"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetUserPostCollectionListLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewGetUserPostCollectionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserPostCollectionListLogic {
  20. return &GetUserPostCollectionListLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. // 获取用户收藏的帖子
  27. func (l *GetUserPostCollectionListLogic) GetUserPostCollectionList(in *slowwildserver.GetUserPostCollectionListReq) (*slowwildserver.GetUserPostCollectionListRsp, error) {
  28. // 从context中获取userId
  29. userId, _ := cast.ToInt64E(l.ctx.Value(constants.UserIDKey))
  30. if in.QueryUserId <= 0 || in.Page <= 0 || in.PageSize <= 0 {
  31. return nil, errorx.ErrInvalidParam
  32. }
  33. // 获取收藏的帖子列表
  34. posts, total, err := l.svcCtx.PostModel.GetUserCollectionPosts(l.ctx, int64(in.QueryUserId), int(in.Page), int(in.PageSize))
  35. if err != nil {
  36. l.Logger.Errorf("获取用户收藏帖子列表失败: %v", err)
  37. return nil, errorx.ErrPostQueryFailed
  38. }
  39. // 获取所有话题ID
  40. var allTagIds []int64
  41. for _, post := range posts {
  42. if post.Tags != "" {
  43. tagStrs := strings.Split(post.Tags, ",")
  44. for _, tagStr := range tagStrs {
  45. tagId, err := strconv.ParseInt(tagStr, 10, 64)
  46. if err != nil {
  47. continue
  48. }
  49. allTagIds = append(allTagIds, tagId)
  50. }
  51. }
  52. }
  53. // 批量获取话题信息
  54. tags, err := l.svcCtx.TagModel.FindByIds(l.ctx, allTagIds)
  55. if err != nil {
  56. l.Logger.Errorf("批量获取话题信息失败: %v", err)
  57. return nil, errorx.ErrPostQueryFailed
  58. }
  59. // 构建话题信息map
  60. tagMap := make(map[int64]*model.Tag)
  61. for _, tag := range tags {
  62. tagMap[tag.ID] = tag
  63. }
  64. // 构建返回数据
  65. var postList []*slowwildserver.GetPostListItem
  66. for _, post := range posts {
  67. // 获取帖子内容
  68. content, err := l.svcCtx.PostModel.GetPostContent(l.ctx, post.ID)
  69. if err != nil {
  70. l.Logger.Errorf("获取帖子内容失败: %v", err)
  71. continue
  72. }
  73. // 检查当前用户是否点赞过该帖子
  74. isLiked, err := l.svcCtx.PostActionModel.CheckPostAction(l.ctx, userId, post.ID, 0)
  75. if err != nil {
  76. l.Logger.Errorf("检查点赞状态失败: %v", err)
  77. }
  78. // 检查当前用户是否收藏过该帖子
  79. isCollected, err := l.svcCtx.PostActionModel.CheckPostAction(l.ctx, userId, post.ID, 1)
  80. if err != nil {
  81. l.Logger.Errorf("检查收藏状态失败: %v", err)
  82. }
  83. // 获取话题信息
  84. var tagItems []*slowwildserver.TagItem
  85. if post.Tags != "" {
  86. tagStrs := strings.Split(post.Tags, ",")
  87. for _, tagStr := range tagStrs {
  88. tagId, err := strconv.ParseInt(tagStr, 10, 64)
  89. if err != nil {
  90. continue
  91. }
  92. if tag, ok := tagMap[tagId]; ok {
  93. tagItems = append(tagItems, &slowwildserver.TagItem{
  94. Id: tag.ID,
  95. Name: tag.Name,
  96. })
  97. }
  98. }
  99. }
  100. postList = append(postList, &slowwildserver.GetPostListItem{
  101. Id: post.ID,
  102. PostType: post.PostType,
  103. Title: content.Title,
  104. ContentSummary: content.ContentSummary,
  105. Images: strings.Split(content.PostCovers, ","),
  106. VideoUrl: content.PostVideoUrl,
  107. VideoCover: content.PostVideoCover,
  108. CommentCount: post.CommentCount,
  109. CollectionCount: post.CollectionCount,
  110. UpvoteCount: post.UpvoteCount,
  111. ShareCount: post.ShareCount,
  112. CreatedOn: post.CreatedOn,
  113. IsLiked: isLiked,
  114. IsCollected: isCollected,
  115. Tags: tagItems,
  116. })
  117. }
  118. return &slowwildserver.GetUserPostCollectionListRsp{
  119. Total: total,
  120. List: postList,
  121. }, nil
  122. }