generated from VLADIMIR/template
updates
This commit is contained in:
@@ -6,7 +6,9 @@ import (
|
||||
"evening_detective_server/internal/services/participation_service"
|
||||
"evening_detective_server/proto"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
@@ -23,36 +25,40 @@ func mapParticipationResponse(p *participation_service.Participation) *proto.Par
|
||||
return result
|
||||
}
|
||||
|
||||
func participationError(err error) string {
|
||||
if errors.Is(err, participation_service.ErrParticipationLink) || errors.Is(err, participation_service.ErrParticipationFinal) || errors.Is(err, participation_service.ErrParticipationClosed) {
|
||||
return err.Error()
|
||||
func participationErrorStatus(err error, internalMessage string) error {
|
||||
switch {
|
||||
case errors.Is(err, participation_service.ErrParticipationLink):
|
||||
return status.Error(codes.NotFound, err.Error())
|
||||
case errors.Is(err, participation_service.ErrInvalidAnswer):
|
||||
return status.Error(codes.InvalidArgument, err.Error())
|
||||
case errors.Is(err, participation_service.ErrParticipationFinal), errors.Is(err, participation_service.ErrParticipationClosed):
|
||||
return status.Error(codes.FailedPrecondition, err.Error())
|
||||
default:
|
||||
return status.Error(codes.Internal, internalMessage)
|
||||
}
|
||||
return "Не удалось обработать ответ. Попробуйте позже."
|
||||
}
|
||||
func (s *server) GetParticipation(ctx context.Context, req *proto.GetParticipationReq) (*proto.ParticipationRsp, error) {
|
||||
_ = grpc.SetHeader(ctx, metadata.Pairs("Cache-Control", "no-store"))
|
||||
if req == nil {
|
||||
return &proto.ParticipationRsp{Error: participationError(participation_service.ErrParticipationLink)}, nil
|
||||
return nil, participationErrorStatus(participation_service.ErrParticipationLink, "не удалось получить данные об участии")
|
||||
}
|
||||
p, err := s.participationService.GetParticipation(ctx, req.Token)
|
||||
result := mapParticipationResponse(p)
|
||||
if err != nil {
|
||||
result.Error = participationError(err)
|
||||
return nil, participationErrorStatus(err, "не удалось получить данные об участии")
|
||||
}
|
||||
return result, nil
|
||||
return mapParticipationResponse(p), nil
|
||||
}
|
||||
func (s *server) AnswerParticipation(ctx context.Context, req *proto.AnswerParticipationReq) (*proto.ParticipationRsp, error) {
|
||||
_ = grpc.SetHeader(ctx, metadata.Pairs("Cache-Control", "no-store"))
|
||||
if req == nil {
|
||||
return &proto.ParticipationRsp{Error: participationError(participation_service.ErrParticipationLink)}, nil
|
||||
return nil, participationErrorStatus(participation_service.ErrParticipationLink, "не удалось сохранить ответ")
|
||||
}
|
||||
if req.Answer != "confirmed" && req.Answer != "cancelled" {
|
||||
return &proto.ParticipationRsp{Error: participation_service.ErrInvalidAnswer.Error()}, nil
|
||||
return nil, participationErrorStatus(participation_service.ErrInvalidAnswer, "не удалось сохранить ответ")
|
||||
}
|
||||
p, err := s.participationService.AnswerParticipation(ctx, req.Token, req.Answer)
|
||||
result := mapParticipationResponse(p)
|
||||
if err != nil {
|
||||
result.Error = participationError(err)
|
||||
return nil, participationErrorStatus(err, "не удалось сохранить ответ")
|
||||
}
|
||||
return result, nil
|
||||
return mapParticipationResponse(p), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user