Files
website/storage/notifications.go

48 lines
1015 B
Go

package storage
import (
"database/sql"
"log"
"synlotto-website/models"
)
func GetNotificationCount(db *sql.DB, userID int) int {
var count int
err := db.QueryRow(`
SELECT COUNT(*) FROM users_notification
WHERE user_id = ? AND is_read = FALSE`, userID).Scan(&count)
if err != nil {
log.Println("⚠️ Failed to count notifications:", err)
return 0
}
return count
}
func GetRecentNotifications(db *sql.DB, userID int, limit int) []models.Notification {
rows, err := db.Query(`
SELECT id, subject, body, is_read, created_at
FROM users_notification
WHERE user_id = ?
ORDER BY created_at DESC
LIMIT ?`, userID, limit)
if err != nil {
log.Println("⚠️ Failed to get notifications:", err)
return nil
}
defer rows.Close()
var notifications []models.Notification
for rows.Next() {
var n models.Notification
if err := rows.Scan(&n.ID, &n.Title, &n.Message, &n.IsRead, &n.CreatedAt); err == nil {
notifications = append(notifications, n)
}
}
return notifications
}