2018-05-01 15:06:31 +02:00
|
|
|
use diesel::{self, PgConnection, ExpressionMethods, QueryDsl, RunQueryDsl};
|
|
|
|
|
2018-05-01 15:23:23 +02:00
|
|
|
use models::users::User;
|
2018-05-01 15:06:31 +02:00
|
|
|
use schema::follows;
|
|
|
|
|
2018-05-01 15:23:23 +02:00
|
|
|
#[derive(Queryable, Identifiable, Associations)]
|
|
|
|
#[belongs_to(User, foreign_key = "following_id")]
|
2018-05-01 15:06:31 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|