Add a Reshare model

This commit is contained in:
Bat 2018-05-19 10:23:02 +01:00
parent 14bf3f00aa
commit 045e885821
5 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE reshares;

View File

@ -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()
)

View File

@ -7,4 +7,5 @@ pub mod likes;
pub mod notifications;
pub mod post_authors;
pub mod posts;
pub mod reshares;
pub mod users;

38
src/models/reshares.rs Normal file
View File

@ -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<Reshare> {
reshares::table.filter(reshares::id.eq(id))
.limit(1)
.load::<Reshare>(conn)
.expect("Could'nt load reshare")
.into_iter().nth(0)
}
}

View File

@ -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,
);