hacky-ruadat/src/model/notification.rs
2024-03-04 10:03:53 +01:00

47 lines
1.1 KiB
Rust

use chrono::{DateTime, Local, NaiveDateTime, TimeZone, Utc};
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
#[derive(FromRow, Debug, Serialize, Deserialize)]
pub struct Notification {
pub id: i64,
pub user_id: i64,
pub message: String,
pub read_at: NaiveDateTime,
pub created_at: NaiveDateTime,
pub category: String,
pub link: Option<String>,
}
impl Notification {
pub async fn create(
db: &SqlitePool,
user: &User,
message: &str,
category: &str,
link: Option<&str>,
) {
sqlx::query!(
"INSERT INTO notification(user_id, message, category, link) VALUES (?, ?, ?, ?)",
user.id,
message,
category,
link
)
.execute(db)
.await
.unwrap()
}
async fn for_user(db: &SqlitePool, user: &User) -> Vec<Self> {
sqlx::query_as!(
Log,
"SELECT * FROM notification WHERE user_id = {}",
user.id
)
.fetch_all(db)
.await
.unwrap()
}
}