summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrail Finder (aider) <wohilas@gmail.com>2025-04-05 15:12:11 +0300
committerGrail Finder (aider) <wohilas@gmail.com>2025-04-05 15:12:11 +0300
commitb737b1ff0f72a23c90ae70b68b135f104e643a10 (patch)
tree7f2cb700bd7837f90d48033193caf1e98c31ac4e
parentcbf71a38424b4c778e3b607cfd52245655ae6a12 (diff)
feat: enhance answer handler with validation and feedback
-rw-r--r--internal/handlers/main.go47
1 files changed, 42 insertions, 5 deletions
diff --git a/internal/handlers/main.go b/internal/handlers/main.go
index 71c7bd2..ce894c2 100644
--- a/internal/handlers/main.go
+++ b/internal/handlers/main.go
@@ -2,6 +2,7 @@ package handlers
import (
"fmt"
+ "strconv"
"demoon/config"
"demoon/internal/database/repos"
"demoon/internal/models"
@@ -40,17 +41,23 @@ func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) {
func (h *Handlers) HandleAnswer(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
+ h.log.Error("failed to parse form", "error", err)
abortWithError(w, "Invalid form data")
return
}
selectedIndex := r.FormValue("selected")
questionID := r.FormValue("question_id")
+ if selectedIndex == "" || questionID == "" {
+ h.log.Error("missing form values", "selected", selectedIndex, "question_id", questionID)
+ abortWithError(w, "Missing required fields")
+ return
+ }
+
h.log.Info("answer received",
"selected", selectedIndex,
"question_id", questionID)
- // Get question from database
question, err := h.repo.DBGetQuestion(questionID)
if err != nil {
h.log.Error("failed to get question", "error", err, "question_id", questionID)
@@ -58,17 +65,47 @@ func (h *Handlers) HandleAnswer(w http.ResponseWriter, r *http.Request) {
return
}
- var feedback string
- if selectedIndex == fmt.Sprint(question.CorrectIndex) {
- feedback = `<div class="feedback">Correct! 🎉</div>`
+ // Convert selectedIndex to int for comparison
+ selectedIdx, err := strconv.Atoi(selectedIndex)
+ if err != nil {
+ h.log.Error("invalid selected index", "error", err, "selected", selectedIndex)
+ abortWithError(w, "Invalid answer selection")
+ return
+ }
+
+ // Validate selected index is within bounds
+ if selectedIdx < 0 || selectedIdx > 3 {
+ h.log.Error("selected index out of bounds", "selected", selectedIndex)
+ abortWithError(w, "Invalid answer selection")
+ return
+ }
+
+ feedback := ""
+ if selectedIdx == int(question.CorrectIndex) {
+ feedback = `<div data-testid="feedback" class="feedback">Correct! 🎉</div>`
} else {
- feedback = `<div class="feedback">Wrong answer, try again! ❌</div>`
+ feedback = `<div data-testid="feedback" class="feedback">Wrong answer! The correct answer was: ` + getCorrectOption(question) + `</div>`
}
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(feedback))
}
+func getCorrectOption(q *models.Question) string {
+ switch q.CorrectIndex {
+ case 0:
+ return q.Option1
+ case 1:
+ return q.Option2
+ case 2:
+ return q.Option3
+ case 3:
+ return q.Option4
+ default:
+ return ""
+ }
+}
+
func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {