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
This commit is contained in:
@@ -24,7 +24,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
|
|||||||
"templates/account/login.html",
|
"templates/account/login.html",
|
||||||
))
|
))
|
||||||
|
|
||||||
context := helpers.TemplateContext(w, r)
|
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
||||||
context["csrfField"] = csrf.TemplateField(r)
|
context["csrfField"] = csrf.TemplateField(r)
|
||||||
|
|
||||||
err := tmpl.ExecuteTemplate(w, "layout", context)
|
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ type AdminLogEntry struct {
|
|||||||
|
|
||||||
func AdminAccessLogHandler(db *sql.DB) http.HandlerFunc {
|
func AdminAccessLogHandler(db *sql.DB) http.HandlerFunc {
|
||||||
return middleware.Auth(true)(func(w http.ResponseWriter, r *http.Request) {
|
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(`
|
rows, err := db.Query(`
|
||||||
SELECT accessed_at, user_id, path, ip, user_agent
|
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 {
|
func AuditLogHandler(db *sql.DB) http.HandlerFunc {
|
||||||
return middleware.Auth(true)(func(w http.ResponseWriter, r *http.Request) {
|
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(`
|
rows, err := db.Query(`
|
||||||
SELECT timestamp, user_id, action, ip, user_agent
|
SELECT timestamp, user_id, action, ip, user_agent
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func AdminDashboardHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
|
|
||||||
// TODO: check is_admin from users table here
|
// TODO: check is_admin from users table here
|
||||||
|
|
||||||
context := helpers.TemplateContext(w, r)
|
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
||||||
|
|
||||||
// Total ticket stats
|
// Total ticket stats
|
||||||
var total, winners int
|
var total, winners int
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
func NewDrawHandler(db *sql.DB) http.HandlerFunc {
|
func NewDrawHandler(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) {
|
||||||
context := helpers.TemplateContext(w, r)
|
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
||||||
|
|
||||||
if r.Method == http.MethodPost {
|
if r.Method == http.MethodPost {
|
||||||
game := r.FormValue("game_type")
|
game := r.FormValue("game_type")
|
||||||
@@ -75,7 +75,7 @@ func DeleteDrawHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
|
|
||||||
func ListDrawsHandler(db *sql.DB) http.HandlerFunc {
|
func ListDrawsHandler(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) {
|
||||||
context := helpers.TemplateContext(w, r)
|
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
||||||
draws := []models.DrawSummary{}
|
draws := []models.DrawSummary{}
|
||||||
|
|
||||||
rows, err := db.Query(`
|
rows, err := db.Query(`
|
||||||
|
|||||||
@@ -10,12 +10,13 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"synlotto-website/helpers"
|
"synlotto-website/helpers"
|
||||||
|
"synlotto-website/models"
|
||||||
services "synlotto-website/services/tickets"
|
services "synlotto-website/services/tickets"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AdminTriggersHandler(db *sql.DB) http.HandlerFunc {
|
func AdminTriggersHandler(db *sql.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
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
|
// Inject flash message if available
|
||||||
if flash := r.URL.Query().Get("flash"); flash != "" {
|
if flash := r.URL.Query().Get("flash"); flash != "" {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"synlotto-website/helpers"
|
"synlotto-website/helpers"
|
||||||
|
"synlotto-website/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddPrizesHandler(db *sql.DB) http.HandlerFunc {
|
func AddPrizesHandler(db *sql.DB) http.HandlerFunc {
|
||||||
@@ -16,7 +17,7 @@ func AddPrizesHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
"templates/layout.html",
|
"templates/layout.html",
|
||||||
"templates/admin/draws/prizes/add_prizes.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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +51,7 @@ func ModifyPrizesHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
"templates/layout.html",
|
"templates/layout.html",
|
||||||
"templates/admin/draws/prizes/modify_prizes.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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ func NewDraw(db *sql.DB) http.HandlerFunc {
|
|||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("➡️ New draw form opened")
|
log.Println("➡️ New draw form opened")
|
||||||
|
|
||||||
context := BuildTemplateContext(db, w, r)
|
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
||||||
context["Page"] = "new_draw"
|
context["Page"] = "new_draw"
|
||||||
context["Data"] = nil
|
context["Data"] = nil
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,8 @@ func Home(db *sql.DB) http.HandlerFunc {
|
|||||||
results = append(results, res)
|
results = append(results, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
context := BuildTemplateContext(db, w, r)
|
data := BuildTemplateData(db, w, r)
|
||||||
|
context := helpers.TemplateContext(w, r, data)
|
||||||
context["Data"] = results
|
context["Data"] = results
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
||||||
|
|||||||
@@ -1,44 +1,29 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"net/http"
|
"net/http"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"synlotto-website/helpers"
|
"synlotto-website/helpers"
|
||||||
"synlotto-website/models"
|
|
||||||
"synlotto-website/storage"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NotificationsHandler(w http.ResponseWriter, r *http.Request) {
|
func NotificationsHandler(db *sql.DB) http.HandlerFunc {
|
||||||
session, _ := helpers.GetSession(w, r)
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
data := BuildTemplateData(db, w, r)
|
||||||
var user *models.User
|
context := helpers.TemplateContext(w, r, data)
|
||||||
switch v := session.Values["user_id"].(type) {
|
|
||||||
case int:
|
|
||||||
user = models.GetUserByID(v)
|
|
||||||
case int64:
|
|
||||||
user = models.GetUserByID(int(v))
|
|
||||||
}
|
|
||||||
|
|
||||||
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").
|
tmpl := template.Must(template.New("notifications.html").
|
||||||
Funcs(helpers.TemplateFuncs()).
|
Funcs(helpers.TemplateFuncs()).
|
||||||
ParseFiles("templates/notifications.html"))
|
ParseFiles(
|
||||||
|
"templates/layout.html",
|
||||||
|
"templates/topbar.html",
|
||||||
|
"templates/notifications/index.html",
|
||||||
|
))
|
||||||
|
|
||||||
context := helpers.TemplateContext(w, r, user, isAdmin, notificationCount, notifications)
|
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
|
if err != nil {
|
||||||
if err := tmpl.Execute(w, context); err != nil {
|
http.Error(w, "Error rendering notifications page", http.StatusInternalServerError)
|
||||||
http.Error(w, "Template rendering error", http.StatusInternalServerError)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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["csrfField"] = csrf.TemplateField(r)
|
||||||
context["DrawDates"] = drawDates
|
context["DrawDates"] = drawDates
|
||||||
|
|
||||||
@@ -359,7 +359,7 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
|
|||||||
tickets = append(tickets, t)
|
tickets = append(tickets, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
context := helpers.TemplateContext(w, r)
|
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
||||||
context["Tickets"] = tickets
|
context["Tickets"] = tickets
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ package helpers
|
|||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"synlotto-website/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Render403(w http.ResponseWriter, r *http.Request) {
|
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(
|
tmpl := template.Must(template.New("").Funcs(TemplateFuncs()).ParseFiles(
|
||||||
"templates/layout.html",
|
"templates/layout.html",
|
||||||
"templates/error/403.html",
|
"templates/error/403.html",
|
||||||
|
|||||||
Reference in New Issue
Block a user