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 00000000..29a2d0fb --- /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 00000000..3366d41b --- /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 f858e364..a7b1b87b 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 00000000..0ad8f464 --- /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 0edf8f7b..07b6688a 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, );