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 GetLikeStatusLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewGetLikeStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLikeStatusLogic { return &GetLikeStatusLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } // 這個人按讚的文章列表(輸入UID 以及文章id,返回這個人有沒有對這些文章按讚) type getLikeStatusReq struct { Targets []string `json:"targets" validate:"required"` LikeType domain.LikeType `json:"like_type" validate:"required,oneof=1 2"` UID string `json:"uid" validate:"required"` } // GetLikeStatus 取得讚/不讚狀態 func (l *GetLikeStatusLogic) GetLikeStatus(in *tweeting.GetLikeStatusReq) (*tweeting.GetLikeStatusResp, error) { // 驗證資料 if err := l.svcCtx.Validate.ValidateAll(&getLikeStatusReq{ Targets: in.GetTargetId(), LikeType: domain.LikeType(in.GetLikeType()), UID: in.GetUid(), }); err != nil { return nil, ers.InvalidFormat(err.Error()) } list, err := l.svcCtx.PostLikeModel.FindUIDPostLikeStatus(l.ctx, &model.QueryUIDPostLikeStatusReq{ Targets: in.GetTargetId(), LikeType: domain.LikeType(in.GetLikeType()), UID: in.GetUid(), }) if err != nil { e := domain.PostMongoErrorL( logx.WithContext(l.ctx), []logx.LogField{ {Key: "req", Value: in}, {Key: "func", Value: "PostModel.FindUIDPostLikeStatus"}, {Key: "err", Value: err}, }, "failed to find uid post like status list").Wrap(err) return nil, e } var result = make([]*tweeting.GetLikeStatusItem, 0, len(list)) for _, item := range list { result = append(result, &tweeting.GetLikeStatusItem{ TargetId: item.TargetID, Status: item.LikeStatus, }) } return &tweeting.GetLikeStatusResp{ Data: result, }, nil }