thing this are broken but checking in so i have a state to revert too.
This commit is contained in:
@@ -5,90 +5,37 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
|
||||||
|
"synlotto-website/handlers"
|
||||||
"synlotto-website/helpers"
|
"synlotto-website/helpers"
|
||||||
"synlotto-website/models"
|
"synlotto-website/models"
|
||||||
|
|
||||||
"github.com/gorilla/csrf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ToDo this should not be in draw for home!
|
func NewDraw(db *sql.DB) http.HandlerFunc {
|
||||||
func Home(db *sql.DB) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
rows, err := db.Query(`
|
log.Println("➡️ New draw form opened")
|
||||||
SELECT id, draw_date, machine, ballset, ball1, ball2, ball3, ball4, ball5, thunderball
|
|
||||||
FROM results_thunderball
|
|
||||||
ORDER BY id DESC
|
|
||||||
`)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
|
||||||
log.Println("❌ DB error:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
var results []models.ThunderballResult
|
context := BuildTemplateContext(db, w, r)
|
||||||
|
context["Page"] = "new_draw"
|
||||||
|
context["Data"] = nil
|
||||||
|
|
||||||
for rows.Next() {
|
tmpl := template.Must(template.New("").Funcs(handlers.TemplateFuncs()).ParseFiles(
|
||||||
var res models.ThunderballResult
|
|
||||||
err := rows.Scan(
|
|
||||||
&res.Id, &res.DrawDate, &res.Machine, &res.BallSet,
|
|
||||||
&res.Ball1, &res.Ball2, &res.Ball3, &res.Ball4, &res.Ball5, &res.Thunderball,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("❌ Row scan error:", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
res.SortedBalls = []int{
|
|
||||||
res.Ball1, res.Ball2, res.Ball3, res.Ball4, res.Ball5,
|
|
||||||
}
|
|
||||||
sort.Ints(res.SortedBalls)
|
|
||||||
|
|
||||||
results = append(results, res)
|
|
||||||
}
|
|
||||||
|
|
||||||
context := helpers.TemplateContext(w, r)
|
|
||||||
context["Data"] = results
|
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
|
||||||
"templates/layout.html",
|
"templates/layout.html",
|
||||||
"templates/topbar.html",
|
"templates/topbar.html",
|
||||||
"templates/index.html",
|
"templates/new_draw.html",
|
||||||
))
|
))
|
||||||
|
|
||||||
err = tmpl.ExecuteTemplate(w, "layout", context)
|
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("❌ Template error:", err)
|
log.Println("❌ Template error:", err)
|
||||||
http.Error(w, "Error rendering homepage", http.StatusInternalServerError)
|
http.Error(w, "Error rendering form", http.StatusInternalServerError)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDraw(w http.ResponseWriter, r *http.Request) {
|
|
||||||
log.Println("➡️ New draw form opened")
|
|
||||||
|
|
||||||
tmpl := template.Must(template.ParseFiles(
|
|
||||||
"templates/layout.html",
|
|
||||||
"templates/topbar.html",
|
|
||||||
"templates/new_draw.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err := tmpl.ExecuteTemplate(w, "layout", map[string]interface{}{
|
|
||||||
"csrfField": csrf.TemplateField(r),
|
|
||||||
"Page": "new_draw",
|
|
||||||
"Data": nil,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Println("❌ Template error:", err)
|
|
||||||
http.Error(w, "Error rendering form", http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Submit(w http.ResponseWriter, r *http.Request) {
|
func Submit(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("📝 Form submission received")
|
log.Println("📝 Form submission received")
|
||||||
r.ParseForm()
|
_ = r.ParseForm()
|
||||||
|
|
||||||
draw := models.ThunderballResult{
|
draw := models.ThunderballResult{
|
||||||
DrawDate: r.FormValue("date"),
|
DrawDate: r.FormValue("date"),
|
||||||
@@ -102,6 +49,7 @@ func Submit(w http.ResponseWriter, r *http.Request) {
|
|||||||
Thunderball: helpers.Atoi(r.FormValue("thunderball")),
|
Thunderball: helpers.Atoi(r.FormValue("thunderball")),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For now you're appending to memory - can replace with DB insert later
|
||||||
Draws = append(Draws, draw)
|
Draws = append(Draws, draw)
|
||||||
|
|
||||||
log.Printf("📅 %s | 🛠 %s | 🎱 %d | 🔢 %d,%d,%d,%d,%d | ⚡ %d\n",
|
log.Printf("📅 %s | 🛠 %s | 🎱 %d | 🔢 %d,%d,%d,%d,%d | ⚡ %d\n",
|
||||||
|
|||||||
79
handlers/template.go
Normal file
79
handlers/template.go
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"synlotto-website/helpers"
|
||||||
|
"synlotto-website/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func templateFuncs() template.FuncMap {
|
||||||
|
return template.FuncMap{
|
||||||
|
"plus1": func(i int) int { return i + 1 },
|
||||||
|
"minus1": func(i int) int {
|
||||||
|
if i > 1 {
|
||||||
|
return i - 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
},
|
||||||
|
"mul": func(a, b int) int { return a * b },
|
||||||
|
"add": func(a, b int) int { return a + b },
|
||||||
|
"min": func(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDraw(db *sql.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Println("➡️ New draw form opened")
|
||||||
|
|
||||||
|
context := BuildTemplateContext(db, w, r)
|
||||||
|
context["Page"] = "new_draw"
|
||||||
|
context["Data"] = nil
|
||||||
|
|
||||||
|
tmpl := template.Must(template.New("").Funcs(templateFuncs()).ParseFiles(
|
||||||
|
"templates/layout.html",
|
||||||
|
"templates/topbar.html",
|
||||||
|
"templates/new_draw.html",
|
||||||
|
))
|
||||||
|
|
||||||
|
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("❌ Template error:", err)
|
||||||
|
http.Error(w, "Error rendering form", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Submit(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Println("📝 Form submission received")
|
||||||
|
_ = r.ParseForm()
|
||||||
|
|
||||||
|
draw := models.ThunderballResult{
|
||||||
|
DrawDate: r.FormValue("date"),
|
||||||
|
Machine: r.FormValue("machine"),
|
||||||
|
BallSet: helpers.Atoi(r.FormValue("ballSet")),
|
||||||
|
Ball1: helpers.Atoi(r.FormValue("ball1")),
|
||||||
|
Ball2: helpers.Atoi(r.FormValue("ball2")),
|
||||||
|
Ball3: helpers.Atoi(r.FormValue("ball3")),
|
||||||
|
Ball4: helpers.Atoi(r.FormValue("ball4")),
|
||||||
|
Ball5: helpers.Atoi(r.FormValue("ball5")),
|
||||||
|
Thunderball: helpers.Atoi(r.FormValue("thunderball")),
|
||||||
|
}
|
||||||
|
|
||||||
|
// For now you're appending to memory - can replace with DB insert later
|
||||||
|
Draws = append(Draws, draw)
|
||||||
|
|
||||||
|
log.Printf("📅 %s | 🖠 %s | 🎱 %d | 🔢 %d,%d,%d,%d,%d | ⚡ %d\n",
|
||||||
|
draw.DrawDate, draw.Machine, draw.BallSet,
|
||||||
|
draw.Ball1, draw.Ball2, draw.Ball3, draw.Ball4, draw.Ball5, draw.Thunderball)
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user