Feature: Full notification read view with conditional mark-as-read logic
- Added dedicated route and view for reading individual notifications (/account/notifications/read) - Ensured notification is only marked as read if it hasn't already been - Updated Notification model to use Subject and Body fields - Fixed field references in templates (Title → Subject, Message → Body) - Updated topbar dropdown to use correct field names and display logic - Gracefully handle "notification not found" cases in template output - Ensured consistent template parsing with layout and topbar inclusion - Improved error logging for better diagnosis
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user