Files
website/handlers/template_context.go
H3ALY 03b1e095ce 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
2025-04-01 21:08:00 +01:00

45 lines
1.1 KiB
Go

package handlers
import (
"database/sql"
"net/http"
"synlotto-website/helpers"
"synlotto-website/models"
"synlotto-website/storage"
)
func BuildTemplateData(db *sql.DB, w http.ResponseWriter, r *http.Request) models.TemplateData {
session, _ := helpers.GetSession(w, r)
var user *models.User
var isAdmin bool
var notificationCount int
var notifications []models.Notification
var messageCount int
var messages []models.Message
switch v := session.Values["user_id"].(type) {
case int:
user = models.GetUserByID(v)
case int64:
user = models.GetUserByID(int(v))
}
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 models.TemplateData{
User: user,
IsAdmin: isAdmin,
NotificationCount: notificationCount,
Notifications: notifications,
MessageCount: messageCount,
Messages: messages,
}
}