Improve the find_by! macro to allow multiple columns
This commit is contained in:
parent
cd1d0d9627
commit
fa2435e725
|
@ -67,14 +67,7 @@ impl Blog {
|
|||
.expect("Couldn't load blogs ")
|
||||
}
|
||||
|
||||
pub fn find_by_name(conn: &PgConnection, name: String, instance_id: i32) -> Option<Blog> {
|
||||
blogs::table.filter(blogs::actor_id.eq(name))
|
||||
.filter(blogs::instance_id.eq(instance_id))
|
||||
.limit(1)
|
||||
.load::<Blog>(conn)
|
||||
.expect("Error loading blog by name")
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
find_by!(blogs, find_by_name, ap_url as String, instance_id as i32);
|
||||
|
||||
pub fn find_local(conn: &PgConnection, name: String) -> Option<Blog> {
|
||||
Blog::find_by_name(conn, name, Instance::local_id(conn))
|
||||
|
|
|
@ -49,7 +49,6 @@ pub struct NewComment {
|
|||
impl Comment {
|
||||
insert!(comments, NewComment);
|
||||
get!(comments);
|
||||
|
||||
find_by!(comments, find_by_post, post_id as i32);
|
||||
find_by!(comments, find_by_ap_url, ap_url as String);
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ impl Like {
|
|||
insert!(likes, NewLike);
|
||||
get!(likes);
|
||||
find_by!(likes, find_by_ap_url, ap_url as String);
|
||||
find_by!(likes, find_by_user_on_post, user_id as i32, post_id as i32);
|
||||
|
||||
pub fn update_ap_url(&self, conn: &PgConnection) {
|
||||
if self.ap_url.len() == 0 {
|
||||
|
@ -47,15 +48,6 @@ impl Like {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn find_by_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) -> activity::Undo {
|
||||
diesel::delete(self).execute(conn).unwrap();
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
// TODO: support multiple columns (see Like::find_by_user_on_post)
|
||||
macro_rules! find_by {
|
||||
($table:ident, $fn:ident, $col:ident as $type:ident) => {
|
||||
($table:ident, $fn:ident, $($col:ident as $type:ident),+) => {
|
||||
/// Try to find a $table with a given $col
|
||||
pub fn $fn(conn: &PgConnection, val: $type) -> Option<Self> {
|
||||
$table::table.filter($table::$col.eq(val))
|
||||
pub fn $fn(conn: &PgConnection, $($col: $type),+) -> Option<Self> {
|
||||
$table::table
|
||||
$(.filter($table::$col.eq($col)))+
|
||||
.limit(1)
|
||||
.load::<Self>(conn)
|
||||
.expect("Error loading $table by $col")
|
||||
|
|
|
@ -52,6 +52,8 @@ pub struct NewPost {
|
|||
impl Post {
|
||||
insert!(posts, NewPost);
|
||||
get!(posts);
|
||||
find_by!(posts, find_by_slug, slug as String);
|
||||
find_by!(posts, find_by_ap_url, ap_url as String);
|
||||
|
||||
pub fn count_local(conn: &PgConnection) -> usize {
|
||||
use schema::post_authors;
|
||||
|
@ -64,9 +66,6 @@ impl Post {
|
|||
.len()
|
||||
}
|
||||
|
||||
find_by!(posts, find_by_slug, slug as String);
|
||||
find_by!(posts, find_by_ap_url, ap_url as String);
|
||||
|
||||
pub fn get_recents(conn: &PgConnection, limit: i64) -> Vec<Post> {
|
||||
posts::table.order(posts::creation_date.desc())
|
||||
.limit(limit)
|
||||
|
|
|
@ -26,6 +26,8 @@ pub struct NewReshare {
|
|||
impl Reshare {
|
||||
insert!(reshares, NewReshare);
|
||||
get!(reshares);
|
||||
find_by!(reshares, find_by_ap_url, ap_url as String);
|
||||
find_by!(reshares, find_by_user_on_post, user_id as i32, post_id as i32);
|
||||
|
||||
pub fn update_ap_url(&self, conn: &PgConnection) {
|
||||
if self.ap_url.len() == 0 {
|
||||
|
@ -39,17 +41,6 @@ impl Reshare {
|
|||
}
|
||||
}
|
||||
|
||||
find_by!(reshares, find_by_ap_url, ap_url as String);
|
||||
|
||||
pub fn find_by_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option<Reshare> {
|
||||
reshares::table.filter(reshares::post_id.eq(post.id))
|
||||
.filter(reshares::user_id.eq(user.id))
|
||||
.limit(1)
|
||||
.load::<Reshare>(conn)
|
||||
.expect("Error loading reshare for user and post")
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
|
||||
pub fn get_recents_for_author(conn: &PgConnection, user: &User, limit: i64) -> Vec<Reshare> {
|
||||
reshares::table.filter(reshares::user_id.eq(user.id))
|
||||
.order(reshares::creation_date.desc())
|
||||
|
|
|
@ -115,15 +115,7 @@ impl User {
|
|||
}
|
||||
|
||||
find_by!(users, find_by_email, email as String);
|
||||
|
||||
pub fn find_by_name(conn: &PgConnection, username: String, instance_id: i32) -> Option<User> {
|
||||
users::table.filter(users::username.eq(username))
|
||||
.filter(users::instance_id.eq(instance_id))
|
||||
.limit(1)
|
||||
.load::<User>(conn)
|
||||
.expect("Error loading user by name")
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
find_by!(users, find_by_name, username as String, instance_id as i32);
|
||||
|
||||
pub fn find_local(conn: &PgConnection, username: String) -> Option<User> {
|
||||
User::find_by_name(conn, username, Instance::local_id(conn))
|
||||
|
|
|
@ -25,7 +25,7 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
|
|||
likes::Like::notify(&*conn, like.into_activity(&*conn), user.clone().into_id());
|
||||
broadcast(&*conn, &user, like.into_activity(&*conn), user.get_followers(&*conn));
|
||||
} else {
|
||||
let like = likes::Like::find_by_user_on_post(&*conn, &user, &post).unwrap();
|
||||
let like = likes::Like::find_by_user_on_post(&*conn, user.id, post.id).unwrap();
|
||||
let delete_act = like.delete(&*conn);
|
||||
broadcast(&*conn, &user, delete_act, user.get_followers(&*conn));
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
|
|||
Reshare::notify(&*conn, reshare.into_activity(&*conn), user.clone().into_id());
|
||||
broadcast(&*conn, &user, reshare.into_activity(&*conn), user.get_followers(&*conn));
|
||||
} else {
|
||||
let reshare = Reshare::find_by_user_on_post(&*conn, &user, &post).unwrap();
|
||||
let reshare = Reshare::find_by_user_on_post(&*conn, user.id, post.id).unwrap();
|
||||
let delete_act = reshare.delete(&*conn);
|
||||
broadcast(&*conn, &user, delete_act, user.get_followers(&*conn));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue