Refactor and remove sqlite and replace with MySQL
This commit is contained in:
130
internal/handlers/admin/manualtriggers.go
Normal file
130
internal/handlers/admin/manualtriggers.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
templateHelpers "synlotto-website/helpers/template"
|
||||
services "synlotto-website/services/tickets"
|
||||
|
||||
"synlotto-website/models"
|
||||
)
|
||||
|
||||
func AdminTriggersHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
data := models.TemplateData{}
|
||||
context := templateHelpers.TemplateContext(w, r, data)
|
||||
|
||||
if flash := r.URL.Query().Get("flash"); flash != "" {
|
||||
context["Flash"] = flash
|
||||
}
|
||||
|
||||
if r.Method == http.MethodPost {
|
||||
action := r.FormValue("action")
|
||||
flashMsg := ""
|
||||
|
||||
switch action {
|
||||
case "match":
|
||||
stats, err := services.RunTicketMatching(db, "manual")
|
||||
if err != nil {
|
||||
http.Error(w, "Matching failed: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
flashMsg = fmt.Sprintf("✅ Matched %d tickets, %d winners.", stats.TicketsMatched, stats.WinnersFound)
|
||||
|
||||
case "prizes":
|
||||
err := services.UpdateMissingPrizes(db)
|
||||
if err != nil {
|
||||
http.Error(w, "Prize update failed: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
flashMsg = "✅ Missing prizes updated."
|
||||
|
||||
case "refresh_prizes":
|
||||
err := services.RefreshTicketPrizes(db)
|
||||
if err != nil {
|
||||
http.Error(w, "Refresh failed: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
flashMsg = "✅ Ticket prizes refreshed."
|
||||
|
||||
case "run_all":
|
||||
stats, err := services.RunTicketMatching(db, "manual")
|
||||
if err != nil {
|
||||
http.Error(w, "Matching failed: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = services.UpdateMissingPrizes(db)
|
||||
if err != nil {
|
||||
http.Error(w, "Prize update failed: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
flashMsg = fmt.Sprintf("✅ Matched %d tickets, %d winners. Prizes updated.", stats.TicketsMatched, stats.WinnersFound)
|
||||
|
||||
default:
|
||||
flashMsg = "⚠️ Unknown action."
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/admin/triggers?flash="+url.QueryEscape(flashMsg), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl := templateHelpers.LoadTemplateFiles("triggers.html", "templates/admin/triggers.html")
|
||||
|
||||
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||
if err != nil {
|
||||
log.Println("Template error:", err)
|
||||
http.Error(w, "Failed to load page", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func MatchTicketsHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateMissingPrizesHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
err := services.UpdateMissingPrizes(db)
|
||||
if err != nil {
|
||||
http.Error(w, "Prize update failed: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/admin/triggers?flash=Updated missing prize data.", http.StatusSeeOther)
|
||||
}
|
||||
}
|
||||
|
||||
func RunAllHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
stats, err := services.RunTicketMatching(db, "manual")
|
||||
if err != nil {
|
||||
http.Error(w, "Matching failed: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = services.UpdateMissingPrizes(db)
|
||||
if err != nil {
|
||||
http.Error(w, "Prize update failed: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/admin/triggers?flash=Matched "+
|
||||
strconv.Itoa(stats.TicketsMatched)+" tickets, "+
|
||||
strconv.Itoa(stats.WinnersFound)+" winners. Prizes updated.", http.StatusSeeOther)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user