From fa2435e725d346f4d189fe413732e15a32173604 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 18 Jun 2018 16:13:09 +0100 Subject: [PATCH] Improve the find_by! macro to allow multiple columns --- src/models/blogs.rs | 9 +-------- src/models/comments.rs | 1 - src/models/likes.rs | 10 +--------- src/models/mod.rs | 8 ++++---- src/models/posts.rs | 5 ++--- src/models/reshares.rs | 13 ++----------- src/models/users.rs | 10 +--------- src/routes/likes.rs | 2 +- src/routes/reshares.rs | 2 +- 9 files changed, 13 insertions(+), 47 deletions(-) diff --git a/src/models/blogs.rs b/src/models/blogs.rs index 71e7bee3..ee1964d3 100644 --- a/src/models/blogs.rs +++ b/src/models/blogs.rs @@ -67,14 +67,7 @@ impl Blog { .expect("Couldn't load blogs ") } - pub fn find_by_name(conn: &PgConnection, name: String, instance_id: i32) -> Option { - blogs::table.filter(blogs::actor_id.eq(name)) - .filter(blogs::instance_id.eq(instance_id)) - .limit(1) - .load::(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::find_by_name(conn, name, Instance::local_id(conn)) diff --git a/src/models/comments.rs b/src/models/comments.rs index a24ac40e..e4788925 100644 --- a/src/models/comments.rs +++ b/src/models/comments.rs @@ -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); diff --git a/src/models/likes.rs b/src/models/likes.rs index 537a4578..58aba083 100644 --- a/src/models/likes.rs +++ b/src/models/likes.rs @@ -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 { - 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) -> activity::Undo { diesel::delete(self).execute(conn).unwrap(); diff --git a/src/models/mod.rs b/src/models/mod.rs index e4bc0d75..d0f01f3c 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -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 { - $table::table.filter($table::$col.eq(val)) + pub fn $fn(conn: &PgConnection, $($col: $type),+) -> Option { + $table::table + $(.filter($table::$col.eq($col)))+ .limit(1) .load::(conn) .expect("Error loading $table by $col") diff --git a/src/models/posts.rs b/src/models/posts.rs index 67068bd0..6b71928b 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -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 { posts::table.order(posts::creation_date.desc()) .limit(limit) diff --git a/src/models/reshares.rs b/src/models/reshares.rs index 6a4b2407..86ead790 100644 --- a/src/models/reshares.rs +++ b/src/models/reshares.rs @@ -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 { - reshares::table.filter(reshares::post_id.eq(post.id)) - .filter(reshares::user_id.eq(user.id)) - .limit(1) - .load::(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 { reshares::table.filter(reshares::user_id.eq(user.id)) .order(reshares::creation_date.desc()) diff --git a/src/models/users.rs b/src/models/users.rs index 2a956fac..88bf979a 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -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 { - users::table.filter(users::username.eq(username)) - .filter(users::instance_id.eq(instance_id)) - .limit(1) - .load::(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::find_by_name(conn, username, Instance::local_id(conn)) diff --git a/src/routes/likes.rs b/src/routes/likes.rs index fb12855d..57bae6f6 100644 --- a/src/routes/likes.rs +++ b/src/routes/likes.rs @@ -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)); } diff --git a/src/routes/reshares.rs b/src/routes/reshares.rs index cabe22ee..5365af48 100644 --- a/src/routes/reshares.rs +++ b/src/routes/reshares.rs @@ -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)); }