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

@@ -12,5 +12,5 @@ var Tmpl = template.Must(template.ParseFiles(
"templates/new_ticket.html", "templates/new_ticket.html",
)) ))
var Draws []models.Draw var Draws []models.ThunderballResult
var MyTickets []models.MyTicket var MyTickets []models.MyTicket

View File

@@ -3,6 +3,7 @@ package handlers
import ( import (
"log" "log"
"net/http" "net/http"
"synlotto-website/helpers"
"synlotto-website/models" "synlotto-website/models"
) )
@@ -36,22 +37,22 @@ func Submit(w http.ResponseWriter, r *http.Request) {
log.Println("📝 Form submission received") log.Println("📝 Form submission received")
r.ParseForm() r.ParseForm()
draw := models.Draw{ draw := models.ThunderballResult{
Date: r.FormValue("date"), DrawDate: r.FormValue("date"),
Machine: r.FormValue("machine"), Machine: r.FormValue("machine"),
Ballset: r.FormValue("ballset"), Ballset: helpers.Atoi(r.FormValue("ballset")),
Ball1: r.FormValue("ball1"), Ball1: helpers.Atoi(r.FormValue("ball1")),
Ball2: r.FormValue("ball2"), Ball2: helpers.Atoi(r.FormValue("ball2")),
Ball3: r.FormValue("ball3"), Ball3: helpers.Atoi(r.FormValue("ball3")),
Ball4: r.FormValue("ball4"), Ball4: helpers.Atoi(r.FormValue("ball4")),
Ball5: r.FormValue("ball5"), Ball5: helpers.Atoi(r.FormValue("ball5")),
Thunderball: r.FormValue("thunderball"), Thunderball: helpers.Atoi(r.FormValue("thunderball")),
} }
Draws = append(Draws, draw) Draws = append(Draws, draw)
log.Printf("📅 %s | 🛠 %s | 🎱 %s | 🔢 %s,%s,%s,%s,%s | ⚡ %s\n", log.Printf("📅 %s | 🛠 %s | 🎱 %d | 🔢 %d,%d,%d,%d,%d | ⚡ %d\n",
draw.Date, draw.Machine, draw.Ballset, draw.DrawDate, draw.Machine, draw.Ballset,
draw.Ball1, draw.Ball2, draw.Ball3, draw.Ball4, draw.Ball5, draw.Thunderball) draw.Ball1, draw.Ball2, draw.Ball3, draw.Ball4, draw.Ball5, draw.Thunderball)
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)

View File

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

8
helpers/strconv.go Normal file
View File

@@ -0,0 +1,8 @@
package helpers
import "strconv"
func Atoi(s string) int {
n, _ := strconv.Atoi(s)
return n
}

View File

@@ -4,9 +4,12 @@ import (
"log" "log"
"net/http" "net/http"
"synlotto-website/handlers" "synlotto-website/handlers"
"synlotto-website/storage"
) )
func main() { func main() {
db := storage.InitDB("synlotto.db")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path { switch r.URL.Path {
case "/": case "/":
@@ -16,14 +19,14 @@ func main() {
case "/submit": case "/submit":
handlers.Submit(w, r) handlers.Submit(w, r)
case "/ticket": case "/ticket":
handlers.NewTicket(w, r) handlers.NewTicket(db)
case "/submit-ticket": case "/submit-ticket":
handlers.SubmitTicket(w, r) handlers.SubmitTicket(db)
default: default:
http.NotFound(w, r) http.NotFound(w, r)
} }
}) })
log.Println("🚀 Lotto Tracker running on http://localhost:8080") log.Println("🌐 Running on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View File

@@ -4,7 +4,7 @@ type ThunderballResult struct {
Id int Id int
DrawDate string DrawDate string
Machine string Machine string
Ballset string Ballset int
Ball1 int Ball1 int
Ball2 int Ball2 int
Ball3 int Ball3 int

View File

@@ -9,6 +9,6 @@ type MyTicket struct {
Ball3 int Ball3 int
Ball4 int Ball4 int
Ball5 int Ball5 int
Bonus1 *int // Optional (nil if not used) Bonus1 *int
Bonus2 *int // For games like EuroMillions Bonus2 *int
} }

View File

@@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"log" "log"
"strings" "strings"
"synlotto-website/helpers"
"synlotto-website/models" "synlotto-website/models"
) )
@@ -28,21 +29,64 @@ func InsertThunderballResult(db *sql.DB, res models.ThunderballResult) error {
return err return err
} }
func InsertMyTicket(db *sql.DB, ticket models.MyTicket) error { func InsertTicket(db *sql.DB, ticket models.MyTicket) error {
stmt := ` // Convert optional fields to interface{} using manual check
var bonus1Val interface{}
var bonus2Val interface{}
if ticket.Bonus1 != nil {
bonus1Val = helpers.Nullable(*ticket.Bonus1)
} else {
bonus1Val = nil
}
if ticket.Bonus2 != nil {
bonus2Val = helpers.Nullable(*ticket.Bonus2)
} else {
bonus2Val = nil
}
query := `
SELECT COUNT(*) FROM my_tickets
WHERE game_type = ? AND draw_date = ?
AND ball1 = ? AND ball2 = ? AND ball3 = ?
AND ball4 = ? AND ball5 = ? AND bonus1 IS ? AND bonus2 IS ?;`
var count int
err := db.QueryRow(query,
ticket.GameType,
ticket.DrawDate,
ticket.Ball1,
ticket.Ball2,
ticket.Ball3,
ticket.Ball4,
ticket.Ball5,
bonus1Val,
bonus2Val,
).Scan(&count)
isDuplicate := count > 0
insert := `
INSERT INTO my_tickets ( INSERT INTO my_tickets (
game_type, draw_date, game_type, draw_date,
ball1, ball2, ball3, ball4, ball5, ball1, ball2, ball3, ball4, ball5,
bonus1, bonus2 bonus1, bonus2, duplicate
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);` ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
_, err := db.Exec(stmt, _, err = db.Exec(insert,
ticket.GameType, ticket.DrawDate, ticket.GameType, ticket.DrawDate,
ticket.Ball1, ticket.Ball2, ticket.Ball3, ticket.Ball4, ticket.Ball5, ticket.Ball1, ticket.Ball2, ticket.Ball3,
ticket.Bonus1, ticket.Bonus2, ticket.Ball4, ticket.Ball5,
bonus1Val, bonus2Val,
isDuplicate,
) )
if err != nil { if err != nil {
log.Println("❌ InsertMyTicket error:", err) log.Println("❌ Failed to insert ticket:", err)
} else if isDuplicate {
log.Println("⚠️ Duplicate ticket detected and flagged.")
} }
return err return err
} }

BIN
synlotto.db Normal file

Binary file not shown.