Get ticket functionality.
This commit is contained in:
@@ -270,27 +270,32 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
|
||||
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
||||
userID, ok := helpers.GetCurrentUserID(r)
|
||||
if !ok {
|
||||
log.Println("🚫 Not logged in, redirecting")
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("👤 Fetching tickets for user ID: %d\n", userID)
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT id, game_type, draw_date,
|
||||
ball1, ball2, ball3, ball4, ball5, ball6,
|
||||
bonus1, bonus2,
|
||||
purchase_method, purchase_date, image_path
|
||||
FROM my_tickets
|
||||
WHERE user_id = ?
|
||||
WHERE userId = ?
|
||||
ORDER BY draw_date DESC, created_at DESC
|
||||
`, userID)
|
||||
|
||||
if err != nil {
|
||||
log.Println("❌ Failed to load user tickets:", err)
|
||||
http.Error(w, "Error loading tickets", http.StatusInternalServerError)
|
||||
log.Println("❌ Query failed:", 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(
|
||||
@@ -299,11 +304,15 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
|
||||
&t.Bonus1, &t.Bonus2,
|
||||
&t.PurchaseMethod, &t.PurchaseDate, &t.ImagePath,
|
||||
)
|
||||
if err == nil {
|
||||
tickets = append(tickets, t)
|
||||
if err != nil {
|
||||
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["Tickets"] = tickets
|
||||
|
||||
@@ -311,6 +320,11 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
|
||||
"templates/layout.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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -23,6 +23,12 @@ func TemplateFuncs() template.FuncMap {
|
||||
}
|
||||
return b
|
||||
},
|
||||
"intVal": func(p *int) int {
|
||||
if p == nil {
|
||||
return 0
|
||||
}
|
||||
return *p
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
package models
|
||||
|
||||
type MyTicket struct {
|
||||
Id int
|
||||
GameType string
|
||||
DrawDate string
|
||||
Ball1 int
|
||||
Ball2 int
|
||||
Ball3 int
|
||||
Ball4 int
|
||||
Ball5 int
|
||||
Bonus1 *int
|
||||
Bonus2 *int
|
||||
Duplicate bool
|
||||
Id int
|
||||
GameType string
|
||||
DrawDate string
|
||||
Ball1 int
|
||||
Ball2 int
|
||||
Ball3 int
|
||||
Ball4 int
|
||||
Ball5 int
|
||||
Ball6 int
|
||||
Bonus1 *int
|
||||
Bonus2 *int
|
||||
PurchaseMethod string
|
||||
PurchaseDate string
|
||||
ImagePath string
|
||||
Duplicate bool
|
||||
}
|
||||
|
||||
53
templates/account/tickets/my_tickets.html
Normal file
53
templates/account/tickets/my_tickets.html
Normal 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 haven’t 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 }}
|
||||
Reference in New Issue
Block a user