Delete likes
This commit is contained in:
+16
-1
@@ -1,9 +1,11 @@
|
||||
use chrono;
|
||||
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
|
||||
|
||||
use models::posts::Post;
|
||||
use models::users::User;
|
||||
use schema::likes;
|
||||
|
||||
#[derive(Queryable)]
|
||||
#[derive(Queryable, Identifiable)]
|
||||
pub struct Like {
|
||||
pub id: i32,
|
||||
pub user_id: i32,
|
||||
@@ -33,4 +35,17 @@ impl Like {
|
||||
.expect("Error loading like by ID")
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
|
||||
pub fn for_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option<Like> {
|
||||
likes::table.filter(likes::post_id.eq(post.id))
|
||||
.filter(likes::user_id.eq(user.id))
|
||||
.limit(1)
|
||||
.load::<Like>(conn)
|
||||
.expect("Error loading like for user and post")
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
|
||||
pub fn delete(&self, conn: &PgConnection) {
|
||||
diesel::delete(self).execute(conn).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
+12
-1
@@ -31,7 +31,7 @@ use schema::users;
|
||||
|
||||
pub const AUTH_COOKIE: &'static str = "user_id";
|
||||
|
||||
#[derive(Queryable, Identifiable, Serialize)]
|
||||
#[derive(Queryable, Identifiable, Serialize, Clone)]
|
||||
pub struct User {
|
||||
pub id: i32,
|
||||
pub username: String,
|
||||
@@ -231,6 +231,17 @@ impl User {
|
||||
users::table.filter(users::id.eq(any(follows))).load::<User>(conn).unwrap()
|
||||
}
|
||||
|
||||
pub fn has_liked(&self, conn: &PgConnection, post: &Post) -> bool {
|
||||
use schema::likes;
|
||||
use models::likes::Like;
|
||||
likes::table
|
||||
.filter(likes::post_id.eq(post.id))
|
||||
.filter(likes::user_id.eq(self.id))
|
||||
.load::<Like>(conn)
|
||||
.expect("Couldn't load likes")
|
||||
.len() > 0
|
||||
}
|
||||
|
||||
pub fn get_keypair(&self) -> PKey<Private> {
|
||||
PKey::from_rsa(Rsa::private_key_from_pem(self.private_key.clone().unwrap().as_ref()).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user