- Replaced legacy TemplateContext calls with structured TemplateData usage - Removed unused variables and redundant storage calls in notifications handler - Ensured consistent use of BuildTemplateData across user-facing handlers - Resolved all compile-time errors from refactor - Ready for runtime testing and further layout integration
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"sort"
|
|
"synlotto-website/helpers"
|
|
"synlotto-website/models"
|
|
)
|
|
|
|
// Home shows latest Thunderball results
|
|
func Home(db *sql.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
rows, err := db.Query(`
|
|
SELECT id, draw_date, machine, ballset, ball1, ball2, ball3, ball4, ball5, thunderball
|
|
FROM results_thunderball
|
|
ORDER BY id DESC
|
|
`)
|
|
if err != nil {
|
|
log.Println("❌ DB error:", err)
|
|
http.Error(w, "Database error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
var results []models.ThunderballResult
|
|
|
|
for rows.Next() {
|
|
var res models.ThunderballResult
|
|
err := rows.Scan(
|
|
&res.Id, &res.DrawDate, &res.Machine, &res.BallSet,
|
|
&res.Ball1, &res.Ball2, &res.Ball3, &res.Ball4, &res.Ball5, &res.Thunderball,
|
|
)
|
|
if err != nil {
|
|
log.Println("❌ Row scan error:", err)
|
|
continue
|
|
}
|
|
|
|
res.SortedBalls = []int{
|
|
res.Ball1, res.Ball2, res.Ball3, res.Ball4, res.Ball5,
|
|
}
|
|
sort.Ints(res.SortedBalls)
|
|
|
|
results = append(results, res)
|
|
}
|
|
|
|
data := BuildTemplateData(db, w, r)
|
|
context := helpers.TemplateContext(w, r, data)
|
|
context["Data"] = results
|
|
|
|
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
|
"templates/layout.html",
|
|
"templates/topbar.html",
|
|
"templates/index.html",
|
|
))
|
|
|
|
err = tmpl.ExecuteTemplate(w, "layout", context)
|
|
if err != nil {
|
|
log.Println("❌ Template error:", err)
|
|
http.Error(w, "Error rendering homepage", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
}
|