summaryrefslogtreecommitdiff
path: root/internal/handlers/main.go
diff options
context:
space:
mode:
authorGrail Finder (aider) <wohilas@gmail.com>2025-04-05 16:32:59 +0300
committerGrail Finder (aider) <wohilas@gmail.com>2025-04-05 16:32:59 +0300
commit1d052c05fdc627b4d5efb306c1f3344c2c1bd8c9 (patch)
tree5b1178892fc858dd675659e43a82fa1eec96214d /internal/handlers/main.go
parent685da156ee8cb6b07c9eef6301b439b56823dfc3 (diff)
feat: add next question button functionality
Diffstat (limited to 'internal/handlers/main.go')
-rw-r--r--internal/handlers/main.go61
1 files changed, 42 insertions, 19 deletions
diff --git a/internal/handlers/main.go b/internal/handlers/main.go
index b907a34..edda1bf 100644
--- a/internal/handlers/main.go
+++ b/internal/handlers/main.go
@@ -13,9 +13,10 @@ import (
// Handlers structure
type Handlers struct {
- cfg config.Config
- log *slog.Logger
- repo repos.FullRepo
+ cfg config.Config
+ log *slog.Logger
+ repo repos.FullRepo
+ showNext bool // Tracks when to show next button
}
// NewHandlers constructor
@@ -79,6 +80,7 @@ func (h *Handlers) HandleAnswer(w http.ResponseWriter, r *http.Request) {
return
}
selectedIdx++ // in db index starts from 1
+ h.showNext = true
feedback := ""
if selectedIdx == int(question.CorrectIndex) {
feedback = `<div data-testid="feedback" class="feedback">Correct! 🎉</div>`
@@ -105,29 +107,50 @@ func getCorrectOption(q *models.Question) string {
}
}
-func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) {
+func (h *Handlers) HandleNextQuestion(w http.ResponseWriter, r *http.Request) {
+ currentID := r.URL.Query().Get("current_id")
+ nextID, _ := strconv.Atoi(currentID)
+ nextID++
+
+ question, err := h.repo.DBGetQuestion(strconv.Itoa(nextID))
+ if err != nil {
+ h.log.Error("failed to get next question", "error", err)
+ abortWithError(w, "No more questions")
+ return
+ }
+
+ h.showNext = false // Reset flag for new question
+ h.renderQuestion(w, question)
+}
+
+func (h *Handlers) renderQuestion(w http.ResponseWriter, question *models.Question) {
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
abortWithError(w, err.Error())
return
}
- testQuestion := &models.Question{
- ID: 1,
- Text: "___ du keine Zweifel daran? (Test Question)",
- Option1: "Haben",
- Option2: "Hast",
- Option3: "Hat",
- Option4: "Habt",
- CorrectIndex: 1,
- Requirement: "Choose the correct verb form",
- Explanation: "Use 'Hast' for 2nd person singular",
- Status: 0,
- ExamID: 1,
- MixedID: 101,
+
+ // Add ShowNext flag to template data
+ type TemplateData struct {
+ *models.Question
+ ShowNext bool
}
- err = tmpl.ExecuteTemplate(w, "main", testQuestion)
+
+ err = tmpl.ExecuteTemplate(w, "main", &TemplateData{
+ Question: question,
+ ShowNext: h.showNext,
+ })
if err != nil {
h.log.Error("failed to render template", "error", err)
- abortWithError(w, "Failed to render page")
}
}
+ question, err := h.repo.DBGetQuestion("1")
+ if err != nil {
+ h.log.Error("failed to get question", "error", err)
+ abortWithError(w, "Question not found")
+ return
+ }
+
+ h.showNext = false
+ h.renderQuestion(w, question)
+}