diff --git a/handlers/account.go b/handlers/account.go index c8793a8..e314e8c 100644 --- a/handlers/account.go +++ b/handlers/account.go @@ -19,8 +19,9 @@ func Login(w http.ResponseWriter, r *http.Request) { return } - tmpl := template.Must(template.ParseFiles( + tmpl := template.Must(template.New("login.html").Funcs(helpers.TemplateFuncs()).ParseFiles( "templates/layout.html", + "templates/topbar.html", "templates/account/login.html", )) diff --git a/handlers/home.go b/handlers/home.go index 9502744..fa6f20a 100644 --- a/handlers/home.go +++ b/handlers/home.go @@ -5,60 +5,23 @@ import ( "html/template" "log" "net/http" - "sort" "synlotto-website/helpers" - "synlotto-website/models" ) -// Home shows latest Thunderball results func Home(db *sql.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - rows, err := db.Query(` - SELECT id, draw_date, machine, ballset, ball1, ball2, ball3, ball4, ball5, thunderball - FROM results_thunderball - ORDER BY id DESC - `) - if err != nil { - log.Println("❌ DB error:", err) - http.Error(w, "Database error", http.StatusInternalServerError) - return - } - defer rows.Close() - - var results []models.ThunderballResult - - for rows.Next() { - var res models.ThunderballResult - err := rows.Scan( - &res.Id, &res.DrawDate, &res.Machine, &res.BallSet, - &res.Ball1, &res.Ball2, &res.Ball3, &res.Ball4, &res.Ball5, &res.Thunderball, - ) - if err != nil { - log.Println("❌ Row scan error:", err) - continue - } - - res.SortedBalls = []int{ - res.Ball1, res.Ball2, res.Ball3, res.Ball4, res.Ball5, - } - sort.Ints(res.SortedBalls) - - results = append(results, res) - } - data := BuildTemplateData(db, w, r) context := helpers.TemplateContext(w, r, data) - context["Data"] = results - tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles( + tmpl := template.Must(template.New("index.html").Funcs(helpers.TemplateFuncs()).ParseFiles( "templates/layout.html", "templates/topbar.html", "templates/index.html", )) - err = tmpl.ExecuteTemplate(w, "layout", context) + err := tmpl.ExecuteTemplate(w, "layout", context) if err != nil { - log.Println("❌ Template error:", err) + log.Println("❌ Template render error:", err) http.Error(w, "Error rendering homepage", http.StatusInternalServerError) } } diff --git a/handlers/notifications.go b/handlers/notifications.go index 510d1cb..37a356c 100644 --- a/handlers/notifications.go +++ b/handlers/notifications.go @@ -2,6 +2,7 @@ package handlers import ( "database/sql" + "log" "net/http" "strconv" "text/template" @@ -20,11 +21,12 @@ func NotificationsHandler(db *sql.DB) http.HandlerFunc { ParseFiles( "templates/layout.html", "templates/topbar.html", - "templates/account/notifications/index.html", + "templates/account/notifications.html", )) err := tmpl.ExecuteTemplate(w, "layout", context) if err != nil { + log.Println("❌ Template render error:", err) http.Error(w, "Error rendering notifications page", http.StatusInternalServerError) } } @@ -46,12 +48,33 @@ func MarkNotificationReadHandler(db *sql.DB) http.HandlerFunc { return } - err = storage.MarkNotificationAsRead(db, userID, notificationID) + notification, err := storage.GetNotificationByID(db, userID, notificationID) if err != nil { - http.Error(w, "Failed to update", http.StatusInternalServerError) - return + log.Printf("❌ Notification not found or belongs to another user: %v", err) + notification = nil + } else if !notification.IsRead { + err = storage.MarkNotificationAsRead(db, userID, notificationID) + if err != nil { + log.Printf("⚠️ Failed to mark as read: %v", err) + } } - http.Redirect(w, r, "/account/notifications", http.StatusSeeOther) + data := BuildTemplateData(db, w, r) + context := helpers.TemplateContext(w, r, data) + context["Notification"] = notification + + tmpl := template.Must(template.New("read.html"). + Funcs(helpers.TemplateFuncs()). + ParseFiles( + "templates/layout.html", + "templates/topbar.html", + "templates/account/notifications/read.html", + )) + + err = tmpl.ExecuteTemplate(w, "layout", context) + if err != nil { + log.Printf("❌ Template render error: %v", err) + http.Error(w, "Template render error", http.StatusInternalServerError) + } } } diff --git a/models/user.go b/models/user.go index 447337f..daff8c9 100644 --- a/models/user.go +++ b/models/user.go @@ -15,8 +15,9 @@ type User struct { type Notification struct { ID int - Title string - Message string + UserId int + Subject string + Body string IsRead bool CreatedAt time.Time } diff --git a/storage/notifications.go b/storage/notifications.go index 4c7777f..9d4dbf9 100644 --- a/storage/notifications.go +++ b/storage/notifications.go @@ -39,7 +39,7 @@ func GetRecentNotifications(db *sql.DB, userID int, limit int) []models.Notifica for rows.Next() { var n models.Notification - if err := rows.Scan(&n.ID, &n.Title, &n.Message, &n.IsRead, &n.CreatedAt); err == nil { + if err := rows.Scan(&n.ID, &n.Subject, &n.Body, &n.IsRead, &n.CreatedAt); err == nil { notifications = append(notifications, n) } } @@ -49,11 +49,10 @@ func GetRecentNotifications(db *sql.DB, userID int, limit int) []models.Notifica func MarkNotificationAsRead(db *sql.DB, userID int, notificationID int) error { result, err := db.Exec(` - UPDATE notifications + UPDATE users_notification SET is_read = TRUE WHERE id = ? AND user_id = ? `, notificationID, userID) - if err != nil { return err } @@ -62,9 +61,25 @@ func MarkNotificationAsRead(db *sql.DB, userID int, notificationID int) error { if err != nil { return err } + if rowsAffected == 0 { - return fmt.Errorf("no matching notification found or not owned by user") + return fmt.Errorf("no matching notification for user_id=%d and id=%d", userID, notificationID) } return nil } + +func GetNotificationByID(db *sql.DB, userID, notificationID int) (*models.Notification, error) { + row := db.QueryRow(` + SELECT id, user_id, subject, body, is_read + FROM users_notification + WHERE id = ? AND user_id = ? + `, notificationID, userID) + + var n models.Notification + err := row.Scan(&n.ID, &n.UserId, &n.Subject, &n.Body, &n.IsRead) + if err != nil { + return nil, err + } + return &n, nil +} diff --git a/templates/account/login.html b/templates/account/login.html index 74279b0..ff8e6be 100644 --- a/templates/account/login.html +++ b/templates/account/login.html @@ -1,10 +1,23 @@ {{ define "content" }}
{{ .Notification.Message }}
- Back to Notifications + {{ if .Notification }} +{{ .Notification.Body }}
+ {{ else }} +Your trusted lottery platform!
{{ end }} diff --git a/templates/topbar.html b/templates/topbar.html index f7efb99..3ed641e 100644 --- a/templates/topbar.html +++ b/templates/topbar.html @@ -55,8 +55,8 @@