From 1d052c05fdc627b4d5efb306c1f3344c2c1bd8c9 Mon Sep 17 00:00:00 2001 From: "Grail Finder (aider)" Date: Sat, 5 Apr 2025 16:32:59 +0300 Subject: feat: add next question button functionality --- internal/handlers/main.go | 61 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'internal/handlers/main.go') 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 = `
Correct! 🎉
` @@ -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) } -- cgit v1.2.3