package postservicelogic import ( "app-cloudep-tweeting-service/internal/domain" model "app-cloudep-tweeting-service/internal/model/mongo" ers "code.30cm.net/digimon/library-go/errs" "context" "app-cloudep-tweeting-service/gen_result/pb/tweeting" "app-cloudep-tweeting-service/internal/svc" "github.com/zeromicro/go-zero/core/logx" ) type LikeLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeLogic { return &LikeLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } type likeReq struct { Target string `json:"target" validate:"required"` UID string `json:"uid" validate:"required"` } // Like 點讚/取消讚 貼文 func (l *LikeLogic) Like(in *tweeting.LikeReq) (*tweeting.PostReactionActionResp, error) { // 驗證資料 if err := l.svcCtx.Validate.ValidateAll(&likeReq{ Target: in.TargetId, UID: in.GetUid(), }); err != nil { return nil, ers.InvalidFormat(err.Error()) } likeResp, err := l.svcCtx.PostLikeModel.LikeDislike(l.ctx, &model.PostLikes{ TargetID: in.GetTargetId(), UID: in.GetUid(), Type: int8(in.GetLikeType()), }) 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 } // 將文章的數量增加或減少的功能,是業務邏輯,從外面再決定要不要丟MQ 後算,或是怎麼算 return &tweeting.PostReactionActionResp{ PostID: likeResp.PostID, ReactionType: int64(likeResp.ReactionType), IsIncrement: likeResp.IsIncrement, }, nil }