use chrono::NaiveDateTime; use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods}; use users::User; use schema::notifications; #[derive(Queryable, Identifiable, Serialize)] pub struct Notification { pub id: i32, pub title: String, pub content: Option, pub link: Option, pub user_id: i32, pub creation_date: NaiveDateTime, pub data: Option } #[derive(Insertable)] #[table_name = "notifications"] pub struct NewNotification { pub title: String, pub content: Option, pub link: Option, pub user_id: i32, pub data: Option } impl Notification { insert!(notifications, NewNotification); get!(notifications); pub fn find_for_user(conn: &PgConnection, user: &User) -> Vec { notifications::table.filter(notifications::user_id.eq(user.id)) .order_by(notifications::creation_date.desc()) .load::(conn) .expect("Couldn't load user notifications") } pub fn page_for_user(conn: &PgConnection, user: &User, (min, max): (i32, i32)) -> Vec { notifications::table.filter(notifications::user_id.eq(user.id)) .order_by(notifications::creation_date.desc()) .offset(min.into()) .limit((max - min).into()) .load::(conn) .expect("Couldn't load user notifications page") } }