| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package logic
- import (
- "context"
- "slowwild/internal/constants"
- "slowwild/internal/errorx"
- "slowwild/internal/model"
- "slowwild/internal/svc"
- "strconv"
- "git.banshen.xyz/huangguangrong/slow_wild_protobuff/slowwild/slowwildserver"
- "strings"
- "github.com/spf13/cast"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type GetUserPostCollectionListLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewGetUserPostCollectionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserPostCollectionListLogic {
- return &GetUserPostCollectionListLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- // 获取用户收藏的帖子
- func (l *GetUserPostCollectionListLogic) GetUserPostCollectionList(in *slowwildserver.GetUserPostCollectionListReq) (*slowwildserver.GetUserPostCollectionListRsp, error) {
- // 从context中获取userId
- userId, _ := cast.ToInt64E(l.ctx.Value(constants.UserIDKey))
- if in.QueryUserId <= 0 || in.Page <= 0 || in.PageSize <= 0 {
- return nil, errorx.ErrInvalidParam
- }
- // 获取收藏的帖子列表
- posts, total, err := l.svcCtx.PostModel.GetUserCollectionPosts(l.ctx, int64(in.QueryUserId), int(in.Page), int(in.PageSize))
- if err != nil {
- l.Logger.Errorf("获取用户收藏帖子列表失败: %v", err)
- return nil, errorx.ErrPostQueryFailed
- }
- // 获取所有话题ID
- var allTagIds []int64
- for _, post := range posts {
- if post.Tags != "" {
- tagStrs := strings.Split(post.Tags, ",")
- for _, tagStr := range tagStrs {
- tagId, err := strconv.ParseInt(tagStr, 10, 64)
- if err != nil {
- continue
- }
- allTagIds = append(allTagIds, tagId)
- }
- }
- }
- // 批量获取话题信息
- tags, err := l.svcCtx.TagModel.FindByIds(l.ctx, allTagIds)
- if err != nil {
- l.Logger.Errorf("批量获取话题信息失败: %v", err)
- return nil, errorx.ErrPostQueryFailed
- }
- // 构建话题信息map
- tagMap := make(map[int64]*model.Tag)
- for _, tag := range tags {
- tagMap[tag.ID] = tag
- }
- // 构建返回数据
- var postList []*slowwildserver.GetPostListItem
- for _, post := range posts {
- // 获取帖子内容
- content, err := l.svcCtx.PostModel.GetPostContent(l.ctx, post.ID)
- if err != nil {
- l.Logger.Errorf("获取帖子内容失败: %v", err)
- continue
- }
- // 检查当前用户是否点赞过该帖子
- isLiked, err := l.svcCtx.PostActionModel.CheckPostAction(l.ctx, userId, post.ID, 0)
- if err != nil {
- l.Logger.Errorf("检查点赞状态失败: %v", err)
- }
- // 检查当前用户是否收藏过该帖子
- isCollected, err := l.svcCtx.PostActionModel.CheckPostAction(l.ctx, userId, post.ID, 1)
- if err != nil {
- l.Logger.Errorf("检查收藏状态失败: %v", err)
- }
- // 获取话题信息
- var tagItems []*slowwildserver.TagItem
- if post.Tags != "" {
- tagStrs := strings.Split(post.Tags, ",")
- for _, tagStr := range tagStrs {
- tagId, err := strconv.ParseInt(tagStr, 10, 64)
- if err != nil {
- continue
- }
- if tag, ok := tagMap[tagId]; ok {
- tagItems = append(tagItems, &slowwildserver.TagItem{
- Id: tag.ID,
- Name: tag.Name,
- })
- }
- }
- }
- postList = append(postList, &slowwildserver.GetPostListItem{
- Id: post.ID,
- PostType: post.PostType,
- Title: content.Title,
- ContentSummary: content.ContentSummary,
- Images: strings.Split(content.PostCovers, ","),
- VideoUrl: content.PostVideoUrl,
- VideoCover: content.PostVideoCover,
- CommentCount: post.CommentCount,
- CollectionCount: post.CollectionCount,
- UpvoteCount: post.UpvoteCount,
- ShareCount: post.ShareCount,
- CreatedOn: post.CreatedOn,
- IsLiked: isLiked,
- IsCollected: isCollected,
- Tags: tagItems,
- })
- }
- return &slowwildserver.GetUserPostCollectionListRsp{
- Total: total,
- List: postList,
- }, nil
- }
|