use chrono; use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; use models::posts::Post; use models::users::User; use schema::likes; #[derive(Queryable, Identifiable)] pub struct Like { pub id: i32, pub user_id: i32, pub post_id: i32, pub creation_date: chrono::NaiveDateTime } #[derive(Insertable)] #[table_name = "likes"] pub struct NewLike { pub user_id: i32, pub post_id: i32 } impl Like { pub fn insert(conn: &PgConnection, new: NewLike) -> Like { diesel::insert_into(likes::table) .values(new) .get_result(conn) .expect("Unable to insert new like") } pub fn get(conn: &PgConnection, id: i32) -> Option { likes::table.filter(likes::id.eq(id)) .limit(1) .load::(conn) .expect("Error loading like by ID") .into_iter().nth(0) } pub fn for_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option { likes::table.filter(likes::post_id.eq(post.id)) .filter(likes::user_id.eq(user.id)) .limit(1) .load::(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(); } }