From 769589c59b9c7c29b11060fe19cec9f0f1ae9603 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Sat, 19 Apr 2025 07:21:49 +0300 Subject: Feat: get next question in mixed group --- internal/database/repos/questions.go | 4 ++-- internal/handlers/main.go | 37 ++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 8 deletions(-) (limited to 'internal') diff --git a/internal/database/repos/questions.go b/internal/database/repos/questions.go index 9d0396e..5a05945 100644 --- a/internal/database/repos/questions.go +++ b/internal/database/repos/questions.go @@ -9,7 +9,7 @@ type QuestionsRepo interface { DBGetQuestion(id string) (*models.Question, error) DBGetMixedUbung(id uint32) (*models.MixedUbung, error) DBListMixed(limit uint32) ([]models.MixedUbung, error) - DBGetQuestionsByMixedID(id string) ([]models.Question, error) + DBGetQuestionsByMixedID(id uint32) ([]models.Question, error) } func (p *Provider) DBGetMixedUbung(id uint32) (*models.MixedUbung, error) { @@ -34,7 +34,7 @@ func (p *Provider) DBListMixed(limit uint32) ([]models.MixedUbung, error) { return ubungs, nil } -func (p *Provider) DBGetQuestionsByMixedID(id string) ([]models.Question, error) { +func (p *Provider) DBGetQuestionsByMixedID(id uint32) ([]models.Question, error) { var questions []models.Question err := p.db.Select(&questions, "SELECT * FROM questions WHERE mixed_id = ? ORDER BY id", id) if err != nil { diff --git a/internal/handlers/main.go b/internal/handlers/main.go index 34f37d9..79d92f4 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -92,7 +92,7 @@ func (h *Handlers) HandleAnswer(w http.ResponseWriter, r *http.Request) { // Execute template with question data including status w.Header().Set("Content-Type", "text/html") - err = tmpl.ExecuteTemplate(w, "main", question) + err = tmpl.ExecuteTemplate(w, "question", question) if err != nil { h.log.Error("failed to render feedback template", "error", err) } @@ -120,8 +120,13 @@ func (h *Handlers) HandleMixedUbung(w http.ResponseWriter, r *http.Request) { abortWithError(w, "Missing exercise ID") return } - - questions, err := h.repo.DBGetQuestionsByMixedID(mixedID) + mid, err := strconv.ParseUint(mixedID, 10, 64) + if err != nil { + h.log.Error("failed to convert mixedid", "mixed_id", mixedID) + abortWithError(w, "failed to convert mixed_id") + return + } + questions, err := h.repo.DBGetQuestionsByMixedID(uint32(mid)) if err != nil { h.log.Error("failed to get questions for mixed exercise", "error", err, "mixed_id", mixedID) abortWithError(w, "Failed to load exercise") @@ -140,22 +145,42 @@ func (h *Handlers) HandleMixedUbung(w http.ResponseWriter, r *http.Request) { func (h *Handlers) HandleNextQuestion(w http.ResponseWriter, r *http.Request) { currentID := r.URL.Query().Get("current_id") + mixedID := r.FormValue("mixed_id") 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 { h.log.Error("failed to get next question", "error", err) abortWithError(w, "No more questions") return } - + if mixedID != "" { + mid, err := strconv.ParseUint(mixedID, 10, 64) + if err != nil { + h.log.Error("failed to convert mixedid", "mixed_id", mixedID) + abortWithError(w, "failed to convert mixed_id") + return + } + qList, err := h.repo.DBGetQuestionsByMixedID(uint32(mid)) + if err != nil { + h.log.Error("failed to retrieve questions", "mixed_id", mid) + abortWithError(w, "failed to retrieve questions") + return + } + for i, q := range qList { + if q.ID > uint32(nextID) { + // load next question + question = &q + h.log.Error("returning question from mixed group", "#", i) + break + } + } + } h.log.Debug("returning new question", "q", question) - h.renderQuestion(w, question) } -- cgit v1.2.3