threadr-rewritten/models/notification.go

49 lines
1.5 KiB
Go

package models
import (
"database/sql"
"time"
)
type Notification struct {
ID int
UserID int
Type string
RelatedID int
CreatedAt time.Time
Read bool
}
func GetNotificationsByUserID(db *sql.DB, userID int) ([]Notification, error) {
query := "SELECT id, user_id, type, related_id, read, created_at FROM notifications WHERE user_id = ? ORDER BY created_at DESC"
rows, err := db.Query(query, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var notifications []Notification
for rows.Next() {
notification := Notification{}
err := rows.Scan(&notification.ID, &notification.UserID, &notification.Type, &notification.RelatedID, &notification.Read, &notification.CreatedAt)
if err != nil {
return nil, err
}
notifications = append(notifications, notification)
}
return notifications, nil
}
// Stubbed for future implementation
func CreateNotification(db *sql.DB, notification Notification) error {
query := "INSERT INTO notifications (user_id, type, related_id, read, created_at) VALUES (?, ?, ?, ?, NOW())"
_, err := db.Exec(query, notification.UserID, notification.Type, notification.RelatedID, notification.Read)
return err
}
// Stubbed for future implementation
func MarkNotificationAsRead(db *sql.DB, id int) error {
query := "UPDATE notifications SET read = true WHERE id = ?"
_, err := db.Exec(query, id)
return err
}