This commit is contained in:
2025-03-25 09:11:23 +00:00
parent f4a8983b48
commit 3598cb5fd9
7 changed files with 92 additions and 11 deletions

View File

@@ -10,6 +10,7 @@ var Tmpl = template.Must(template.ParseFiles(
"templates/index.html",
"templates/new_draw.html",
"templates/new_ticket.html",
"templates/tickets.html",
))
var Draws []models.ThunderballResult

View File

@@ -48,3 +48,45 @@ func SubmitTicket(db *sql.DB) http.HandlerFunc {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
func ListTickets(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("📋 Tickets page hit")
rows, err := db.Query(`
SELECT id, game_type, draw_date, ball1, ball2, ball3, ball4, ball5, bonus1, bonus2, duplicate
FROM my_tickets
ORDER BY draw_date DESC
`)
if err != nil {
log.Println("❌ Failed to query tickets:", err)
http.Error(w, "Could not load tickets", http.StatusInternalServerError)
return
}
defer rows.Close()
var tickets []models.MyTicket
for rows.Next() {
var t models.MyTicket
err := rows.Scan(
&t.Id, &t.GameType, &t.DrawDate,
&t.Ball1, &t.Ball2, &t.Ball3, &t.Ball4, &t.Ball5,
&t.Bonus1, &t.Bonus2, &t.Duplicate,
)
if err != nil {
log.Println("❌ Row scan error:", err)
continue
}
tickets = append(tickets, t)
}
err = Tmpl.ExecuteTemplate(w, "tickets", map[string]any{
"Page": "tickets",
"Data": tickets,
})
if err != nil {
log.Println("❌ Template rendering error:", err)
http.Error(w, "Could not render page", http.StatusInternalServerError)
}
}
}

View File

@@ -20,6 +20,8 @@ func main() {
handlers.Submit(w, r)
case "/ticket":
handlers.NewTicket(db)
case "/tickets":
handlers.ListTickets(db)
case "/submit-ticket":
handlers.SubmitTicket(db)
default:

View File

@@ -11,4 +11,5 @@ type MyTicket struct {
Ball5 int
Bonus1 *int
Bonus2 *int
Duplicate bool
}

View File

@@ -30,7 +30,6 @@ func InsertThunderballResult(db *sql.DB, res models.ThunderballResult) error {
}
func InsertTicket(db *sql.DB, ticket models.MyTicket) error {
// Convert optional fields to interface{} using manual check
var bonus1Val interface{}
var bonus2Val interface{}

36
templates/tickets.html Normal file
View File

@@ -0,0 +1,36 @@
{{ define "tickets" }}
{{ define "content" }}
<a href="/">← Back to Home</a>
<h2>My Tickets</h2>
<table>
<thead>
<tr>
<th>Date</th>
<th>Game</th>
<th>Numbers</th>
<th>Bonus</th>
<th>Duplicate?</th>
</tr>
</thead>
<tbody>
{{ range . }}
<tr>
<td>{{ .DrawDate }}</td>
<td>{{ .GameType }}</td>
<td>{{ .Ball1 }}, {{ .Ball2 }}, {{ .Ball3 }}, {{ .Ball4 }}, {{ .Ball5 }}</td>
<td>
{{ if .Bonus1 }}{{ .Bonus1 }}{{ end }}
{{ if .Bonus2 }}, {{ .Bonus2 }}{{ end }}
</td>
<td>
{{ if .Duplicate }}⚠️ Yes{{ else }}✔️ No{{ end }}
</td>
</tr>
{{ else }}
<tr><td colspan="5">No tickets logged yet.</td></tr>
{{ end }}
</tbody>
</table>
{{ template "layout" . }}
{{ end }}

Binary file not shown.