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:
@@ -6,7 +6,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"synlotto-website/handlers"
|
||||
"synlotto-website/helpers"
|
||||
"synlotto-website/models"
|
||||
)
|
||||
@@ -19,7 +18,7 @@ func NewDraw(db *sql.DB) http.HandlerFunc {
|
||||
context["Page"] = "new_draw"
|
||||
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/topbar.html",
|
||||
"templates/new_draw.html",
|
||||
|
||||
44
handlers/notifications.go
Normal file
44
handlers/notifications.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -6,55 +6,39 @@ import (
|
||||
"synlotto-website/helpers"
|
||||
"synlotto-website/models"
|
||||
"synlotto-website/storage"
|
||||
|
||||
"github.com/gorilla/csrf"
|
||||
)
|
||||
|
||||
type TemplateData map[string]interface{}
|
||||
|
||||
func BuildTemplateContext(db *sql.DB, w http.ResponseWriter, r *http.Request) TemplateData {
|
||||
func BuildTemplateData(db *sql.DB, w http.ResponseWriter, r *http.Request) models.TemplateData {
|
||||
session, _ := helpers.GetSession(w, r)
|
||||
|
||||
var flash string
|
||||
if f, ok := session.Values["flash"].(string); ok {
|
||||
flash = f
|
||||
delete(session.Values, "flash")
|
||||
session.Save(r, w)
|
||||
}
|
||||
|
||||
var currentUser *models.User
|
||||
var user *models.User
|
||||
var isAdmin bool
|
||||
|
||||
notificationCount := 0
|
||||
notifications := []models.Notification{}
|
||||
messageCount := 0
|
||||
messages := []models.Message{}
|
||||
var notificationCount int
|
||||
var notifications []models.Notification
|
||||
var messageCount int
|
||||
var messages []models.Message
|
||||
|
||||
switch v := session.Values["user_id"].(type) {
|
||||
case int:
|
||||
currentUser = models.GetUserByID(v)
|
||||
user = models.GetUserByID(v)
|
||||
case int64:
|
||||
currentUser = models.GetUserByID(int(v))
|
||||
user = models.GetUserByID(int(v))
|
||||
}
|
||||
|
||||
if currentUser != nil {
|
||||
isAdmin = currentUser.IsAdmin
|
||||
|
||||
notificationCount = storage.GetNotificationCount(db, currentUser.Id)
|
||||
notifications = storage.GetRecentNotifications(db, currentUser.Id, 15)
|
||||
|
||||
messageCount, _ = storage.GetMessageCount(db, currentUser.Id)
|
||||
messages = storage.GetRecentMessages(db, currentUser.Id, 15)
|
||||
if user != nil {
|
||||
isAdmin = user.IsAdmin
|
||||
notificationCount = storage.GetNotificationCount(db, user.Id)
|
||||
notifications = storage.GetRecentNotifications(db, user.Id, 15)
|
||||
messageCount, _ = storage.GetMessageCount(db, user.Id)
|
||||
messages = storage.GetRecentMessages(db, user.Id, 15)
|
||||
}
|
||||
|
||||
return TemplateData{
|
||||
"CSRFField": csrf.TemplateField(r),
|
||||
"Flash": flash,
|
||||
"User": currentUser,
|
||||
"IsAdmin": isAdmin,
|
||||
"NotificationCount": notificationCount,
|
||||
"Notifications": notifications,
|
||||
"MessageCount": messageCount,
|
||||
"Messages": messages,
|
||||
return models.TemplateData{
|
||||
User: user,
|
||||
IsAdmin: isAdmin,
|
||||
NotificationCount: notificationCount,
|
||||
Notifications: notifications,
|
||||
MessageCount: messageCount,
|
||||
Messages: messages,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,35 @@ import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"synlotto-website/models"
|
||||
"synlotto-website/storage"
|
||||
|
||||
"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 {
|
||||
return template.FuncMap{
|
||||
"plus1": func(i int) int { return i + 1 },
|
||||
@@ -24,8 +47,9 @@ func TemplateFuncs() template.FuncMap {
|
||||
"min": func(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
},
|
||||
"intVal": func(p *int) int {
|
||||
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)
|
||||
|
||||
var flash string
|
||||
if f, ok := session.Values["flash"].(string); ok {
|
||||
flash = f
|
||||
delete(session.Values, "flash")
|
||||
session.Values["flash"] = message
|
||||
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 {
|
||||
@@ -83,7 +76,6 @@ func InSlice(n int, list []int) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -107,9 +99,3 @@ func rangeClass(n int) string {
|
||||
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)
|
||||
}
|
||||
|
||||
4
main.go
4
main.go
@@ -52,8 +52,8 @@ func setupAdminRoutes(mux *http.ServeMux, db *sql.DB) {
|
||||
|
||||
// Draw management
|
||||
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/submit", middleware.AdminOnly(db, admin.CreateDrawHandler(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/modify", middleware.AdminOnly(db, admin.ModifyDrawHandler(db)))
|
||||
mux.HandleFunc("/admin/draws/delete", middleware.AdminOnly(db, admin.DeleteDrawHandler(db)))
|
||||
|
||||
|
||||
10
models/template.go
Normal file
10
models/template.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package models
|
||||
|
||||
type TemplateData struct {
|
||||
User *User
|
||||
IsAdmin bool
|
||||
NotificationCount int
|
||||
Notifications []Notification
|
||||
MessageCount int
|
||||
Messages []Message
|
||||
}
|
||||
Reference in New Issue
Block a user