From 045e885821492e541d96b35b97c0d51ec4578805 Mon Sep 17 00:00:00 2001 From: Bat Date: Sat, 19 May 2018 10:23:02 +0100 Subject: [PATCH] Add a Reshare model --- .../down.sql | 2 + .../2018-05-19-091428_create_reshares/up.sql | 8 ++++ src/models/mod.rs | 1 + src/models/reshares.rs | 38 +++++++++++++++++++ src/schema.rs | 13 +++++++ 5 files changed, 62 insertions(+) create mode 100644 migrations/2018-05-19-091428_create_reshares/down.sql create mode 100644 migrations/2018-05-19-091428_create_reshares/up.sql create mode 100644 src/models/reshares.rs diff --git a/migrations/2018-05-19-091428_create_reshares/down.sql b/migrations/2018-05-19-091428_create_reshares/down.sql new file mode 100644 index 0000000..29a2d0f --- /dev/null +++ b/migrations/2018-05-19-091428_create_reshares/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE reshares; diff --git a/migrations/2018-05-19-091428_create_reshares/up.sql b/migrations/2018-05-19-091428_create_reshares/up.sql new file mode 100644 index 0000000..3366d41 --- /dev/null +++ b/migrations/2018-05-19-091428_create_reshares/up.sql @@ -0,0 +1,8 @@ +-- Your SQL goes here +CREATE TABLE reshares ( + id SERIAL PRIMARY KEY, + user_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL, + post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE NOT NULL, + ap_url VARCHAR NOT NULL DEFAULT '', + creation_date TIMESTAMP NOT NULL DEFAULT now() +) diff --git a/src/models/mod.rs b/src/models/mod.rs index f858e36..a7b1b87 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -7,4 +7,5 @@ pub mod likes; pub mod notifications; pub mod post_authors; pub mod posts; +pub mod reshares; pub mod users; diff --git a/src/models/reshares.rs b/src/models/reshares.rs new file mode 100644 index 0000000..0ad8f46 --- /dev/null +++ b/src/models/reshares.rs @@ -0,0 +1,38 @@ +use chrono::NaiveDateTime; +use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; + +use schema::reshares; + +#[derive(Serialize, Deserialize, Queryable, Identifiable)] +pub struct Reshare { + id: i32, + user_id: i32, + post_id: i32, + ap_url: String, + creation_date: NaiveDateTime +} + +#[derive(Insertable)] +#[table_name = "reshares"] +pub struct NewReshare { + user_id: i32, + post_id: i32, + ap_url: String +} + +impl Reshare { + pub fn insert(conn: &PgConnection, new: NewReshare) -> Reshare { + diesel::insert_into(reshares::table) + .values(new) + .get_result(conn) + .expect("Couldn't save reshare") + } + + pub fn get(conn: &PgConnection, id: i32) -> Option { + reshares::table.filter(reshares::id.eq(id)) + .limit(1) + .load::(conn) + .expect("Could'nt load reshare") + .into_iter().nth(0) + } +} diff --git a/src/schema.rs b/src/schema.rs index 0edf8f7..07b6688 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -98,6 +98,16 @@ table! { } } +table! { + reshares (id) { + id -> Int4, + user_id -> Int4, + post_id -> Int4, + ap_url -> Varchar, + creation_date -> Timestamp, + } +} + table! { users (id) { id -> Int4, @@ -129,6 +139,8 @@ joinable!(notifications -> users (user_id)); joinable!(post_authors -> posts (post_id)); joinable!(post_authors -> users (author_id)); joinable!(posts -> blogs (blog_id)); +joinable!(reshares -> posts (post_id)); +joinable!(reshares -> users (user_id)); joinable!(users -> instances (instance_id)); allow_tables_to_appear_in_same_query!( @@ -141,5 +153,6 @@ allow_tables_to_appear_in_same_query!( notifications, post_authors, posts, + reshares, users, );