package postservicelogic import ( "app-cloudep-tweeting-service/gen_result/pb/tweeting" "app-cloudep-tweeting-service/internal/domain" "app-cloudep-tweeting-service/internal/svc" ers "code.30cm.net/digimon/library-go/errs" "context" "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), } } type countLikeReq struct { PostID string `json:"post_id" validate:"required"` // 貼文的 ID ReactionType domain.LikeType `json:"reaction_type" validate:"required,oneof=1 2"` // 用戶的反應類型,可能是讚或不讚 } // CountLike 取得讚/不讚數量 func (l *CountLikeLogic) CountLike(in *tweeting.LikeCountReq) (*tweeting.LikeCountResp, error) { // 驗證資料 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 } return &tweeting.LikeCountResp{ Count: count, }, nil }