app-cloudep-tweeting-service/internal/logic/postservice/inc_dec_like_dislike_count_...

66 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 IncDecLikeDislikeCountLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewIncDecLikeDislikeCountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *IncDecLikeDislikeCountLogic {
return &IncDecLikeDislikeCountLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
type postReactionAction struct {
PostID string `json:"post_id" validate:"required"` // 貼文的 ID
ReactionType domain.LikeType `json:"reaction_type" validate:"required,oneof=1 2"` // 用戶的反應類型,可能是讚或不讚
IsIncrement bool `json:"is_increment" validate:"required"` // 表示是否增加true 表示增加false 表示減少)
}
// IncDecLikeDislikeCount 增減數量
func (l *IncDecLikeDislikeCountLogic) IncDecLikeDislikeCount(in *tweeting.IncDecLikeDislikeCountReq) (*tweeting.OKResp, error) {
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&postReactionAction{
PostID: in.GetPostID(),
ReactionType: domain.LikeType(in.GetReactionType()),
IsIncrement: in.GetIsIncrement(),
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
}
err := l.svcCtx.PostModel.IncDecLikeDislikeCountLogic(l.ctx, &model.PostReactionAction{
PostID: in.GetPostID(),
ReactionType: domain.LikeType(in.GetReactionType()),
IsIncrement: in.GetIsIncrement(),
})
if err != nil {
e := domain.PostMongoErrorL(
logx.WithContext(l.ctx),
[]logx.LogField{
{Key: "req", Value: in},
{Key: "func", Value: "PostModel.IncDecLikeDislikeCountLogic"},
{Key: "err", Value: err},
},
"failed to inc like or dislike").Wrap(err)
return nil, e
}
return &tweeting.OKResp{}, nil
}