From 5ea780fcab7bd163b2b5f64a9cbe12bcc3f870cd Mon Sep 17 00:00:00 2001 From: H3ALY Date: Tue, 1 Apr 2025 21:20:05 +0100 Subject: [PATCH] Cleanup: Finalize template context integration and remove legacy code - Replaced legacy TemplateContext calls with structured TemplateData usage - Removed unused variables and redundant storage calls in notifications handler - Ensured consistent use of BuildTemplateData across user-facing handlers - Resolved all compile-time errors from refactor - Ready for runtime testing and further layout integration --- handlers/account.go | 2 +- handlers/admin/audit.go | 4 +-- handlers/admin/dashboard.go | 2 +- handlers/admin/draws.go | 4 +-- handlers/admin/manualtriggers.go | 3 +- handlers/admin/prizes.go | 5 ++-- handlers/draw_handler.go | 2 +- handlers/home.go | 3 +- handlers/notifications.go | 47 +++++++++++--------------------- handlers/ticket_handler.go | 4 +-- helpers/pages.go | 4 ++- 11 files changed, 35 insertions(+), 45 deletions(-) diff --git a/handlers/account.go b/handlers/account.go index eb9a49f..c8793a8 100644 --- a/handlers/account.go +++ b/handlers/account.go @@ -24,7 +24,7 @@ func Login(w http.ResponseWriter, r *http.Request) { "templates/account/login.html", )) - context := helpers.TemplateContext(w, r) + context := helpers.TemplateContext(w, r, models.TemplateData{}) context["csrfField"] = csrf.TemplateField(r) err := tmpl.ExecuteTemplate(w, "layout", context) diff --git a/handlers/admin/audit.go b/handlers/admin/audit.go index bf0dabe..3c84ffc 100644 --- a/handlers/admin/audit.go +++ b/handlers/admin/audit.go @@ -20,7 +20,7 @@ type AdminLogEntry struct { func AdminAccessLogHandler(db *sql.DB) http.HandlerFunc { return middleware.Auth(true)(func(w http.ResponseWriter, r *http.Request) { - context := helpers.TemplateContext(w, r) + context := helpers.TemplateContext(w, r, models.TemplateData{}) rows, err := db.Query(` SELECT accessed_at, user_id, path, ip, user_agent @@ -56,7 +56,7 @@ func AdminAccessLogHandler(db *sql.DB) http.HandlerFunc { func AuditLogHandler(db *sql.DB) http.HandlerFunc { return middleware.Auth(true)(func(w http.ResponseWriter, r *http.Request) { - context := helpers.TemplateContext(w, r) + context := helpers.TemplateContext(w, r, models.TemplateData{}) rows, err := db.Query(` SELECT timestamp, user_id, action, ip, user_agent diff --git a/handlers/admin/dashboard.go b/handlers/admin/dashboard.go index c3d7549..1333a2b 100644 --- a/handlers/admin/dashboard.go +++ b/handlers/admin/dashboard.go @@ -20,7 +20,7 @@ func AdminDashboardHandler(db *sql.DB) http.HandlerFunc { // TODO: check is_admin from users table here - context := helpers.TemplateContext(w, r) + context := helpers.TemplateContext(w, r, models.TemplateData{}) // Total ticket stats var total, winners int diff --git a/handlers/admin/draws.go b/handlers/admin/draws.go index 341875e..49eb8bb 100644 --- a/handlers/admin/draws.go +++ b/handlers/admin/draws.go @@ -12,7 +12,7 @@ import ( func NewDrawHandler(db *sql.DB) http.HandlerFunc { return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) { - context := helpers.TemplateContext(w, r) + context := helpers.TemplateContext(w, r, models.TemplateData{}) if r.Method == http.MethodPost { game := r.FormValue("game_type") @@ -75,7 +75,7 @@ func DeleteDrawHandler(db *sql.DB) http.HandlerFunc { func ListDrawsHandler(db *sql.DB) http.HandlerFunc { return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) { - context := helpers.TemplateContext(w, r) + context := helpers.TemplateContext(w, r, models.TemplateData{}) draws := []models.DrawSummary{} rows, err := db.Query(` diff --git a/handlers/admin/manualtriggers.go b/handlers/admin/manualtriggers.go index 8345bcf..52209a7 100644 --- a/handlers/admin/manualtriggers.go +++ b/handlers/admin/manualtriggers.go @@ -10,12 +10,13 @@ import ( "strconv" "synlotto-website/helpers" + "synlotto-website/models" services "synlotto-website/services/tickets" ) func AdminTriggersHandler(db *sql.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - context := helpers.TemplateContext(w, r) + context := helpers.TemplateContext(w, r, models.TemplateData{}) // Inject flash message if available if flash := r.URL.Query().Get("flash"); flash != "" { diff --git a/handlers/admin/prizes.go b/handlers/admin/prizes.go index 821807e..31163c5 100644 --- a/handlers/admin/prizes.go +++ b/handlers/admin/prizes.go @@ -7,6 +7,7 @@ import ( "net/http" "strconv" "synlotto-website/helpers" + "synlotto-website/models" ) func AddPrizesHandler(db *sql.DB) http.HandlerFunc { @@ -16,7 +17,7 @@ func AddPrizesHandler(db *sql.DB) http.HandlerFunc { "templates/layout.html", "templates/admin/draws/prizes/add_prizes.html", )) - tmpl.ExecuteTemplate(w, "layout", helpers.TemplateContext(w, r)) + tmpl.ExecuteTemplate(w, "layout", helpers.TemplateContext(w, r, models.TemplateData{})) return } @@ -50,7 +51,7 @@ func ModifyPrizesHandler(db *sql.DB) http.HandlerFunc { "templates/layout.html", "templates/admin/draws/prizes/modify_prizes.html", )) - tmpl.ExecuteTemplate(w, "layout", helpers.TemplateContext(w, r)) + tmpl.ExecuteTemplate(w, "layout", helpers.TemplateContext(w, r, models.TemplateData{})) return } diff --git a/handlers/draw_handler.go b/handlers/draw_handler.go index 1b9d1e0..47a837f 100644 --- a/handlers/draw_handler.go +++ b/handlers/draw_handler.go @@ -14,7 +14,7 @@ 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 := helpers.TemplateContext(w, r, models.TemplateData{}) context["Page"] = "new_draw" context["Data"] = nil diff --git a/handlers/home.go b/handlers/home.go index 44105f1..9502744 100644 --- a/handlers/home.go +++ b/handlers/home.go @@ -46,7 +46,8 @@ func Home(db *sql.DB) http.HandlerFunc { results = append(results, res) } - context := BuildTemplateContext(db, w, r) + data := BuildTemplateData(db, w, r) + context := helpers.TemplateContext(w, r, data) context["Data"] = results tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles( diff --git a/handlers/notifications.go b/handlers/notifications.go index d8cfcbb..e9086d3 100644 --- a/handlers/notifications.go +++ b/handlers/notifications.go @@ -1,44 +1,29 @@ package handlers import ( + "database/sql" "net/http" "text/template" "synlotto-website/helpers" - "synlotto-website/models" - "synlotto-website/storage" ) -func NotificationsHandler(w http.ResponseWriter, r *http.Request) { - session, _ := helpers.GetSession(w, r) +func NotificationsHandler(db *sql.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + data := BuildTemplateData(db, w, r) + context := helpers.TemplateContext(w, r, data) - var user *models.User - switch v := session.Values["user_id"].(type) { - case int: - user = models.GetUserByID(v) - case int64: - user = models.GetUserByID(int(v)) - } + tmpl := template.Must(template.New("notifications.html"). + Funcs(helpers.TemplateFuncs()). + ParseFiles( + "templates/layout.html", + "templates/topbar.html", + "templates/notifications/index.html", + )) - var ( - isAdmin bool - notificationCount int - notifications []models.Notification - ) - - if user != nil { - isAdmin = user.IsAdmin - notificationCount = storage.GetNotificationCount(user.Id) - notifications = storage.GetRecentNotifications(user.Id, 15) - } - - tmpl := template.Must(template.New("notifications.html"). - Funcs(helpers.TemplateFuncs()). - ParseFiles("templates/notifications.html")) - - context := helpers.TemplateContext(w, r, user, isAdmin, notificationCount, notifications) - - if err := tmpl.Execute(w, context); err != nil { - http.Error(w, "Template rendering error", http.StatusInternalServerError) + err := tmpl.ExecuteTemplate(w, "layout", context) + if err != nil { + http.Error(w, "Error rendering notifications page", http.StatusInternalServerError) + } } } diff --git a/handlers/ticket_handler.go b/handlers/ticket_handler.go index acb0bab..f06b3ea 100644 --- a/handlers/ticket_handler.go +++ b/handlers/ticket_handler.go @@ -40,7 +40,7 @@ func AddTicket(db *sql.DB) http.HandlerFunc { } } - context := helpers.TemplateContext(w, r) + context := helpers.TemplateContext(w, r, models.TemplateData{}) context["csrfField"] = csrf.TemplateField(r) context["DrawDates"] = drawDates @@ -359,7 +359,7 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc { tickets = append(tickets, t) } - context := helpers.TemplateContext(w, r) + context := helpers.TemplateContext(w, r, models.TemplateData{}) context["Tickets"] = tickets tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles( diff --git a/helpers/pages.go b/helpers/pages.go index 33a6c4a..eb8ffc0 100644 --- a/helpers/pages.go +++ b/helpers/pages.go @@ -4,10 +4,12 @@ package helpers import ( "html/template" "net/http" + + "synlotto-website/models" ) func Render403(w http.ResponseWriter, r *http.Request) { - context := TemplateContext(w, r) + context := TemplateContext(w, r, models.TemplateData{}) tmpl := template.Must(template.New("").Funcs(TemplateFuncs()).ParseFiles( "templates/layout.html", "templates/error/403.html",