generated from VLADIMIR/template
65 lines
2.6 KiB
Go
65 lines
2.6 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"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"
|
|
)
|
|
|
|
func mapParticipationResponse(p *participation_service.Participation) *proto.ParticipationRsp {
|
|
result := &proto.ParticipationRsp{}
|
|
if p != nil {
|
|
result.TeamName = p.TeamName
|
|
result.GameName = p.GameName
|
|
result.StartAt = timestamppb.New(p.StartAt)
|
|
result.ParticipantsCount = p.ParticipantsCount
|
|
result.Status = p.Status
|
|
result.Closed = p.Closed
|
|
}
|
|
return result
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
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 nil, participationErrorStatus(participation_service.ErrParticipationLink, "не удалось получить данные об участии")
|
|
}
|
|
p, err := s.participationService.GetParticipation(ctx, req.Token)
|
|
if err != nil {
|
|
return nil, participationErrorStatus(err, "не удалось получить данные об участии")
|
|
}
|
|
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 nil, participationErrorStatus(participation_service.ErrParticipationLink, "не удалось сохранить ответ")
|
|
}
|
|
if req.Answer != "confirmed" && req.Answer != "cancelled" {
|
|
return nil, participationErrorStatus(participation_service.ErrInvalidAnswer, "не удалось сохранить ответ")
|
|
}
|
|
p, err := s.participationService.AnswerParticipation(ctx, req.Token, req.Answer)
|
|
if err != nil {
|
|
return nil, participationErrorStatus(err, "не удалось сохранить ответ")
|
|
}
|
|
return mapParticipationResponse(p), nil
|
|
}
|