feat(attempts): прохождение тестов + автогрейд single/multi
Some checks failed
CI / test (push) Failing after 10s
Build and Deploy / build-and-deploy (push) Successful in 25s

AttemptRepository:
- Start: проверка max_attempts (учитывает уже использованные с этого
  user_id или public_token_id), вставка in_progress'а;
- Get/ListByUser/ListByTest: чтение с per-attempt scope;
- SubmitAndGrade: транзакционно сохраняет ответы в attempt_answers
  (JSONB payload + correct + score), считает итог:
    single — 1 правильный → points за вопрос, иначе 0;
    multi  — set ответов == set is_correct=TRUE → points, иначе 0
             (частичные баллы не делаем в MVP);
    text   — correct=NULL и score=NULL, ждут ручной оценки HR'ом.
  max_score = SUM(points) по всем вопросам (не только отвеченным).
  passed = NULL если у теста нет passing_score; иначе процент vs порог.
  status: graded если все автогрейд'ятся; submitted если есть text.

AttemptHandler:
- POST /tests/{id}/attempts — Start (X-User-Id из portal-gateway).
  Не-владелец стартует только если is_published=true.
- GET  /attempts/{id} — Get с проверкой «я респондент / я владелец теста».
- POST /attempts/{id}/submit — Submit (только свою попытку).
- GET  /attempts — ListMine.
- GET  /tests/{id}/attempts — ListByTest (только для владельца).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Ilya
2026-05-25 23:00:38 +03:00
parent 5ab6cc95cd
commit 4f9b1b1491
3 changed files with 596 additions and 5 deletions

View File

@@ -44,9 +44,11 @@ func main() {
}
testRepo := repository.NewTestRepository(pool)
attemptRepo := repository.NewAttemptRepository(pool)
healthH := handler.NewHealthHandler(pool)
testH := handler.NewTestHandler(testRepo)
attemptH := handler.NewAttemptHandler(attemptRepo, testRepo)
r := chi.NewRouter()
r.Use(chimw.RequestID)
@@ -71,11 +73,15 @@ func main() {
r.Post("/tests/{id}/questions", testH.CreateQuestion)
r.Delete("/tests/{id}/questions/{questionId}", testH.DeleteQuestion)
// Заглушки на следующие итерации. Возвращают 501 — фронт может
// рендерить «функция в разработке», если случайно дёрнет.
r.HandleFunc("/tests/{id}/attempts", notImplemented)
r.HandleFunc("/attempts/{id}", notImplemented)
r.HandleFunc("/attempts/{id}/submit", notImplemented)
// Attempts: старт + получение + сабмит. Списки — отдельно
// (мои попытки vs все попытки по тесту для HR).
r.Post("/tests/{id}/attempts", attemptH.Start)
r.Get("/tests/{id}/attempts", attemptH.ListByTest)
r.Get("/attempts", attemptH.ListMine)
r.Get("/attempts/{id}", attemptH.Get)
r.Post("/attempts/{id}/submit", attemptH.Submit)
// Заглушки на следующие итерации.
r.HandleFunc("/courses", notImplemented)
r.HandleFunc("/courses/{id}", notImplemented)
r.HandleFunc("/courses/{id}/lessons", notImplemented)