From a0b4a6eacb30891f0df865fff82471c67e58e056 Mon Sep 17 00:00:00 2001 From: Bat Date: Thu, 24 May 2018 11:12:27 +0100 Subject: [PATCH] Order notifications by creation date --- .../down.sql | 2 ++ .../2018-05-24-100613_add_notifications_creation_date/up.sql | 2 ++ src/models/notifications.rs | 5 ++++- src/schema.rs | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 migrations/2018-05-24-100613_add_notifications_creation_date/down.sql create mode 100644 migrations/2018-05-24-100613_add_notifications_creation_date/up.sql 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 00000000..ca04e11e --- /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 00000000..fddcc387 --- /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 70ffde89..1087d5cb 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 07b6688a..9c85dc6f 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -73,6 +73,7 @@ table! { content -> Nullable, link -> Nullable, user_id -> Int4, + creation_date -> Timestamp, } }