- 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
45 lines
1.0 KiB
Go
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)
|
|
}
|
|
}
|