getuserpostlistlogic.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package logic
  2. import (
  3. "context"
  4. "slowwild/internal/errorx"
  5. "slowwild/internal/model"
  6. "slowwild/internal/svc"
  7. "strconv"
  8. "strings"
  9. "git.banshen.xyz/huangguangrong/slow_wild_protobuff/slowwild/slowwildserver"
  10. "slowwild/internal/constants"
  11. "github.com/spf13/cast"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetUserPostListLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewGetUserPostListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserPostListLogic {
  20. return &GetUserPostListLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. // 获取用户发布的帖子
  27. func (l *GetUserPostListLogic) GetUserPostList(in *slowwildserver.GetUserPostListReq) (*slowwildserver.GetUserPostListRsp, 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.GetUserPosts(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. var isLiked bool
  74. // 检查当前用户是否点赞过该帖子
  75. if userId > 0 {
  76. isLiked, err = l.svcCtx.PostActionModel.CheckPostAction(l.ctx, userId, post.ID, 0)
  77. if err != nil {
  78. l.Logger.Errorf("检查点赞状态失败: %v", err)
  79. }
  80. }
  81. var isCollected bool
  82. // 检查当前用户是否收藏过该帖子
  83. if userId > 0 {
  84. isCollected, err = l.svcCtx.PostActionModel.CheckPostAction(l.ctx, userId, post.ID, 1)
  85. if err != nil {
  86. l.Logger.Errorf("检查收藏状态失败: %v", err)
  87. }
  88. }
  89. // 获取话题信息
  90. var tagItems []*slowwildserver.TagItem
  91. if post.Tags != "" {
  92. tagStrs := strings.Split(post.Tags, ",")
  93. for _, tagStr := range tagStrs {
  94. tagId, err := strconv.ParseInt(tagStr, 10, 64)
  95. if err != nil {
  96. continue
  97. }
  98. if tag, ok := tagMap[tagId]; ok {
  99. tagItems = append(tagItems, &slowwildserver.TagItem{
  100. Id: tag.ID,
  101. Name: tag.Name,
  102. })
  103. }
  104. }
  105. }
  106. postList = append(postList, &slowwildserver.GetPostListItem{
  107. Id: post.ID,
  108. PostType: post.PostType,
  109. Title: content.Title,
  110. ContentSummary: content.ContentSummary,
  111. Images: strings.Split(content.PostCovers, ","),
  112. VideoUrl: content.PostVideoUrl,
  113. VideoCover: content.PostVideoCover,
  114. CommentCount: post.CommentCount,
  115. CollectionCount: post.CollectionCount,
  116. UpvoteCount: post.UpvoteCount,
  117. ShareCount: post.ShareCount,
  118. CreatedOn: post.CreatedOn,
  119. IsLiked: isLiked,
  120. IsCollected: isCollected,
  121. Tags: tagItems,
  122. })
  123. }
  124. return &slowwildserver.GetUserPostListRsp{
  125. Total: total,
  126. List: postList,
  127. }, nil
  128. }