177b4e8fd8
- Full library home page: question list with per-user mastery stats,
search/source filter, sort (A-Z, weakest first, most-seen), source
breakdown in header, Take a test CTA.
- GET/POST /questions/{id}: view and edit question text, source, answers;
radio-select correct answer; shows seen×/correct% stat.
- POST /questions/{id}/delete: hard delete (cascades to answers via FK).
- repo: ListQuestions supports SortWeakest/SortMostSeen via LEFT JOIN;
added CountBySource, UpdateQuestion, UpdateAnswers, DeleteQuestion.
- render: added pct template func (correct*100/seen).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"html/template"
|
|
"log/slog"
|
|
"net/http"
|
|
"path/filepath"
|
|
|
|
"qbank/internal/auth"
|
|
)
|
|
|
|
var tmplFuncs = template.FuncMap{
|
|
"inc": func(i int) int { return i + 1 },
|
|
"pct": func(correct, seen int) int {
|
|
if seen == 0 {
|
|
return 0
|
|
}
|
|
return correct * 100 / seen
|
|
},
|
|
}
|
|
|
|
// Renderer parses and executes HTML templates from a directory.
|
|
type Renderer struct {
|
|
dir string
|
|
}
|
|
|
|
func NewRenderer(dir string) *Renderer { return &Renderer{dir: dir} }
|
|
|
|
// Render executes layout.html + <name>.html, passing data to the "layout" template.
|
|
func (r *Renderer) Render(w http.ResponseWriter, status int, name string, data any) {
|
|
t, err := template.New("").Funcs(tmplFuncs).ParseFiles(
|
|
filepath.Join(r.dir, "layout.html"),
|
|
filepath.Join(r.dir, name+".html"),
|
|
)
|
|
if err != nil {
|
|
slog.Error("parse template", "name", name, "err", err)
|
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.WriteHeader(status)
|
|
if err := t.ExecuteTemplate(w, "layout", data); err != nil {
|
|
slog.Error("execute template", "name", name, "err", err)
|
|
}
|
|
}
|
|
|
|
// HTTPError writes a plain-text HTTP error.
|
|
func HTTPError(w http.ResponseWriter, status int) {
|
|
http.Error(w, http.StatusText(status), status)
|
|
}
|
|
|
|
// BaseData builds the common template map (User, CSRFToken, Flash).
|
|
func BaseData(a *auth.Manager, r *http.Request) map[string]any {
|
|
return map[string]any{
|
|
"User": auth.UserFromCtx(r.Context()),
|
|
"CSRFToken": a.CSRFToken(r),
|
|
"Flash": a.PopFlash(r),
|
|
}
|
|
}
|