2018-05-24 12:12:27 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2018-05-13 14:44:18 +02:00
|
|
|
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods};
|
|
|
|
|
2018-05-13 15:35:55 +02:00
|
|
|
use models::users::User;
|
2018-05-13 14:44:18 +02:00
|
|
|
use schema::notifications;
|
|
|
|
|
2018-05-13 15:35:55 +02:00
|
|
|
#[derive(Queryable, Identifiable, Serialize)]
|
2018-05-13 14:44:18 +02:00
|
|
|
pub struct Notification {
|
|
|
|
pub id: i32,
|
|
|
|
pub title: String,
|
|
|
|
pub content: Option<String>,
|
|
|
|
pub link: Option<String>,
|
2018-05-24 12:12:27 +02:00
|
|
|
pub user_id: i32,
|
2018-06-17 22:19:27 +02:00
|
|
|
pub creation_date: NaiveDateTime,
|
|
|
|
pub data: Option<String>
|
2018-05-13 14:44:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "notifications"]
|
|
|
|
pub struct NewNotification {
|
|
|
|
pub title: String,
|
|
|
|
pub content: Option<String>,
|
|
|
|
pub link: Option<String>,
|
2018-06-17 22:19:27 +02:00
|
|
|
pub user_id: i32,
|
|
|
|
pub data: Option<String>
|
2018-05-13 14:44:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Notification {
|
2018-06-18 15:57:38 +02:00
|
|
|
insert!(notifications, NewNotification);
|
2018-06-18 15:44:23 +02:00
|
|
|
get!(notifications);
|
2018-05-13 15:35:55 +02:00
|
|
|
|
|
|
|
pub fn find_for_user(conn: &PgConnection, user: &User) -> Vec<Notification> {
|
|
|
|
notifications::table.filter(notifications::user_id.eq(user.id))
|
2018-05-24 12:12:27 +02:00
|
|
|
.order_by(notifications::creation_date.desc())
|
2018-05-13 15:35:55 +02:00
|
|
|
.load::<Notification>(conn)
|
|
|
|
.expect("Couldn't load user notifications")
|
|
|
|
}
|
2018-05-13 14:44:18 +02:00
|
|
|
}
|