add and refine thunderball results page
This commit is contained in:
@@ -1,29 +1,67 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"sort"
|
||||
"synlotto-website/helpers"
|
||||
"synlotto-website/models"
|
||||
|
||||
"github.com/gorilla/csrf"
|
||||
)
|
||||
|
||||
func Home(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("✅ Home hit")
|
||||
func Home(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("✅ Home hit")
|
||||
|
||||
tmpl := template.Must(template.ParseFiles(
|
||||
"templates/layout.html",
|
||||
"templates/index.html",
|
||||
))
|
||||
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 {
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
log.Println("❌ DB error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
err := tmpl.ExecuteTemplate(w, "layout", map[string]interface{}{
|
||||
"Data": Draws,
|
||||
})
|
||||
if err != nil {
|
||||
log.Println("❌ Template error:", err)
|
||||
http.Error(w, "Error rendering homepage", http.StatusInternalServerError)
|
||||
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
|
||||
}
|
||||
|
||||
// ✅ Add sorted ball list
|
||||
res.SortedBalls = []int{
|
||||
res.Ball1, res.Ball2, res.Ball3, res.Ball4, res.Ball5,
|
||||
}
|
||||
sort.Ints(res.SortedBalls)
|
||||
|
||||
results = append(results, res)
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFiles(
|
||||
"templates/layout.html",
|
||||
"templates/index.html",
|
||||
))
|
||||
|
||||
err = tmpl.ExecuteTemplate(w, "layout", map[string]interface{}{
|
||||
"Data": results,
|
||||
})
|
||||
if err != nil {
|
||||
log.Println("❌ Template error:", err)
|
||||
http.Error(w, "Error rendering homepage", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +91,7 @@ func Submit(w http.ResponseWriter, r *http.Request) {
|
||||
draw := models.ThunderballResult{
|
||||
DrawDate: r.FormValue("date"),
|
||||
Machine: r.FormValue("machine"),
|
||||
Ballset: helpers.Atoi(r.FormValue("ballset")),
|
||||
BallSet: helpers.Atoi(r.FormValue("ballSet")),
|
||||
Ball1: helpers.Atoi(r.FormValue("ball1")),
|
||||
Ball2: helpers.Atoi(r.FormValue("ball2")),
|
||||
Ball3: helpers.Atoi(r.FormValue("ball3")),
|
||||
@@ -65,7 +103,7 @@ func Submit(w http.ResponseWriter, r *http.Request) {
|
||||
Draws = append(Draws, draw)
|
||||
|
||||
log.Printf("📅 %s | 🛠 %s | 🎱 %d | 🔢 %d,%d,%d,%d,%d | ⚡ %d\n",
|
||||
draw.DrawDate, draw.Machine, draw.Ballset,
|
||||
draw.DrawDate, draw.Machine, draw.BallSet,
|
||||
draw.Ball1, draw.Ball2, draw.Ball3, draw.Ball4, draw.Ball5, draw.Thunderball)
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
|
||||
Reference in New Issue
Block a user