1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
package handlers
import (
"demoon/config"
"demoon/internal/database/repos"
"demoon/internal/models"
"html/template"
"log/slog"
"net/http"
"os"
"strconv"
)
// Handlers structure
type Handlers struct {
cfg config.Config
log *slog.Logger
repo repos.FullRepo
}
// NewHandlers constructor
func NewHandlers(
cfg config.Config, l *slog.Logger, repo repos.FullRepo,
) *Handlers {
if l == nil {
l = slog.New(slog.NewJSONHandler(os.Stdout, nil))
}
h := &Handlers{
cfg: cfg,
log: l,
repo: repo,
}
return h
}
func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) {
h.log.Info("got ping request")
w.Write([]byte("pong"))
}
func (h *Handlers) HandleAnswer(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
h.log.Error("failed to parse form", "error", err)
abortWithError(w, "Invalid form data")
return
}
selectedIndex := r.FormValue("selected")
questionID := r.FormValue("question_id")
if selectedIndex == "" || questionID == "" {
h.log.Error("missing form values", "selected", selectedIndex, "question_id", questionID)
abortWithError(w, "Missing required fields")
return
}
h.log.Info("answer received",
"selected", selectedIndex,
"question_id", questionID)
question, err := h.repo.DBGetQuestion(questionID)
if err != nil {
h.log.Error("failed to get question", "error", err, "question_id", questionID)
abortWithError(w, "Question not found")
return
}
// Convert selectedIndex to int for comparison
selectedIdx, err := strconv.Atoi(selectedIndex)
if err != nil {
h.log.Error("invalid selected index", "error", err, "selected", selectedIndex)
abortWithError(w, "Invalid answer selection")
return
}
// Validate selected index is within bounds
if selectedIdx < 0 || selectedIdx > 3 {
h.log.Error("selected index out of bounds", "selected", selectedIndex)
abortWithError(w, "Invalid answer selection")
return
}
selectedIdx++ // in db index starts from 1
// 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) {
question.Status = 1
}
// Execute template with question data including status
w.Header().Set("Content-Type", "text/html")
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 {
switch q.CorrectIndex {
case 0:
return q.Option1
case 1:
return q.Option2
case 2:
return q.Option3
case 3:
return q.Option4
default:
return ""
}
}
func (h *Handlers) HandleNextQuestion(w http.ResponseWriter, r *http.Request) {
currentID := r.URL.Query().Get("current_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
}
h.log.Debug("returning new question", "q", question)
h.renderQuestion(w, question)
}
func (h *Handlers) renderQuestion(w http.ResponseWriter, question *models.Question) {
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
abortWithError(w, err.Error())
return
}
// Add ShowNext flag to template data
type TemplateData struct {
*models.Question
}
err = tmpl.ExecuteTemplate(w, "main", question)
if err != nil {
h.log.Error("failed to render template", "error", err)
}
}
func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) {
question, err := h.repo.DBGetQuestion("1")
if err != nil {
h.log.Error("failed to get question", "error", err)
abortWithError(w, "Question not found")
return
}
h.renderQuestion(w, question)
}
|