Cleanup: Finalize template context integration and remove legacy code
- Replaced legacy TemplateContext calls with structured TemplateData usage - Removed unused variables and redundant storage calls in notifications handler - Ensured consistent use of BuildTemplateData across user-facing handlers - Resolved all compile-time errors from refactor - Ready for runtime testing and further layout integration
This commit is contained in:
@@ -1,44 +1,29 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"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)
|
||||
func NotificationsHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
data := BuildTemplateData(db, w, r)
|
||||
context := helpers.TemplateContext(w, r, data)
|
||||
|
||||
var user *models.User
|
||||
switch v := session.Values["user_id"].(type) {
|
||||
case int:
|
||||
user = models.GetUserByID(v)
|
||||
case int64:
|
||||
user = models.GetUserByID(int(v))
|
||||
}
|
||||
tmpl := template.Must(template.New("notifications.html").
|
||||
Funcs(helpers.TemplateFuncs()).
|
||||
ParseFiles(
|
||||
"templates/layout.html",
|
||||
"templates/topbar.html",
|
||||
"templates/notifications/index.html",
|
||||
))
|
||||
|
||||
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)
|
||||
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||
if err != nil {
|
||||
http.Error(w, "Error rendering notifications page", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user