use crate::{db::Pool, error::AppResult, models::notification::Notification}; pub async fn create(pool: &Pool, kind: &str, title: &str, body: &str) -> AppResult<()> { let n = Notification::new(kind, title, body); sqlx::query( "INSERT INTO notifications (id, kind, title, body, is_read, created_at) \ VALUES (?, ?, ?, ?, 0, ?)", ) .bind(&n.id) .bind(&n.kind) .bind(&n.title) .bind(&n.body) .bind(&n.created_at) .execute(pool) .await?; Ok(()) } pub async fn list(pool: &Pool) -> AppResult> { sqlx::query_as::<_, Notification>( "SELECT id, kind, title, body, is_read, created_at \ FROM notifications ORDER BY created_at DESC LIMIT 50", ) .fetch_all(pool) .await .map_err(Into::into) } pub async fn mark_read(pool: &Pool, id: &str) -> AppResult { let r = sqlx::query("UPDATE notifications SET is_read = 1 WHERE id = ?") .bind(id) .execute(pool) .await?; Ok(r.rows_affected() > 0) } pub async fn mark_all_read(pool: &Pool) -> AppResult { let r = sqlx::query("UPDATE notifications SET is_read = 1 WHERE is_read = 0") .execute(pool) .await?; Ok(r.rows_affected() as i64) } pub async fn unread_count(pool: &Pool) -> AppResult { sqlx::query_scalar("SELECT COUNT(*) FROM notifications WHERE is_read = 0") .fetch_one(pool) .await .map_err(Into::into) }