update ticket handler
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
|||||||
|
|
||||||
func AddTicket(db *sql.DB) http.HandlerFunc {
|
func AddTicket(db *sql.DB) http.HandlerFunc {
|
||||||
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == http.MethodGet {
|
||||||
rows, err := db.Query(`
|
rows, err := db.Query(`
|
||||||
SELECT DISTINCT draw_date
|
SELECT DISTINCT draw_date
|
||||||
FROM results_thunderball
|
FROM results_thunderball
|
||||||
@@ -52,6 +53,99 @@ func AddTicket(db *sql.DB) http.HandlerFunc {
|
|||||||
log.Println("❌ Template render error:", err)
|
log.Println("❌ Template render error:", err)
|
||||||
http.Error(w, "Error rendering form", http.StatusInternalServerError)
|
http.Error(w, "Error rendering form", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.ParseMultipartForm(10 << 20)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Invalid form", http.StatusBadRequest)
|
||||||
|
log.Println("❌ Failed to parse form:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userID, ok := helpers.GetCurrentUserID(r)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
game := r.FormValue("game_type")
|
||||||
|
drawDate := r.FormValue("draw_date")
|
||||||
|
purchaseMethod := r.FormValue("purchase_method")
|
||||||
|
purchaseDate := r.FormValue("purchase_date")
|
||||||
|
purchaseTime := r.FormValue("purchase_time")
|
||||||
|
|
||||||
|
if purchaseTime != "" {
|
||||||
|
purchaseDate += "T" + purchaseTime
|
||||||
|
}
|
||||||
|
|
||||||
|
imagePath := ""
|
||||||
|
file, handler, err := r.FormFile("ticket_image")
|
||||||
|
if err == nil && handler != nil {
|
||||||
|
defer file.Close()
|
||||||
|
filename := fmt.Sprintf("uploads/ticket_%d_%s", time.Now().UnixNano(), handler.Filename)
|
||||||
|
out, err := os.Create(filename)
|
||||||
|
if err == nil {
|
||||||
|
defer out.Close()
|
||||||
|
io.Copy(out, file)
|
||||||
|
imagePath = filename
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ballCount := 6
|
||||||
|
bonusCount := 2
|
||||||
|
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
for j := 0; j < ballCount; j++ {
|
||||||
|
if j < len(balls) && i < len(balls[j]) {
|
||||||
|
b[j] = balls[j][i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := 0; j < bonusCount; j++ {
|
||||||
|
if j < len(bonuses) && i < len(bonuses[j]) {
|
||||||
|
bo[j] = bonuses[j][i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := db.Exec(`
|
||||||
|
INSERT INTO my_tickets (
|
||||||
|
user_id, game_type, draw_date,
|
||||||
|
ball1, ball2, ball3, ball4, ball5, ball6,
|
||||||
|
bonus1, bonus2,
|
||||||
|
purchase_method, purchase_date, image_path
|
||||||
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`,
|
||||||
|
userID, game, drawDate,
|
||||||
|
b[0], b[1], b[2], b[3], b[4], b[5],
|
||||||
|
bo[0], bo[1],
|
||||||
|
purchaseMethod, purchaseDate, imagePath,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("❌ Failed to insert ticket line:", err)
|
||||||
|
} else {
|
||||||
|
log.Printf("✅ Ticket line %d saved", i+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/tickets", http.StatusSeeOther)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user