Add tickets to db.

This commit is contained in:
2025-03-26 13:26:43 +00:00
parent f001cfe35e
commit 66abdbdd4d
5 changed files with 231 additions and 42 deletions

View File

@@ -92,42 +92,71 @@ func AddTicket(db *sql.DB) http.HandlerFunc {
}
}
ballCount := 6
bonusCount := 2
var ballCount, bonusCount int
switch game {
case "Thunderball":
ballCount, bonusCount = 5, 1
case "Lotto":
ballCount, bonusCount = 6, 0
case "EuroMillions":
ballCount, bonusCount = 5, 2
case "SetForLife":
ballCount, bonusCount = 5, 1
default:
http.Error(w, "Unsupported game type", http.StatusBadRequest)
return
}
balls := make([][]int, ballCount)
bonuses := make([][]int, bonusCount)
for i := 1; i <= ballCount; i++ {
balls[i-1] = helpers.ParseIntSlice(r.Form["ball"+strconv.Itoa(i)])
log.Printf("🔢 ball%d: %v", i, balls[i-1])
field := fmt.Sprintf("ball%d[]", i)
balls[i-1] = helpers.ParseIntSlice(r.Form[field])
log.Printf("🔢 %s: %v", field, balls[i-1])
}
for i := 1; i <= bonusCount; i++ {
bonuses[i-1] = helpers.ParseIntSlice(r.Form["bonus"+strconv.Itoa(i)])
log.Printf("🎯 bonus%d: %v", i, bonuses[i-1])
field := fmt.Sprintf("bonus%d[]", i)
bonuses[i-1] = helpers.ParseIntSlice(r.Form[field])
log.Printf("🎯 %s: %v", field, bonuses[i-1])
}
lineCount := len(balls[0])
lineCount := 0
if len(balls) > 0 {
lineCount = len(balls[0])
}
log.Println("🧾 Total lines to insert:", lineCount)
for i := 0; i < lineCount; i++ {
var b [6]int
var bo [2]int
b := make([]int, 6)
bo := make([]int, 2)
valid := true
for j := 0; j < ballCount; j++ {
if j < len(balls) && i < len(balls[j]) {
b[j] = balls[j][i]
if b[j] == 0 {
valid = false
}
}
}
for j := 0; j < bonusCount; j++ {
if j < len(bonuses) && i < len(bonuses[j]) {
bo[j] = bonuses[j][i]
if bo[j] == 0 {
valid = false
}
}
}
if !valid {
log.Printf("⚠️ Skipping invalid line %d (incomplete values)", i+1)
continue
}
_, err := db.Exec(`
INSERT INTO my_tickets (
user_id, game_type, draw_date,
userId, game_type, draw_date,
ball1, ball2, ball3, ball4, ball5, ball6,
bonus1, bonus2,
purchase_method, purchase_date, image_path
@@ -237,23 +266,26 @@ func SubmitTicket(db *sql.DB) http.HandlerFunc {
})
}
func ListTickets(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("📋 Tickets page hit")
tmpl := template.Must(template.ParseFiles(
"templates/layout.html",
"templates/tickets.html",
))
func GetMyTickets(db *sql.DB) http.HandlerFunc {
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
userID, ok := helpers.GetCurrentUserID(r)
if !ok {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
rows, err := db.Query(`
SELECT id, game_type, draw_date, ball1, ball2, ball3, ball4, ball5, bonus1, bonus2, duplicate
SELECT id, game_type, draw_date,
ball1, ball2, ball3, ball4, ball5, ball6,
bonus1, bonus2,
purchase_method, purchase_date, image_path
FROM my_tickets
ORDER BY draw_date DESC
`)
WHERE user_id = ?
ORDER BY draw_date DESC, created_at DESC
`, userID)
if err != nil {
log.Println("❌ Failed to query tickets:", err)
http.Error(w, "Could not load tickets", http.StatusInternalServerError)
log.Println("❌ Failed to load user tickets:", err)
http.Error(w, "Error loading tickets", http.StatusInternalServerError)
return
}
defer rows.Close()
@@ -263,23 +295,22 @@ func ListTickets(db *sql.DB) http.HandlerFunc {
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,
&t.Ball1, &t.Ball2, &t.Ball3, &t.Ball4, &t.Ball5, &t.Ball6,
&t.Bonus1, &t.Bonus2,
&t.PurchaseMethod, &t.PurchaseDate, &t.ImagePath,
)
if err != nil {
log.Println("❌ Row scan error:", err)
continue
if err == nil {
tickets = append(tickets, t)
}
tickets = append(tickets, t)
}
err = tmpl.ExecuteTemplate(w, "layout", 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)
}
}
context := helpers.TemplateContext(w, r)
context["Tickets"] = tickets
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/account/tickets/my_tickets.html",
))
tmpl.ExecuteTemplate(w, "layout", context)
})
}