app-cloudep-tweeting-service/internal/logic/postservice/list_posts_logic.go

125 lines
3.0 KiB
Go
Raw Permalink Normal View History

2024-08-28 09:09:01 +00:00
package postservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
model "app-cloudep-tweeting-service/internal/model/mongo"
2024-08-28 09:09:01 +00:00
"context"
ers "code.30cm.net/digimon/library-go/errs"
"google.golang.org/protobuf/proto"
2024-08-28 14:29:42 +00:00
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/svc"
2024-08-28 09:09:01 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
type ListPostsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewListPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPostsLogic {
return &ListPostsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 只列出要驗證的資料
type listReq struct {
OnlyAdds int32 `json:"only_adds" validate:"oneof=0 1 2 3"`
PageSize int64 `json:"page_size" validate:"required"`
PageIndex int64 `json:"page_index" validate:"required"`
}
// 將單個 Post 轉換為 PostDetailItem
func convertToPostDetailItem(item *model.Post) *tweeting.PostDetailItem {
media := make([]*tweeting.Media, 0, len(item.MediaURL))
for _, subItem := range item.MediaURL {
media = append(media, &tweeting.Media{
Type: subItem.Type,
Url: subItem.Links,
})
}
return &tweeting.PostDetailItem{
PostId: item.ID.Hex(),
Uid: item.UID,
Content: item.Content,
Tags: item.Tags,
Media: media,
IsAd: item.IsAd,
CreatedAt: item.CreateAt,
UpdateAt: item.UpdateAt,
2024-09-03 09:47:34 +00:00
LikeCount: item.Like,
DislikeCount: item.DisLike,
}
}
// ListPosts 查詢貼文 -> 主流程
2024-08-28 14:24:47 +00:00
func (l *ListPostsLogic) ListPosts(in *tweeting.QueryPostsReq) (*tweeting.ListPostsResp, error) {
// 將 PageSize 和 PageIndex 提前轉換為 int64
pageSize := int64(in.GetPageSize())
pageIndex := int64(in.GetPageIndex())
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&listReq{
PageSize: pageSize,
PageIndex: pageIndex,
OnlyAdds: in.GetOnlyAds(),
}); err != nil {
// 錯誤代碼 05-011-00
return nil, ers.InvalidFormat(err.Error())
}
// 構建查詢條件
query := &model.QueryPostModelReq{
UID: in.GetUid(),
Id: in.GetPostId(),
PageSize: pageSize,
PageIndex: pageIndex,
}
// 處理 OnlyAds 條件
if in.OnlyAds != nil {
onlyAds := in.GetOnlyAds()
query.OnlyAds = proto.Bool(onlyAds == domain.AdTypeOnlyAd.ToInt32())
}
// 執行查詢
find, count, err := l.svcCtx.PostModel.Find(l.ctx, query)
if err != nil {
// 錯誤代碼 05-021-05
e := domain.CommentErrorL(
domain.ListPostError,
logx.WithContext(l.ctx),
[]logx.LogField{
{Key: "query", Value: query},
{Key: "func", Value: "PostModel.Find"},
{Key: "err", Value: err},
},
"failed to find posts").Wrap(err)
2024-09-03 09:47:34 +00:00
return nil, e
}
// 將查詢結果轉換為 API 回應格式
result := make([]*tweeting.PostDetailItem, 0, count)
for _, item := range find {
result = append(result, convertToPostDetailItem(item))
}
2024-08-28 09:09:01 +00:00
// 返回結果
return &tweeting.ListPostsResp{
Posts: result,
Page: &tweeting.Pager{
Total: count,
Index: pageIndex,
Size: pageSize,
},
}, nil
2024-08-28 09:09:01 +00:00
}