Get ticket functionality.

This commit is contained in:
2025-03-26 15:33:40 +00:00
parent 66abdbdd4d
commit f7b54db7c1
4 changed files with 94 additions and 17 deletions

View File

@@ -270,27 +270,32 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) { return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
userID, ok := helpers.GetCurrentUserID(r) userID, ok := helpers.GetCurrentUserID(r)
if !ok { if !ok {
log.Println("🚫 Not logged in, redirecting")
http.Redirect(w, r, "/login", http.StatusSeeOther) http.Redirect(w, r, "/login", http.StatusSeeOther)
return return
} }
log.Printf("👤 Fetching tickets for user ID: %d\n", userID)
rows, err := db.Query(` rows, err := db.Query(`
SELECT id, game_type, draw_date, SELECT id, game_type, draw_date,
ball1, ball2, ball3, ball4, ball5, ball6, ball1, ball2, ball3, ball4, ball5, ball6,
bonus1, bonus2, bonus1, bonus2,
purchase_method, purchase_date, image_path purchase_method, purchase_date, image_path
FROM my_tickets FROM my_tickets
WHERE user_id = ? WHERE userId = ?
ORDER BY draw_date DESC, created_at DESC ORDER BY draw_date DESC, created_at DESC
`, userID) `, userID)
if err != nil { if err != nil {
log.Println("❌ Failed to load user tickets:", err) log.Println("❌ Query failed:", err)
http.Error(w, "Error loading tickets", http.StatusInternalServerError) http.Error(w, "Could not load tickets", http.StatusInternalServerError)
return return
} }
defer rows.Close() defer rows.Close()
var tickets []models.MyTicket var tickets []models.MyTicket
for rows.Next() { for rows.Next() {
var t models.MyTicket var t models.MyTicket
err := rows.Scan( err := rows.Scan(
@@ -299,11 +304,15 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
&t.Bonus1, &t.Bonus2, &t.Bonus1, &t.Bonus2,
&t.PurchaseMethod, &t.PurchaseDate, &t.ImagePath, &t.PurchaseMethod, &t.PurchaseDate, &t.ImagePath,
) )
if err == nil { if err != nil {
tickets = append(tickets, t) log.Println("⚠️ Failed to scan ticket row:", err)
continue
} }
tickets = append(tickets, t)
} }
log.Printf("📄 Loaded %d tickets\n", len(tickets))
context := helpers.TemplateContext(w, r) context := helpers.TemplateContext(w, r)
context["Tickets"] = tickets context["Tickets"] = tickets
@@ -311,6 +320,11 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
"templates/layout.html", "templates/layout.html",
"templates/account/tickets/my_tickets.html", "templates/account/tickets/my_tickets.html",
)) ))
tmpl.ExecuteTemplate(w, "layout", context)
err = tmpl.ExecuteTemplate(w, "layout", context)
if err != nil {
log.Println("❌ Template error:", err)
http.Error(w, "Error rendering page", http.StatusInternalServerError)
}
}) })
} }

View File

@@ -23,6 +23,12 @@ func TemplateFuncs() template.FuncMap {
} }
return b return b
}, },
"intVal": func(p *int) int {
if p == nil {
return 0
}
return *p
},
} }
} }

View File

@@ -1,15 +1,19 @@
package models package models
type MyTicket struct { type MyTicket struct {
Id int Id int
GameType string GameType string
DrawDate string DrawDate string
Ball1 int Ball1 int
Ball2 int Ball2 int
Ball3 int Ball3 int
Ball4 int Ball4 int
Ball5 int Ball5 int
Bonus1 *int Ball6 int
Bonus2 *int Bonus1 *int
Duplicate bool Bonus2 *int
PurchaseMethod string
PurchaseDate string
ImagePath string
Duplicate bool
} }

View File

@@ -0,0 +1,53 @@
{{ define "content" }}
<a href="/account/tickets/add_ticket">+ Add Ticket</a>
<h2>My Tickets</h2>
{{ if eq (len .Tickets) 0 }}
<p>You havent logged any tickets yet.</p>
{{ else }}
<table>
<thead>
<tr>
<th>Game</th>
<th>Draw Date</th>
<th>Numbers</th>
<th>Bonus</th>
<th>Purchased</th>
<th>Via</th>
<th>Image</th>
</tr>
</thead>
<tbody>
{{ range .Tickets }}
<tr>
<td>{{ .GameType }}</td>
<td>{{ .DrawDate }}</td>
<td>
{{ .Ball1 }} {{ .Ball2 }} {{ .Ball3 }} {{ .Ball4 }} {{ .Ball5 }}
{{ if ne .Ball6 0 }} {{ .Ball6 }} {{ end }}
</td>
<td>
{{ if eq .GameType "Lotto" }}
<span style="color: lightgray; font-style: italic;"></span>
{{ else if gt (intVal .Bonus2) 0 }}
{{ if gt (intVal .Bonus1) 0 }}{{ intVal .Bonus1 }}, {{ end }}
{{ intVal .Bonus2 }}
{{ else if gt (intVal .Bonus1) 0 }}
{{ intVal .Bonus1 }}
{{ else }}
<span style="color: lightgray;"></span>
{{ end }}
</td>
<td>{{ .PurchaseDate }}</td>
<td>{{ .PurchaseMethod }}</td>
<td>
{{ if .ImagePath }}
<a href="/{{ .ImagePath }}" target="_blank">View</a>
{{ else }}{{ end }}
</td>
</tr>
{{ end }}
</tbody>
</table>
{{ end }}
{{ end }}