Refactor: Centralize template context using unified TemplateData struct

- Introduced models.TemplateData for shared user/context state
- Moved context construction logic into handlers/template_context.go
- Simplified helpers.TemplateContext to accept structured data
- Restored and organized template helper functions
- Updated affected handlers (main.go, draw_handler.go, notifications.go)
- Improved scalability and separation of concerns in template rendering
This commit is contained in:
2025-04-01 21:08:00 +01:00
parent 6dbac8ab14
commit 03b1e095ce
6 changed files with 108 additions and 85 deletions

View File

@@ -6,7 +6,6 @@ import (
"log" "log"
"net/http" "net/http"
"synlotto-website/handlers"
"synlotto-website/helpers" "synlotto-website/helpers"
"synlotto-website/models" "synlotto-website/models"
) )
@@ -19,7 +18,7 @@ func NewDraw(db *sql.DB) http.HandlerFunc {
context["Page"] = "new_draw" context["Page"] = "new_draw"
context["Data"] = nil context["Data"] = nil
tmpl := template.Must(template.New("").Funcs(handlers.TemplateFuncs()).ParseFiles( tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html", "templates/layout.html",
"templates/topbar.html", "templates/topbar.html",
"templates/new_draw.html", "templates/new_draw.html",

44
handlers/notifications.go Normal file
View File

@@ -0,0 +1,44 @@
package handlers
import (
"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)
var user *models.User
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").
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)
}
}

View File

@@ -6,55 +6,39 @@ import (
"synlotto-website/helpers" "synlotto-website/helpers"
"synlotto-website/models" "synlotto-website/models"
"synlotto-website/storage" "synlotto-website/storage"
"github.com/gorilla/csrf"
) )
type TemplateData map[string]interface{} func BuildTemplateData(db *sql.DB, w http.ResponseWriter, r *http.Request) models.TemplateData {
func BuildTemplateContext(db *sql.DB, w http.ResponseWriter, r *http.Request) TemplateData {
session, _ := helpers.GetSession(w, r) session, _ := helpers.GetSession(w, r)
var flash string var user *models.User
if f, ok := session.Values["flash"].(string); ok {
flash = f
delete(session.Values, "flash")
session.Save(r, w)
}
var currentUser *models.User
var isAdmin bool var isAdmin bool
var notificationCount int
notificationCount := 0 var notifications []models.Notification
notifications := []models.Notification{} var messageCount int
messageCount := 0 var messages []models.Message
messages := []models.Message{}
switch v := session.Values["user_id"].(type) { switch v := session.Values["user_id"].(type) {
case int: case int:
currentUser = models.GetUserByID(v) user = models.GetUserByID(v)
case int64: case int64:
currentUser = models.GetUserByID(int(v)) user = models.GetUserByID(int(v))
} }
if currentUser != nil { if user != nil {
isAdmin = currentUser.IsAdmin isAdmin = user.IsAdmin
notificationCount = storage.GetNotificationCount(db, user.Id)
notificationCount = storage.GetNotificationCount(db, currentUser.Id) notifications = storage.GetRecentNotifications(db, user.Id, 15)
notifications = storage.GetRecentNotifications(db, currentUser.Id, 15) messageCount, _ = storage.GetMessageCount(db, user.Id)
messages = storage.GetRecentMessages(db, user.Id, 15)
messageCount, _ = storage.GetMessageCount(db, currentUser.Id)
messages = storage.GetRecentMessages(db, currentUser.Id, 15)
} }
return TemplateData{ return models.TemplateData{
"CSRFField": csrf.TemplateField(r), User: user,
"Flash": flash, IsAdmin: isAdmin,
"User": currentUser, NotificationCount: notificationCount,
"IsAdmin": isAdmin, Notifications: notifications,
"NotificationCount": notificationCount, MessageCount: messageCount,
"Notifications": notifications, Messages: messages,
"MessageCount": messageCount,
"Messages": messages,
} }
} }

View File

@@ -4,12 +4,35 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"strings" "strings"
"synlotto-website/models" "synlotto-website/models"
"synlotto-website/storage"
"github.com/gorilla/csrf" "github.com/gorilla/csrf"
) )
func TemplateContext(w http.ResponseWriter, r *http.Request, data models.TemplateData) map[string]interface{} {
session, _ := GetSession(w, r)
var flash string
if f, ok := session.Values["flash"].(string); ok {
flash = f
delete(session.Values, "flash")
session.Save(r, w)
}
return map[string]interface{}{
"CSRFField": csrf.TemplateField(r),
"Flash": flash,
"User": data.User,
"IsAdmin": data.IsAdmin,
"NotificationCount": data.NotificationCount,
"Notifications": data.Notifications,
"MessageCount": data.MessageCount,
"Messages": data.Messages,
}
}
// TemplateFuncs provides helper functions to be used in templates.
func TemplateFuncs() template.FuncMap { func TemplateFuncs() template.FuncMap {
return template.FuncMap{ return template.FuncMap{
"plus1": func(i int) int { return i + 1 }, "plus1": func(i int) int { return i + 1 },
@@ -24,8 +47,9 @@ func TemplateFuncs() template.FuncMap {
"min": func(a, b int) int { "min": func(a, b int) int {
if a < b { if a < b {
return a return a
} } else {
return b return b
}
}, },
"intVal": func(p *int) int { "intVal": func(p *int) int {
if p == nil { if p == nil {
@@ -39,42 +63,11 @@ func TemplateFuncs() template.FuncMap {
} }
} }
func TemplateContext(w http.ResponseWriter, r *http.Request) map[string]interface{} { // SetFlash sets a flash message in session.
func SetFlash(w http.ResponseWriter, r *http.Request, message string) {
session, _ := GetSession(w, r) session, _ := GetSession(w, r)
session.Values["flash"] = message
var flash string
if f, ok := session.Values["flash"].(string); ok {
flash = f
delete(session.Values, "flash")
session.Save(r, w) session.Save(r, w)
}
var currentUser *models.User
var isAdmin bool
var notificationCount int
var notifications []models.Notification
switch v := session.Values["user_id"].(type) {
case int:
currentUser = models.GetUserByID(v)
case int64:
currentUser = models.GetUserByID(int(v))
}
if currentUser != nil {
isAdmin = currentUser.IsAdmin
notificationCount = storage.GetNotificationCount(currentUser.Id)
notifications = storage.GetRecentNotifications(currentUser.Id, 15)
}
return map[string]interface{}{
"CSRFField": csrf.TemplateField(r),
"Flash": flash,
"User": currentUser,
"IsAdmin": isAdmin,
"NotificationCount": notificationCount,
"Notifications": notifications,
}
} }
func InSlice(n int, list []int) bool { func InSlice(n int, list []int) bool {
@@ -83,7 +76,6 @@ func InSlice(n int, list []int) bool {
return true return true
} }
} }
return false return false
} }
@@ -107,9 +99,3 @@ func rangeClass(n int) string {
return "50-plus" return "50-plus"
} }
} }
func SetFlash(w http.ResponseWriter, r *http.Request, message string) {
session, _ := GetSession(w, r)
session.Values["flash"] = message
session.Save(r, w)
}

View File

@@ -52,8 +52,8 @@ func setupAdminRoutes(mux *http.ServeMux, db *sql.DB) {
// Draw management // Draw management
mux.HandleFunc("/admin/draws", middleware.AdminOnly(db, admin.ListDrawsHandler(db))) mux.HandleFunc("/admin/draws", middleware.AdminOnly(db, admin.ListDrawsHandler(db)))
mux.HandleFunc("/admin/draws/new", middleware.AdminOnly(db, admin.RenderNewDrawForm(db))) // mux.HandleFunc("/admin/draws/new", middleware.AdminOnly(db, admin.RenderNewDrawForm(db)))
mux.HandleFunc("/admin/draws/submit", middleware.AdminOnly(db, admin.CreateDrawHandler(db))) // mux.HandleFunc("/admin/draws/submit", middleware.AdminOnly(db, admin.CreateDrawHandler(db)))
mux.HandleFunc("/admin/draws/modify", middleware.AdminOnly(db, admin.ModifyDrawHandler(db))) mux.HandleFunc("/admin/draws/modify", middleware.AdminOnly(db, admin.ModifyDrawHandler(db)))
mux.HandleFunc("/admin/draws/delete", middleware.AdminOnly(db, admin.DeleteDrawHandler(db))) mux.HandleFunc("/admin/draws/delete", middleware.AdminOnly(db, admin.DeleteDrawHandler(db)))

10
models/template.go Normal file
View File

@@ -0,0 +1,10 @@
package models
type TemplateData struct {
User *User
IsAdmin bool
NotificationCount int
Notifications []Notification
MessageCount int
Messages []Message
}