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

140 lines
3.5 KiB
Go

package postservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
model "app-cloudep-tweeting-service/internal/model/mongo"
"context"
"fmt"
"strings"
ers "code.30cm.net/digimon/library-go/errs"
"code.30cm.net/digimon/library-go/errs/code"
"google.golang.org/protobuf/proto"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/svc"
"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"`
}
// ListPostError 0502105 資料庫錯誤
func ListPostError(s ...string) *ers.LibError {
return ers.NewError(code.CloudEPTweeting, code.DBError,
domain.ListPostError.ToUint32(),
fmt.Sprintf("%s", strings.Join(s, " ")))
}
// ListPostErrorL logs error message and returns Error
func ListPostErrorL(l logx.Logger, filed []logx.LogField, s ...string) *ers.LibError {
e := ListPostError(s...)
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
return e
}
// 將單個 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,
LikeCount: int64(item.Like),
DislikeCount: int64(item.DisLike),
}
}
// ListPosts 查詢貼文 -> 主流程
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 {
e := ListPostErrorL(
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)
return nil, e
}
// 將查詢結果轉換為 API 回應格式
result := make([]*tweeting.PostDetailItem, 0, count)
for _, item := range find {
result = append(result, convertToPostDetailItem(item))
}
// 返回結果
return &tweeting.ListPostsResp{
Posts: result,
Page: &tweeting.Pager{
Total: count,
Index: pageIndex,
Size: pageSize,
},
}, nil
}