summaryrefslogtreecommitdiff
path: root/internal/handlers/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/handlers/main.go')
-rw-r--r--internal/handlers/main.go94
1 files changed, 64 insertions, 30 deletions
diff --git a/internal/handlers/main.go b/internal/handlers/main.go
index 4a64380..34f37d9 100644
--- a/internal/handlers/main.go
+++ b/internal/handlers/main.go
@@ -13,10 +13,9 @@ import (
// Handlers structure
type Handlers struct {
- cfg config.Config
- log *slog.Logger
- repo repos.FullRepo
- showNext bool // Tracks when to show next button
+ cfg config.Config
+ log *slog.Logger
+ repo repos.FullRepo
}
// NewHandlers constructor
@@ -80,16 +79,23 @@ func (h *Handlers) HandleAnswer(w http.ResponseWriter, r *http.Request) {
return
}
selectedIdx++ // in db index starts from 1
- h.showNext = true
- feedback := ""
+ // Render feedback section with full question state
+ tmpl, err := template.ParseGlob("components/*.html")
+ if err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ question.Status = 2
if selectedIdx == int(question.CorrectIndex) {
- feedback = `<div data-testid="feedback" class="feedback">Correct! 🎉</div>`
- } else {
- feedback = `<div data-testid="feedback" class="feedback">Wrong answer! The correct answer was: ` + getCorrectOption(question) + `</div>`
+ question.Status = 1
}
+ // Execute template with question data including status
w.Header().Set("Content-Type", "text/html")
- w.Write([]byte(feedback))
+ err = tmpl.ExecuteTemplate(w, "main", question)
+ if err != nil {
+ h.log.Error("failed to render feedback template", "error", err)
+ }
}
func getCorrectOption(q *models.Question) string {
@@ -107,10 +113,39 @@ func getCorrectOption(q *models.Question) string {
}
}
+func (h *Handlers) HandleMixedUbung(w http.ResponseWriter, r *http.Request) {
+ mixedID := r.URL.Query().Get("id")
+ if mixedID == "" {
+ h.log.Error("missing mixed ID parameter")
+ abortWithError(w, "Missing exercise ID")
+ return
+ }
+
+ questions, err := h.repo.DBGetQuestionsByMixedID(mixedID)
+ if err != nil {
+ h.log.Error("failed to get questions for mixed exercise", "error", err, "mixed_id", mixedID)
+ abortWithError(w, "Failed to load exercise")
+ return
+ }
+
+ if len(questions) == 0 {
+ h.log.Error("no questions found for mixed exercise", "mixed_id", mixedID)
+ abortWithError(w, "Exercise contains no questions")
+ return
+ }
+
+ // Render first question in the sequence
+ h.renderQuestion(w, &questions[0])
+}
+
func (h *Handlers) HandleNextQuestion(w http.ResponseWriter, r *http.Request) {
currentID := r.URL.Query().Get("current_id")
- nextID, _ := strconv.Atoi(currentID)
- nextID++
+ currID, err := strconv.Atoi(currentID)
+ if err != nil {
+ h.log.Error("invalid current question ID", "error", err, "current_id", currentID)
+ currID = 0 // Start from first question if invalid ID
+ }
+ nextID := currID + 1
question, err := h.repo.DBGetQuestion(strconv.Itoa(nextID))
if err != nil {
@@ -119,7 +154,8 @@ func (h *Handlers) HandleNextQuestion(w http.ResponseWriter, r *http.Request) {
return
}
- h.showNext = false // Reset flag for new question
+ h.log.Debug("returning new question", "q", question)
+
h.renderQuestion(w, question)
}
@@ -133,34 +169,32 @@ func (h *Handlers) renderQuestion(w http.ResponseWriter, question *models.Questi
// Add ShowNext flag to template data
type TemplateData struct {
*models.Question
- ShowNext bool
- Correct bool
}
- err = tmpl.ExecuteTemplate(w, "main", &TemplateData{
- Question: question,
- ShowNext: h.showNext,
- })
+ err = tmpl.ExecuteTemplate(w, "question", question)
if err != nil {
h.log.Error("failed to render template", "error", err)
}
- question, err = h.repo.DBGetQuestion("1")
- if err != nil {
- h.log.Error("failed to get question", "error", err)
- abortWithError(w, "Question not found")
- return
- }
}
func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) {
- question, err := h.repo.DBGetQuestion("1")
+ ubungs, err := h.repo.DBListMixed(10)
if err != nil {
- h.log.Error("failed to get question", "error", err)
- abortWithError(w, "Question not found")
+ h.log.Error("failed to get mixed ubungs", "error", err)
+ abortWithError(w, "Failed to load exercises")
return
}
- h.showNext = false
- h.renderQuestion(w, question)
+ tmpl, err := template.ParseGlob("components/*.html")
+ if err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+
+ w.Header().Set("Content-Type", "text/html")
+ err = tmpl.ExecuteTemplate(w, "main", ubungs)
+ if err != nil {
+ h.log.Error("failed to render template", "error", err)
+ }
}