Files
juwan-backend/app/community/api/internal/logic/community/listPostsLogic.go
T
2026-04-23 01:06:58 +08:00

57 lines
1.5 KiB
Go

// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"strings"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type ListPostsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取帖子列表
func NewListPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPostsLogic {
return &ListPostsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ListPostsLogic) ListPosts(req *types.PostListReq) (resp *types.PostListResp, err error) {
if req.Limit <= 0 {
req.Limit = 20
}
in := &communitypb.SearchPostsReq{Offset: req.Offset, Limit: req.Limit}
if req.Tags != "" {
in.Tags = strings.Split(req.Tags, ",")
}
posts, err := l.svcCtx.CommunityRpc.SearchPosts(l.ctx, in)
if err != nil {
return nil, err
}
uid := currentUserIDOrZero(l.ctx)
items := make([]types.Post, 0, len(posts.GetPosts()))
for _, p := range posts.GetPosts() {
liked := false
if uid > 0 {
likes, e := l.svcCtx.CommunityRpc.SearchPostLikes(l.ctx, &communitypb.SearchPostLikesReq{Offset: 0, Limit: 1, PostId: &p.Id, UserId: &uid})
liked = e == nil && len(likes.GetPostLikes()) > 0
}
items = append(items, mapPost(l.ctx, l.svcCtx, p, liked))
}
return &types.PostListResp{Items: items, Meta: types.PageMeta{Total: int64(len(items)), Offset: req.Offset, Limit: req.Limit}}, nil
}