Refactoring for Gin, NoSurf and SCS continues.
This commit is contained in:
3
internal/storage/notifications/create.go
Normal file
3
internal/storage/notifications/create.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package storage
|
||||
|
||||
// ToDo: somethign must create notifications?
|
||||
3
internal/storage/notifications/delete.go
Normal file
3
internal/storage/notifications/delete.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package storage
|
||||
|
||||
// ToDo: not used, check messages and do something similar maybe dont store them?
|
||||
63
internal/storage/notifications/read.go
Normal file
63
internal/storage/notifications/read.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package storage
|
||||
|
||||
//ToDo: should be using my own logging wrapper?
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
"synlotto-website/internal/models"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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.Subject, &n.Body, &n.IsRead, &n.CreatedAt); err == nil {
|
||||
notifications = append(notifications, n)
|
||||
}
|
||||
}
|
||||
|
||||
return notifications
|
||||
}
|
||||
29
internal/storage/notifications/update.go
Normal file
29
internal/storage/notifications/update.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package storage
|
||||
|
||||
// ToDo: Should be logging fmt to my loggign wrapper
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func MarkNotificationAsRead(db *sql.DB, userID int, notificationID int) error {
|
||||
result, err := db.Exec(`
|
||||
UPDATE users_notification
|
||||
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 for user_id=%d and id=%d", userID, notificationID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user