mirror of
https://git.joinplu.me/Plume/Plume.git
synced 2024-12-23 10:30:41 +00:00
Add a follow model
This commit is contained in:
parent
4daa482aa8
commit
78f3062f6d
5 changed files with 52 additions and 0 deletions
2
migrations/2018-05-01-124607_create_follow/down.sql
Normal file
2
migrations/2018-05-01-124607_create_follow/down.sql
Normal file
|
@ -0,0 +1,2 @@
|
|||
-- This file should undo anything in `up.sql`
|
||||
DROP TABLE follows;
|
6
migrations/2018-05-01-124607_create_follow/up.sql
Normal file
6
migrations/2018-05-01-124607_create_follow/up.sql
Normal 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
34
src/models/follows.rs
Normal 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)
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
pub mod blog_authors;
|
||||
pub mod blogs;
|
||||
pub mod follows;
|
||||
pub mod instance;
|
||||
pub mod post_authors;
|
||||
pub mod posts;
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue