This commit is contained in:
2026-09-01 01:27:12 +07:00
parent 69666ee264
commit f648beaa33
3 changed files with 34 additions and 25 deletions
+12 -19
View File
@@ -2,16 +2,11 @@ package team_answers_repo
import (
"context"
"errors"
"evening_detective_server/internal/repos"
"github.com/jackc/pgx/v5/pgxpool"
)
var (
ErrAnswerNotFound = errors.New("Ответ не найден")
)
type TeamAnswersRepo struct {
pool *pgxpool.Pool
}
@@ -136,28 +131,26 @@ func (r *TeamAnswersRepo) GetAnswersByGameID(
}
// SetAnswerScore выставляет балл за ответ; score == nil сбрасывает балл.
// Балл можно выставить и за неотвеченный вопрос (команда может не дать
// ответ): если строки (team_id, question_code) ещё нет, она создаётся с
// пустым ответом и выставленным баллом.
func (r *TeamAnswersRepo) SetAnswerScore(
ctx context.Context,
teamId int,
questionCode string,
score *int,
) error {
tag, err := r.pool.Exec(
_, err := r.pool.Exec(
ctx,
`UPDATE team_answers
SET
score = $1,
updated_at = NOW()
WHERE team_id = $2 AND question_code = $3`,
score,
`INSERT INTO team_answers (team_id, question_code, answer, score)
VALUES ($1, $2, '', $3)
ON CONFLICT (team_id, question_code)
DO UPDATE SET
score = EXCLUDED.score,
updated_at = NOW()`,
teamId,
questionCode,
score,
)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrAnswerNotFound
}
return nil
return err
}