diff options
author | Grail Finder (aider) <wohilas@gmail.com> | 2025-04-06 09:27:45 +0300 |
---|---|---|
committer | Grail Finder (aider) <wohilas@gmail.com> | 2025-04-06 09:27:45 +0300 |
commit | ad29977e1ad64439f2dd620849439e40c8b97860 (patch) | |
tree | 2dce8be52b2446f95188549e8b42f9e57f1da17f | |
parent | 42018ec3137dd2af5c84567e7b603f0eaf055048 (diff) |
feat: add MixedUbung buttons to main page
-rw-r--r-- | components/index.html | 15 | ||||
-rw-r--r-- | internal/database/repos/questions.go | 10 | ||||
-rw-r--r-- | internal/handlers/main.go | 19 |
3 files changed, 39 insertions, 5 deletions
diff --git a/components/index.html b/components/index.html index 00df90f..b8b126d 100644 --- a/components/index.html +++ b/components/index.html @@ -9,7 +9,20 @@ <script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script> </head> <body> - <div id="ancestor"> + <div class="container"> + <h1>Mixed Übungen</h1> + <div class="ubung-list"> + {{range .}} + <button + class="ubung-button" + hx-get="/question?mixed_id={{.ID}}" + hx-target="#ancestor" + hx-swap="outerHTML"> + {{.Name}} (Level {{.LevelID}}) + </button> + {{end}} + </div> + <div id="ancestor"></div> </div> </body> </html> diff --git a/internal/database/repos/questions.go b/internal/database/repos/questions.go index ab5e093..daf7e16 100644 --- a/internal/database/repos/questions.go +++ b/internal/database/repos/questions.go @@ -5,6 +5,7 @@ import "demoon/internal/models" type QuestionsRepo interface { DBGetQuestion(id string) (*models.Question, error) DBGetMixedUbung(id uint32) (*models.MixedUbung, error) + DBGetFirst10MixedUbungs() ([]*models.MixedUbung, error) } func (p *Provider) DBGetMixedUbung(id uint32) (*models.MixedUbung, error) { @@ -16,6 +17,15 @@ func (p *Provider) DBGetMixedUbung(id uint32) (*models.MixedUbung, error) { return &ubung, nil } +func (p *Provider) DBGetFirst10MixedUbungs() ([]*models.MixedUbung, error) { + var ubungs []*models.MixedUbung + err := p.db.Select(&ubungs, "SELECT * FROM Table_Mixed ORDER BY MixedID LIMIT 10") + if err != nil { + return nil, err + } + return ubungs, nil +} + func (p *Provider) DBGetQuestion(id string) (*models.Question, error) { var question models.Question err := p.db.Get(&question, "SELECT * FROM questions WHERE id = ?", id) diff --git a/internal/handlers/main.go b/internal/handlers/main.go index 41ff85f..4aa8eca 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -154,11 +154,22 @@ func (h *Handlers) renderQuestion(w http.ResponseWriter, question *models.Questi } func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { - question, err := h.repo.DBGetQuestion("1") + ubungs, err := h.repo.DBGetFirst10MixedUbungs() 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.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) + } } |