diff --git a/handlers/draw_handler.go b/handlers/draw_handler.go index 13231ca..1b9d1e0 100644 --- a/handlers/draw_handler.go +++ b/handlers/draw_handler.go @@ -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", diff --git a/handlers/notifications.go b/handlers/notifications.go new file mode 100644 index 0000000..d8cfcbb --- /dev/null +++ b/handlers/notifications.go @@ -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) + } +} diff --git a/handlers/template_context.go b/handlers/template_context.go index 883a31d..13a20f7 100644 --- a/handlers/template_context.go +++ b/handlers/template_context.go @@ -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, } } diff --git a/helpers/template.go b/helpers/template.go index 5cf79ea..afbe856 100644 --- a/helpers/template.go +++ b/helpers/template.go @@ -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 } - 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.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, - } + session.Values["flash"] = message + session.Save(r, w) } 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) -} diff --git a/main.go b/main.go index f5c50be..ba50f09 100644 --- a/main.go +++ b/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))) diff --git a/models/template.go b/models/template.go new file mode 100644 index 0000000..0f40783 --- /dev/null +++ b/models/template.go @@ -0,0 +1,10 @@ +package models + +type TemplateData struct { + User *User + IsAdmin bool + NotificationCount int + Notifications []Notification + MessageCount int + Messages []Message +}