Add a follow model

This commit is contained in:
Bat 2018-05-01 14:06:31 +01:00
parent 4daa482aa8
commit 78f3062f6d
5 changed files with 52 additions and 0 deletions

View File

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

View File

@ -0,0 +1,6 @@
-- Your SQL goes here
CREATE TABLE follows (
id SERIAL PRIMARY KEY,
follower_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL,
following_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL
)

34
src/models/follows.rs Normal file
View File

@ -0,0 +1,34 @@
use diesel::{self, PgConnection, ExpressionMethods, QueryDsl, RunQueryDsl};
use schema::follows;
#[derive(Queryable, Identifiable)]
pub struct Follow {
pub id: i32,
pub follower_id: i32,
pub following_id: i32
}
#[derive(Insertable)]
#[table_name = "follows"]
pub struct NewFollow {
pub follower_id: i32,
pub following_id: i32
}
impl Follow {
pub fn insert(conn: &PgConnection, new: NewFollow) -> Follow {
diesel::insert_into(follows::table)
.values(new)
.get_result(conn)
.expect("Unable to insert new follow")
}
pub fn get(conn: &PgConnection, id: i32) -> Option<Follow> {
follows::table.filter(follows::id.eq(id))
.limit(1)
.load::<Follow>(conn)
.expect("Unable to load follow by id")
.into_iter().nth(0)
}
}

View File

@ -1,5 +1,6 @@
pub mod blog_authors;
pub mod blogs;
pub mod follows;
pub mod instance;
pub mod post_authors;
pub mod posts;

View File

@ -20,6 +20,14 @@ table! {
}
}
table! {
follows (id) {
id -> Int4,
follower_id -> Int4,
following_id -> Int4,
}
}
table! {
instances (id) {
id -> Int4,
@ -80,6 +88,7 @@ joinable!(users -> instances (instance_id));
allow_tables_to_appear_in_same_query!(
blog_authors,
blogs,
follows,
instances,
post_authors,
posts,