Order notifications by creation date

This commit is contained in:
Bat 2018-05-24 11:12:27 +01:00
parent daf9120fba
commit a0b4a6eacb
4 changed files with 9 additions and 1 deletions

View file

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE notifications DROP COLUMN creation_date;

View file

@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TABLE notifications ADD COLUMN creation_date TIMESTAMP NOT NULL DEFAULT now();

View file

@ -1,3 +1,4 @@
use chrono::NaiveDateTime;
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods};
use models::users::User;
@ -9,7 +10,8 @@ pub struct Notification {
pub title: String,
pub content: Option<String>,
pub link: Option<String>,
pub user_id: i32
pub user_id: i32,
pub creation_date: NaiveDateTime
}
#[derive(Insertable)]
@ -39,6 +41,7 @@ impl Notification {
pub fn find_for_user(conn: &PgConnection, user: &User) -> Vec<Notification> {
notifications::table.filter(notifications::user_id.eq(user.id))
.order_by(notifications::creation_date.desc())
.load::<Notification>(conn)
.expect("Couldn't load user notifications")
}

View file

@ -73,6 +73,7 @@ table! {
content -> Nullable<Text>,
link -> Nullable<Varchar>,
user_id -> Int4,
creation_date -> Timestamp,
}
}