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

87 lines
2.2 KiB
Go

package postservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
model "app-cloudep-tweeting-service/internal/model/mongo"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type LikeListLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewLikeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeListLogic {
return &LikeListLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 換句話說就是對這個文章按讚的人的列表
type likeListReq struct {
Target string `json:"target" validate:"required"`
LikeType domain.LikeType `json:"like_type" validate:"required,oneof=1 2"`
PageSize int64 `json:"page_size" validate:"required"`
PageIndex int64 `json:"page_index" validate:"required"`
}
// LikeList 取得讚/不讚列表
func (l *LikeListLogic) LikeList(in *tweeting.LikeListReq) (*tweeting.LikeListResp, error) {
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&likeListReq{
Target: in.TargetId,
LikeType: domain.LikeType(in.GetLikeType()),
PageSize: in.GetPageSize(),
PageIndex: in.GetPageIndex(),
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
}
result, total, err := l.svcCtx.PostLikeModel.FindLikeUsers(l.ctx, &model.QueryPostLikeReq{
Target: in.GetTargetId(),
LikeType: domain.LikeType(in.GetLikeType()),
PageSize: in.GetPageSize(),
PageIndex: in.GetPageIndex(),
})
if err != nil {
e := domain.PostMongoErrorL(
logx.WithContext(l.ctx),
[]logx.LogField{
{Key: "req", Value: in},
{Key: "func", Value: "PostModel.LikeDislike"},
{Key: "err", Value: err},
},
"failed to like or dislike").Wrap(err)
return nil, e
}
var list = make([]*tweeting.LikeItem, 0, len(result))
for _, item := range result {
list = append(list, &tweeting.LikeItem{
LikeType: int64(item.Type),
TargetId: item.TargetID,
Uid: item.UID,
})
}
return &tweeting.LikeListResp{
List: list,
Page: &tweeting.Pager{
Size: in.GetPageSize(),
Index: in.GetPageIndex(),
Total: total,
},
}, nil
}