diff --git a/handlers/ticket_handler.go b/handlers/ticket_handler.go index 663085d..fbb2794 100644 --- a/handlers/ticket_handler.go +++ b/handlers/ticket_handler.go @@ -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) + }) } diff --git a/main.go b/main.go index 91b7771..cd81b17 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,8 @@ func main() { mux.HandleFunc("/login", middleware.Auth(false)(handlers.Login)) mux.HandleFunc("/logout", handlers.Logout) mux.HandleFunc("/signup", middleware.Auth(false)(handlers.Signup)) - mux.HandleFunc("/account/tickets/add_ticket", middleware.Auth(true)(handlers.AddTicket(db))) + mux.HandleFunc("/account/tickets/add_ticket", handlers.AddTicket(db)) + mux.HandleFunc("/account/tickets/my_tickets", handlers.GetMyTickets(db)) log.Println("๐ŸŒ Running on http://localhost:8080") http.ListenAndServe(":8080", helpers.RateLimit(csrfMiddleware(mux))) diff --git a/models/user.go b/models/user.go index b298d90..6f64b70 100644 --- a/models/user.go +++ b/models/user.go @@ -55,7 +55,7 @@ func GetUserByID(id int) *User { } func LogLoginAttempt(username string, success bool) { - _, err := db.Exec("INSERT INTO login_audit (username, success, timestamp) VALUES (?, ?, ?)", + _, err := db.Exec("INSERT INTO auditlog (username, success, timestamp) VALUES (?, ?, ?)", username, boolToInt(success), time.Now().Format(time.RFC3339)) if err != nil { log.Println("โŒ Failed to log login:", err) diff --git a/storage/db.go b/storage/db.go index 375dc40..7c2b2b0 100644 --- a/storage/db.go +++ b/storage/db.go @@ -42,11 +42,13 @@ func InitDB(filepath string) *sql.DB { ball3 INTEGER, ball4 INTEGER, ball5 INTEGER, + ball6 INTEGER, bonus1 INTEGER, bonus2 INTEGER, duplicate BOOLEAN DEFAULT 0, - purchased_date TEXT, - purchased_location TEXT, + purchase_date TEXT, + purchase_method TEXT, + image_path TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (userId) REFERENCES users(id) );` diff --git a/templates/account/tickets/add_ticket.html b/templates/account/tickets/add_ticket.html new file mode 100644 index 0000000..4a2e3c5 --- /dev/null +++ b/templates/account/tickets/add_ticket.html @@ -0,0 +1,155 @@ +{{ define "content" }} +โ† Back +

Log My Ticket

+ +
+ {{ .csrfField }} + +
+ +
+ +
+ +
+ +
+ +
+ +
+ + + + + +
+ +
+ +
+ +
+ + +
+ +

+ +
+ + +{{ end }}