Refactor and remove sqlite and replace with MySQL
This commit is contained in:
70
internal/handlers/notifications.go
Normal file
70
internal/handlers/notifications.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
templateHandlers "synlotto-website/handlers/template"
|
||||
httpHelpers "synlotto-website/helpers/http"
|
||||
templateHelpers "synlotto-website/helpers/template"
|
||||
|
||||
"synlotto-website/storage"
|
||||
)
|
||||
|
||||
func NotificationsHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
data := templateHandlers.BuildTemplateData(db, w, r)
|
||||
context := templateHelpers.TemplateContext(w, r, data)
|
||||
|
||||
tmpl := templateHelpers.LoadTemplateFiles("index.html", "templates/account/notifications/index.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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, _ := httpHelpers.GetSession(w, r)
|
||||
userID, ok := session.Values["user_id"].(int)
|
||||
if !ok {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
notification, err := storage.GetNotificationByID(db, userID, notificationID)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
data := templateHandlers.BuildTemplateData(db, w, r)
|
||||
context := templateHelpers.TemplateContext(w, r, data)
|
||||
context["Notification"] = notification
|
||||
|
||||
tmpl := templateHelpers.LoadTemplateFiles("read.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user