diff options
-rw-r--r-- | components/index.html | 3 | ||||
-rw-r--r-- | components/question.html | 12 | ||||
-rw-r--r-- | internal/database/repos/questions.go | 4 | ||||
-rw-r--r-- | internal/handlers/main.go | 37 |
4 files changed, 40 insertions, 16 deletions
diff --git a/components/index.html b/components/index.html index 816fd21..3020e05 100644 --- a/components/index.html +++ b/components/index.html @@ -9,7 +9,7 @@ <script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script> </head> <body> - <div class="container"> + <div id="ancestor"> <h1>Mixed Übungen</h1> <div class="ubung-list"> {{range .}} @@ -24,7 +24,6 @@ </p> {{end}} </div> - <div id="ancestor"></div> </div> </body> </html> diff --git a/components/question.html b/components/question.html index ef4618b..506e450 100644 --- a/components/question.html +++ b/components/question.html @@ -20,8 +20,8 @@ {{.Explanation}} </div> <button - hx-get="/next-question?current_id={{.ID}}" - hx-target="#ancestor" + hx-get="/next-question?current_id={{.ID}}&mixed_id={{.MixedID}}" + hx-target="#question" hx-swap="outerHTML" class="next-button" data-testid="next-button"> @@ -34,7 +34,7 @@ data-testid="option1" hx-post="/answer" hx-vals='{"selected": "0", "question_id": "{{.ID}}"}' - hx-target="#ancestor" + hx-target="#question" hx-swap="innerHTML" class="option-button" > @@ -44,7 +44,7 @@ data-testid="option2" hx-post="/answer" hx-vals='{"selected": "1", "question_id": "{{.ID}}"}' - hx-target="#ancestor" + hx-target="#question" hx-swap="innerHTML" class="option-button" > @@ -54,7 +54,7 @@ data-testid="option3" hx-post="/answer" hx-vals='{"selected": "2", "question_id": "{{.ID}}"}' - hx-target="#ancestor" + hx-target="#question" hx-swap="innerHTML" class="option-button" > @@ -64,7 +64,7 @@ data-testid="option4" hx-post="/answer" hx-vals='{"selected": "3", "question_id": "{{.ID}}"}' - hx-target="#ancestor" + hx-target="#question" hx-swap="innerHTML" class="option-button" > 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) } |