generated from VLADIMIR/template
53 lines
1.9 KiB
Go
53 lines
1.9 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"evening_detective_server/internal/repos/teams_repo"
|
|
"evening_detective_server/proto"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
func mapParticipationResponse(p *teams_repo.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 participationError(err error) string {
|
|
if errors.Is(err, teams_repo.ErrParticipationLink) || errors.Is(err, teams_repo.ErrParticipationFinal) || errors.Is(err, teams_repo.ErrParticipationClosed) {
|
|
return err.Error()
|
|
}
|
|
return "Не удалось обработать ответ. Попробуйте позже."
|
|
}
|
|
func (s *server) GetParticipation(ctx context.Context, req *proto.GetParticipationReq) (*proto.ParticipationRsp, error) {
|
|
_ = grpc.SetHeader(ctx, metadata.Pairs("Cache-Control", "no-store"))
|
|
p, err := s.participationService.GetParticipation(ctx, req.Token)
|
|
result := mapParticipationResponse(p)
|
|
if err != nil {
|
|
result.Error = participationError(err)
|
|
}
|
|
return result, 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.Answer != "confirmed" && req.Answer != "cancelled" {
|
|
return &proto.ParticipationRsp{Error: "Выберите подтверждение или отмену участия"}, nil
|
|
}
|
|
p, err := s.participationService.AnswerParticipation(ctx, req.Token, req.Answer)
|
|
result := mapParticipationResponse(p)
|
|
if err != nil {
|
|
result.Error = participationError(err)
|
|
}
|
|
return result, nil
|
|
}
|