Lots of changes around viewing tickets from css perspective logic changes nwe handlers and service triggers... just lots of stuff...

This commit is contained in:
2025-03-28 10:05:54 +00:00
parent e13b375af7
commit 23e0208317
22 changed files with 410 additions and 148 deletions

View File

@@ -0,0 +1,39 @@
package handlers
import (
"database/sql"
"html/template"
"net/http"
"strconv"
"synlotto-website/helpers"
"synlotto-website/services"
)
func AdminTriggersHandler(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
context := helpers.TemplateContext(w, r)
if r.Method == http.MethodPost {
stats, err := services.RunTicketMatching(db, "manual")
if err != nil {
http.Error(w, "Matching failed: "+err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/triggers?flash=Matched "+
strconv.Itoa(stats.TicketsMatched)+" tickets, "+
strconv.Itoa(stats.WinnersFound)+" winners.", http.StatusSeeOther)
return
}
// Render admin trigger page
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/triggers.html",
))
err := tmpl.ExecuteTemplate(w, "layout", context)
if err != nil {
http.Error(w, "Failed to load page", http.StatusInternalServerError)
}
}
}

View File

@@ -5,4 +5,4 @@ import (
)
var Draws []models.ThunderballResult
var MyTickets []models.MyTicket
var MyTickets []models.Ticket

View File

@@ -14,8 +14,6 @@ import (
func Home(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("✅ Home hit")
rows, err := db.Query(`
SELECT id, draw_date, machine, ballset, ball1, ball2, ball3, ball4, ball5, thunderball
FROM results_thunderball

View File

@@ -170,7 +170,7 @@ func AddTicket(db *sql.DB) http.HandlerFunc {
if err != nil {
log.Println("❌ Failed to insert ticket line:", err)
} else {
log.Printf("✅ Ticket line %d saved", i+1)
log.Printf("✅ Ticket line %d saved", i+1) // ToDo create audit
}
}
@@ -270,23 +270,20 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
userID, ok := helpers.GetCurrentUserID(r)
if !ok {
log.Println("🚫 Not logged in, redirecting")
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
log.Printf("👤 Fetching tickets for user ID: %d\n", userID)
rows, err := db.Query(`
SELECT id, game_type, draw_date,
ball1, ball2, ball3, ball4, ball5, ball6,
bonus1, bonus2,
purchase_method, purchase_date, image_path
ball1, ball2, ball3, ball4, ball5, ball6,
bonus1, bonus2,
purchase_method, purchase_date, image_path, duplicate,
matched_main, matched_bonus, prize_tier, is_winner
FROM my_tickets
WHERE userId = ?
WHERE userid = ?
ORDER BY draw_date DESC, created_at DESC
`, userID)
if err != nil {
log.Println("❌ Query failed:", err)
http.Error(w, "Could not load tickets", http.StatusInternalServerError)
@@ -294,25 +291,51 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
}
defer rows.Close()
var tickets []models.MyTicket
var tickets []models.Ticket
for rows.Next() {
var t models.MyTicket
var t models.Ticket
var b1, b2, b3, b4, b5, b6, bo1, bo2 sql.NullInt64
var matchedMain, matchedBonus sql.NullInt64
var prizeTier sql.NullString
var isWinner sql.NullBool
err := rows.Scan(
&t.Id, &t.GameType, &t.DrawDate,
&t.Ball1, &t.Ball2, &t.Ball3, &t.Ball4, &t.Ball5, &t.Ball6,
&t.Bonus1, &t.Bonus2,
&t.PurchaseMethod, &t.PurchaseDate, &t.ImagePath,
&b1, &b2, &b3, &b4, &b5, &b6,
&bo1, &bo2,
&t.PurchaseMethod, &t.PurchaseDate, &t.ImagePath, &t.Duplicate,
&matchedMain, &matchedBonus, &prizeTier, &isWinner,
)
if err != nil {
log.Println("⚠️ Failed to scan ticket row:", err)
continue
}
t.Ball1, t.Ball2, t.Ball3 = int(b1.Int64), int(b2.Int64), int(b3.Int64)
t.Ball4, t.Ball5, t.Ball6 = int(b4.Int64), int(b5.Int64), int(b6.Int64)
t.Bonus1 = helpers.IntPtrIfValid(bo1)
t.Bonus2 = helpers.IntPtrIfValid(bo2)
if matchedMain.Valid {
t.MatchedMain = int(matchedMain.Int64)
}
if matchedBonus.Valid {
t.MatchedBonus = int(matchedBonus.Int64)
}
if prizeTier.Valid {
t.PrizeTier = prizeTier.String
}
if isWinner.Valid {
t.IsWinner = isWinner.Bool
}
t.Balls = helpers.BuildBallsSlice(t)
t.BonusBalls = helpers.BuildBonusSlice(t)
tickets = append(tickets, t)
}
log.Printf("📄 Loaded %d tickets\n", len(tickets))
context := helpers.TemplateContext(w, r)
context["Tickets"] = tickets

View File

@@ -4,7 +4,7 @@ import (
"synlotto-website/models"
)
func MatchTicketToDraw(ticket models.Ticket, draw models.DrawResult, rules []models.PrizeRule) models.MatchResult {
func MatchTicketToDraw(ticket models.MatchTicket, draw models.DrawResult, rules []models.PrizeRule) models.MatchResult {
mainMatches := countMatches(ticket.Balls, draw.Balls)
bonusMatches := countMatches(ticket.BonusBalls, draw.BonusBalls)