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"` LikeType domain.LikeType `json:"like_type" validate:"required,oneof=1 2"` } // Like 點讚/取消讚 貼文 func (l *LikeLogic) Like(in *tweeting.LikeReq) (*tweeting.OKResp, error) { // 驗證資料 if err := l.svcCtx.Validate.ValidateAll(&likeReq{ Target: in.TargetId, UID: in.GetUid(), LikeType: domain.LikeType(in.GetLikeType()), }); err != nil { return nil, ers.InvalidFormat(err.Error()) } 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 } return &tweeting.OKResp{}, nil }