generated from VLADIMIR/template
fix
This commit is contained in:
@@ -38,6 +38,10 @@ func NewAnswerService(
|
||||
}
|
||||
|
||||
var (
|
||||
// ErrTeamNotFound — команда не найдена или пароль неверен.
|
||||
ErrTeamNotFound = errors.New("Команда не найдена")
|
||||
// ErrGameNotFound — игра не найдена.
|
||||
ErrGameNotFound = errors.New("Игра не найдена")
|
||||
// ErrGameFinished — после завершения игры ответы команд больше нельзя
|
||||
// изменять: организатор оценивает их в панели игры.
|
||||
ErrGameFinished = errors.New("Игра завершена, ответы больше нельзя изменять")
|
||||
@@ -47,19 +51,27 @@ var (
|
||||
ErrQuestionNotFound = errors.New("Вопрос не найден")
|
||||
// ErrGameNotFinished — итоги игры доступны только после её завершения.
|
||||
ErrGameNotFinished = errors.New("Итоги доступны после завершения игры")
|
||||
// ErrOfflineAnswers — ответы для offline-игры недоступны через сайт.
|
||||
ErrOfflineAnswers = errors.New("В offline-игре ответы через сайт недоступны")
|
||||
)
|
||||
|
||||
// getTeamAndGame проверяет команду по паролю и возвращает её вместе с игрой.
|
||||
func (s *AnswerService) getTeamAndGame(ctx context.Context, teamID int, password string) (*repos.Team, *repos.Game, error) {
|
||||
team, err := s.teamsRepo.GetTeamByID(ctx, teamID)
|
||||
if err != nil {
|
||||
if errors.Is(err, teams_repo.ErrTeamNotFound) {
|
||||
return nil, nil, ErrTeamNotFound
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
if team.Password != password || team.ParticipationStatus == "cancelled" {
|
||||
return nil, nil, teams_repo.ErrTeamNotFound
|
||||
return nil, nil, ErrTeamNotFound
|
||||
}
|
||||
game, err := s.gamesRepo.GetGameByID(ctx, team.GameID)
|
||||
if err != nil {
|
||||
if errors.Is(err, games_repo.ErrGameNotFound) {
|
||||
return nil, nil, ErrGameNotFound
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
return team, game, nil
|
||||
@@ -120,7 +132,7 @@ func (s *AnswerService) SubmitTeamAnswers(ctx context.Context, teamID int, passw
|
||||
return err
|
||||
}
|
||||
if game.Format == "offline" {
|
||||
return errors.New("В offline-игре ответы через сайт недоступны")
|
||||
return ErrOfflineAnswers
|
||||
}
|
||||
switch game.Status {
|
||||
case "run", "pause":
|
||||
@@ -157,7 +169,7 @@ func (s *AnswerService) SubmitTeamAnswers(ctx context.Context, teamID int, passw
|
||||
func (s *AnswerService) GetGameAnswers(ctx context.Context, gameID int) (*GameAnswers, error) {
|
||||
game, err := s.gamesRepo.GetGameByID(ctx, gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, mapAnswerError(err)
|
||||
}
|
||||
questions, byCode, err := s.getScenarioQuestionsByCode(ctx, game.ScenarioID)
|
||||
if err != nil {
|
||||
@@ -227,11 +239,11 @@ func (s *AnswerService) GetGameAnswers(ctx context.Context, gameID int) (*GameAn
|
||||
func (s *AnswerService) SetTeamAnswerScore(ctx context.Context, teamID int, questionCode string, score *int) error {
|
||||
team, err := s.teamsRepo.GetTeamByID(ctx, teamID)
|
||||
if err != nil {
|
||||
return err
|
||||
return mapAnswerError(err)
|
||||
}
|
||||
game, err := s.gamesRepo.GetGameByID(ctx, team.GameID)
|
||||
if err != nil {
|
||||
return err
|
||||
return mapAnswerError(err)
|
||||
}
|
||||
_, byCode, err := s.getScenarioQuestionsByCode(ctx, game.ScenarioID)
|
||||
if err != nil {
|
||||
@@ -251,7 +263,7 @@ func (s *AnswerService) SetTeamAnswerScore(ctx context.Context, teamID int, ques
|
||||
func (s *AnswerService) GetGameResults(ctx context.Context, gameID int) (*GameResults, error) {
|
||||
game, err := s.gamesRepo.GetGameByID(ctx, gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, mapAnswerError(err)
|
||||
}
|
||||
if game.Status != "finish" {
|
||||
return nil, ErrGameNotFinished
|
||||
@@ -301,3 +313,13 @@ func (s *AnswerService) GetGameResults(ctx context.Context, gameID int) (*GameRe
|
||||
Teams: rankTeams(buildTeamResults(questions, mappedTeams, answerByTeamAndCode)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func mapAnswerError(err error) error {
|
||||
if errors.Is(err, teams_repo.ErrTeamNotFound) {
|
||||
return ErrTeamNotFound
|
||||
}
|
||||
if errors.Is(err, games_repo.ErrGameNotFound) {
|
||||
return ErrGameNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user