Lots of changes and fixes

This commit is contained in:
2025-03-24 23:23:29 +00:00
parent 09b2ad8c56
commit d2013ec5c5
9 changed files with 116 additions and 54 deletions

View File

@@ -1,44 +1,50 @@
package handlers
import (
"database/sql"
"log"
"net/http"
"synlotto-website/helpers"
"synlotto-website/models"
"synlotto-website/storage"
)
func NewTicket(w http.ResponseWriter, r *http.Request) {
log.Println("🎟️ New ticket form opened")
func NewTicket(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("➡️ New ticket form opened")
err := Tmpl.ExecuteTemplate(w, "new_ticket", map[string]interface{}{
"Page": "new_ticket",
"Data": nil,
})
if err != nil {
log.Println("❌ Template error:", err)
http.Error(w, "Error rendering ticket form", http.StatusInternalServerError)
err := Tmpl.ExecuteTemplate(w, "new_ticket", map[string]interface{}{
"Page": "new_ticket",
"Data": nil,
})
if err != nil {
log.Println("❌ Template error:", err)
http.Error(w, "Error rendering form", http.StatusInternalServerError)
}
}
}
func SubmitTicket(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
func SubmitTicket(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ticket := models.MyTicket{
GameType: r.FormValue("game_type"),
DrawDate: r.FormValue("draw_date"),
Ball1: helpers.Atoi(r.FormValue("ball1")),
Ball2: helpers.Atoi(r.FormValue("ball2")),
Ball3: helpers.Atoi(r.FormValue("ball3")),
Ball4: helpers.Atoi(r.FormValue("ball4")),
Ball5: helpers.Atoi(r.FormValue("ball5")),
Bonus1: helpers.Nullable(helpers.Atoi(r.FormValue("bonus1"))),
Bonus2: helpers.Nullable(helpers.Atoi(r.FormValue("bonus2"))),
}
ticket := models.MyTicket{
DrawDate: r.FormValue("draw_date"),
Ball1: r.FormValue("ball1"),
Ball2: r.FormValue("ball2"),
Ball3: r.FormValue("ball3"),
Ball4: r.FormValue("ball4"),
Ball5: r.FormValue("ball5"),
Thunderball: r.FormValue("thunderball"),
if err := storage.InsertTicket(db, ticket); err != nil {
log.Println("❌ Failed to insert ticket:", err)
http.Error(w, "Error storing ticket", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
MyTickets = append(MyTickets, ticket)
log.Printf("🎟 Ticket for %s: %s %s %s %s %s + %s",
ticket.DrawDate,
ticket.Ball1, ticket.Ball2, ticket.Ball3, ticket.Ball4, ticket.Ball5, ticket.Thunderball,
)
http.Redirect(w, r, "/", http.StatusSeeOther)
}