2024-08-28 09:09:01 +00:00
|
|
|
package postservicelogic
|
|
|
|
|
|
|
|
import (
|
2024-08-28 14:29:42 +00:00
|
|
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
2024-08-29 14:49:42 +00:00
|
|
|
"app-cloudep-tweeting-service/internal/domain"
|
2024-08-28 14:29:42 +00:00
|
|
|
"app-cloudep-tweeting-service/internal/svc"
|
2024-08-29 14:49:42 +00:00
|
|
|
ers "code.30cm.net/digimon/library-go/errs"
|
|
|
|
"context"
|
2024-08-28 09:09:01 +00:00
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CountLikeLogic struct {
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
logx.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCountLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CountLikeLogic {
|
|
|
|
return &CountLikeLogic{
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-29 14:49:42 +00:00
|
|
|
type countLikeReq struct {
|
|
|
|
PostID string `json:"post_id" validate:"required"` // 貼文的 ID
|
|
|
|
ReactionType domain.LikeType `json:"reaction_type" validate:"required,oneof=1 2"` // 用戶的反應類型,可能是讚或不讚
|
|
|
|
}
|
|
|
|
|
2024-08-28 09:09:01 +00:00
|
|
|
// CountLike 取得讚/不讚數量
|
2024-08-28 14:24:47 +00:00
|
|
|
func (l *CountLikeLogic) CountLike(in *tweeting.LikeCountReq) (*tweeting.LikeCountResp, error) {
|
2024-08-29 14:49:42 +00:00
|
|
|
// 驗證資料
|
|
|
|
if err := l.svcCtx.Validate.ValidateAll(&countLikeReq{
|
|
|
|
PostID: in.GetTargetId(),
|
|
|
|
ReactionType: domain.LikeType(in.GetLikeType()),
|
|
|
|
}); err != nil {
|
|
|
|
return nil, ers.InvalidFormat(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := l.svcCtx.PostLikeModel.Count(l.ctx, in.GetTargetId(), domain.LikeType(in.GetLikeType()))
|
|
|
|
if err != nil {
|
|
|
|
e := domain.PostMongoErrorL(
|
|
|
|
logx.WithContext(l.ctx),
|
|
|
|
[]logx.LogField{
|
|
|
|
{Key: "req", Value: in},
|
|
|
|
{Key: "func", Value: "PostLikeModel.Count"},
|
|
|
|
{Key: "err", Value: err},
|
|
|
|
},
|
|
|
|
"failed to count like or dislike").Wrap(err)
|
|
|
|
return nil, e
|
|
|
|
}
|
2024-08-28 09:09:01 +00:00
|
|
|
|
2024-08-29 14:49:42 +00:00
|
|
|
return &tweeting.LikeCountResp{
|
|
|
|
Count: count,
|
|
|
|
}, nil
|
2024-08-28 09:09:01 +00:00
|
|
|
}
|