getuserpostlistlogic.go 3.6 KB

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