diff options
author | Grail Finder (aider) <wohilas@gmail.com> | 2025-03-29 14:44:08 +0300 |
---|---|---|
committer | Grail Finder (aider) <wohilas@gmail.com> | 2025-03-29 14:44:08 +0300 |
commit | 194e66b403fd6cb101707c26908c1bc6d81a6555 (patch) | |
tree | 48f1f497ec86f746403e28541c520a4bc7a4f18f | |
parent | 01a128d9f93960599c0f20beb640b5b035015893 (diff) |
feat: add answer handler with question ID tracking
-rw-r--r-- | components/index.html | 2 | ||||
-rw-r--r-- | internal/handlers/main.go | 17 | ||||
-rw-r--r-- | internal/server/router.go | 1 |
3 files changed, 19 insertions, 1 deletions
diff --git a/components/index.html b/components/index.html index 5626dbd..0c51047 100644 --- a/components/index.html +++ b/components/index.html @@ -17,7 +17,7 @@ {{range $index, $option := .Options}} <button hx-post="/answer" - hx-vals='{"selected": "{{$index}}"}' + hx-vals='{"selected": "{{$index}}", "question_id": "{{.ID}}"}' hx-target="#ancestor" hx-swap="outerHTML" class="option-button" diff --git a/internal/handlers/main.go b/internal/handlers/main.go index 3f2e1b2..1f3f740 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -37,6 +37,23 @@ func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { w.Write([]byte("pong")) } +func (h *Handlers) HandleAnswer(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + abortWithError(w, "Invalid form data") + return + } + + selectedIndex := r.FormValue("selected") + questionID := r.FormValue("question_id") + h.log.Info("answer received", + "selected", selectedIndex, + "question_id", questionID) + + // TODO: Add actual answer validation logic here + // For now just return the same question + h.MainPage(w, r) +} + func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { tmpl, err := template.ParseGlob("components/*.html") if err != nil { diff --git a/internal/server/router.go b/internal/server/router.go index 7034103..43a29b9 100644 --- a/internal/server/router.go +++ b/internal/server/router.go @@ -22,6 +22,7 @@ func (srv *server) ListenToRequests() { mux.HandleFunc("GET /ping", h.Ping) mux.HandleFunc("GET /", h.MainPage) + mux.HandleFunc("POST /answer", h.HandleAnswer) // mux.HandleFunc("POST /login", h.HandleLogin) // mux.HandleFunc("POST /signup", h.HandleSignup) |