Files
website/handlers/notifications.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.0 KiB
Go

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)
}
}