diff --git a/handlers/notifications.go b/handlers/notifications.go index e9086d3..510d1cb 100644 --- a/handlers/notifications.go +++ b/handlers/notifications.go @@ -3,9 +3,11 @@ package handlers import ( "database/sql" "net/http" + "strconv" "text/template" "synlotto-website/helpers" + "synlotto-website/storage" ) func NotificationsHandler(db *sql.DB) http.HandlerFunc { @@ -18,7 +20,7 @@ func NotificationsHandler(db *sql.DB) http.HandlerFunc { ParseFiles( "templates/layout.html", "templates/topbar.html", - "templates/notifications/index.html", + "templates/account/notifications/index.html", )) err := tmpl.ExecuteTemplate(w, "layout", context) @@ -27,3 +29,29 @@ func NotificationsHandler(db *sql.DB) http.HandlerFunc { } } } + +func MarkNotificationReadHandler(db *sql.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + notificationIDStr := r.URL.Query().Get("id") + notificationID, err := strconv.Atoi(notificationIDStr) + if err != nil { + http.Error(w, "Invalid notification ID", http.StatusBadRequest) + return + } + + session, _ := helpers.GetSession(w, r) + userID, ok := session.Values["user_id"].(int) + if !ok { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + + err = storage.MarkNotificationAsRead(db, userID, notificationID) + if err != nil { + http.Error(w, "Failed to update", http.StatusInternalServerError) + return + } + + http.Redirect(w, r, "/account/notifications", http.StatusSeeOther) + } +} diff --git a/main.go b/main.go index ba50f09..81d2e66 100644 --- a/main.go +++ b/main.go @@ -68,6 +68,7 @@ func setupAccountRoutes(mux *http.ServeMux, db *sql.DB) { mux.HandleFunc("/signup", middleware.Auth(false)(handlers.Signup)) mux.HandleFunc("/account/tickets/add_ticket", handlers.AddTicket(db)) mux.HandleFunc("/account/tickets/my_tickets", handlers.GetMyTickets(db)) + mux.HandleFunc("/account/notifications/read", middleware.Auth(true)(handlers.MarkNotificationReadHandler(db))) } func setupResultRoutes(mux *http.ServeMux, db *sql.DB) { diff --git a/storage/notifications.go b/storage/notifications.go index 6d300c7..4c7777f 100644 --- a/storage/notifications.go +++ b/storage/notifications.go @@ -2,6 +2,7 @@ package storage import ( "database/sql" + "fmt" "log" "synlotto-website/models" @@ -45,3 +46,25 @@ func GetRecentNotifications(db *sql.DB, userID int, limit int) []models.Notifica return notifications } + +func MarkNotificationAsRead(db *sql.DB, userID int, notificationID int) error { + result, err := db.Exec(` + UPDATE notifications + SET is_read = TRUE + WHERE id = ? AND user_id = ? + `, notificationID, userID) + + if err != nil { + return err + } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + if rowsAffected == 0 { + return fmt.Errorf("no matching notification found or not owned by user") + } + + return nil +} diff --git a/templates/account/notifications/read.html b/templates/account/notifications/read.html new file mode 100644 index 0000000..67cb7a1 --- /dev/null +++ b/templates/account/notifications/read.html @@ -0,0 +1,12 @@ +{{ define "notifications_read" }} +