diff --git a/migrations/2018-05-24-100613_add_notifications_creation_date/down.sql b/migrations/2018-05-24-100613_add_notifications_creation_date/down.sql new file mode 100644 index 0000000..ca04e11 --- /dev/null +++ b/migrations/2018-05-24-100613_add_notifications_creation_date/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE notifications DROP COLUMN creation_date; diff --git a/migrations/2018-05-24-100613_add_notifications_creation_date/up.sql b/migrations/2018-05-24-100613_add_notifications_creation_date/up.sql new file mode 100644 index 0000000..fddcc38 --- /dev/null +++ b/migrations/2018-05-24-100613_add_notifications_creation_date/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TABLE notifications ADD COLUMN creation_date TIMESTAMP NOT NULL DEFAULT now(); diff --git a/src/models/notifications.rs b/src/models/notifications.rs index 70ffde8..1087d5c 100644 --- a/src/models/notifications.rs +++ b/src/models/notifications.rs @@ -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, pub link: Option, - 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 { notifications::table.filter(notifications::user_id.eq(user.id)) + .order_by(notifications::creation_date.desc()) .load::(conn) .expect("Couldn't load user notifications") } diff --git a/src/schema.rs b/src/schema.rs index 07b6688..9c85dc6 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -73,6 +73,7 @@ table! { content -> Nullable, link -> Nullable, user_id -> Int4, + creation_date -> Timestamp, } }