more changes but unsure of state had to trash last set fof changes and try repair.
This commit is contained in:
64
handlers/home.go
Normal file
64
handlers/home.go
Normal file
@@ -0,0 +1,64 @@
|
||||
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)
|
||||
}
|
||||
|
||||
context := BuildTemplateContext(db, w, r)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user