From 780a51157f84c4c5f6ac013bf2dc6536844514db Mon Sep 17 00:00:00 2001 From: Bat Date: Sun, 13 May 2018 13:44:18 +0100 Subject: [PATCH] Add a notification model --- .../down.sql | 2 + .../up.sql | 8 ++++ src/models/mod.rs | 1 + src/models/notifications.rs | 38 +++++++++++++++++++ src/schema.rs | 12 ++++++ 5 files changed, 61 insertions(+) create mode 100644 migrations/2018-05-13-122311_create_notifications/down.sql create mode 100644 migrations/2018-05-13-122311_create_notifications/up.sql create mode 100644 src/models/notifications.rs diff --git a/migrations/2018-05-13-122311_create_notifications/down.sql b/migrations/2018-05-13-122311_create_notifications/down.sql new file mode 100644 index 00000000..bcebcc05 --- /dev/null +++ b/migrations/2018-05-13-122311_create_notifications/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE notifications; diff --git a/migrations/2018-05-13-122311_create_notifications/up.sql b/migrations/2018-05-13-122311_create_notifications/up.sql new file mode 100644 index 00000000..f8d11849 --- /dev/null +++ b/migrations/2018-05-13-122311_create_notifications/up.sql @@ -0,0 +1,8 @@ +-- Your SQL goes here +CREATE TABLE notifications ( + id SERIAL PRIMARY KEY, + title VARCHAR NOT NULL DEFAULT '', + content TEXT, + link VARCHAR, + user_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL +) diff --git a/src/models/mod.rs b/src/models/mod.rs index eb093a13..f858e364 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -4,6 +4,7 @@ pub mod comments; pub mod follows; pub mod instance; pub mod likes; +pub mod notifications; pub mod post_authors; pub mod posts; pub mod users; diff --git a/src/models/notifications.rs b/src/models/notifications.rs new file mode 100644 index 00000000..c35e4f04 --- /dev/null +++ b/src/models/notifications.rs @@ -0,0 +1,38 @@ +use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods}; + +use schema::notifications; + +#[derive(Queryable, Identifiable)] +pub struct Notification { + pub id: i32, + pub title: String, + pub content: Option, + pub link: Option, + pub user_id: i32 +} + +#[derive(Insertable)] +#[table_name = "notifications"] +pub struct NewNotification { + pub title: String, + pub content: Option, + pub link: Option, + pub user_id: i32 +} + +impl Notification { + pub fn insert(conn: &PgConnection, new: NewNotification) -> Notification { + diesel::insert_into(notifications::table) + .values(new) + .get_result(conn) + .expect("Couldn't save notification") + } + + pub fn get(conn: &PgConnection, id: i32) -> Option { + notifications::table.filter(notifications::id.eq(id)) + .limit(1) + .load::(conn) + .expect("Couldn't load notification by ID") + .into_iter().nth(0) + } +} diff --git a/src/schema.rs b/src/schema.rs index af20e6d5..264a2b8d 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -66,6 +66,16 @@ table! { } } +table! { + notifications (id) { + id -> Int4, + title -> Varchar, + content -> Nullable, + link -> Nullable, + user_id -> Int4, + } +} + table! { post_authors (id) { id -> Int4, @@ -114,6 +124,7 @@ joinable!(comments -> posts (post_id)); joinable!(comments -> users (author_id)); joinable!(likes -> posts (post_id)); joinable!(likes -> users (user_id)); +joinable!(notifications -> users (user_id)); joinable!(post_authors -> posts (post_id)); joinable!(post_authors -> users (author_id)); joinable!(posts -> blogs (blog_id)); @@ -126,6 +137,7 @@ allow_tables_to_appear_in_same_query!( follows, instances, likes, + notifications, post_authors, posts, users,