From 3c9210a0ed88028ebef2ac36e1b650497fffc176 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 18 Jun 2018 14:37:49 +0100 Subject: [PATCH] Introduce a find_by! macro to avoid some code duplication --- src/models/comments.rs | 15 ++------------- src/models/instance.rs | 8 +------- src/models/likes.rs | 8 +------- src/models/mod.rs | 14 ++++++++++++++ src/models/posts.rs | 17 ++--------------- src/models/reshares.rs | 8 +------- src/models/users.rs | 8 +------- 7 files changed, 22 insertions(+), 56 deletions(-) diff --git a/src/models/comments.rs b/src/models/comments.rs index 75df9df0..116779af 100644 --- a/src/models/comments.rs +++ b/src/models/comments.rs @@ -62,19 +62,8 @@ impl Comment { .into_iter().nth(0) } - pub fn find_by_post(conn: &PgConnection, post_id: i32) -> Vec { - comments::table.filter(comments::post_id.eq(post_id)) - .load::(conn) - .expect("Error loading comment by post id") - } - - pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option { - comments::table.filter(comments::ap_url.eq(ap_url)) - .limit(1) - .load::(conn) - .expect("Error loading comment by AP URL") - .into_iter().nth(0) - } + find_by!(comments, find_by_post, post_id as i32); + find_by!(comments, find_by_ap_url, ap_url as String); pub fn get_author(&self, conn: &PgConnection) -> User { User::get(conn, self.author_id).unwrap() diff --git a/src/models/instance.rs b/src/models/instance.rs index c4861ee1..f058988b 100644 --- a/src/models/instance.rs +++ b/src/models/instance.rs @@ -63,13 +63,7 @@ impl Instance { .into_iter().nth(0) } - pub fn find_by_domain(conn: &PgConnection, domain: String) -> Option { - instances::table.filter(instances::public_domain.eq(domain)) - .limit(1) - .load::(conn) - .expect("Couldn't load instance by domain") - .into_iter().nth(0) - } + find_by!(instances, find_by_domain, public_domain as String); pub fn block(&self) { unimplemented!() diff --git a/src/models/likes.rs b/src/models/likes.rs index 2462118a..4dcafeeb 100644 --- a/src/models/likes.rs +++ b/src/models/likes.rs @@ -58,13 +58,7 @@ impl Like { .into_iter().nth(0) } - pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option { - likes::table.filter(likes::ap_url.eq(ap_url)) - .limit(1) - .load::(conn) - .expect("Error loading like by AP URL") - .into_iter().nth(0) - } + find_by!(likes, find_by_ap_url, ap_url as String); pub fn find_by_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option { likes::table.filter(likes::post_id.eq(post.id)) diff --git a/src/models/mod.rs b/src/models/mod.rs index a7b1b87b..455e3d3b 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,3 +1,17 @@ +// TODO: support multiple columns (see Like::find_by_user_on_post) +macro_rules! find_by { + ($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)) + .limit(1) + .load::(conn) + .expect("Error loading $table by $col") + .into_iter().nth(0) + } + }; +} + pub mod blog_authors; pub mod blogs; pub mod comments; diff --git a/src/models/posts.rs b/src/models/posts.rs index b17a2340..bd85e235 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -76,21 +76,8 @@ impl Post { .len() } - pub fn find_by_slug(conn: &PgConnection, slug: String) -> Option { - posts::table.filter(posts::slug.eq(slug)) - .limit(1) - .load::(conn) - .expect("Error loading post by slug") - .into_iter().nth(0) - } - - pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option { - posts::table.filter(posts::ap_url.eq(ap_url)) - .limit(1) - .load::(conn) - .expect("Error loading post by AP URL") - .into_iter().nth(0) - } + 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()) diff --git a/src/models/reshares.rs b/src/models/reshares.rs index 75537672..2b11cc37 100644 --- a/src/models/reshares.rs +++ b/src/models/reshares.rs @@ -51,13 +51,7 @@ impl Reshare { } } - pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option { - reshares::table.filter(reshares::ap_url.eq(ap_url)) - .limit(1) - .load::(conn) - .expect("Error loading reshare by AP URL") - .into_iter().nth(0) - } + 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)) diff --git a/src/models/users.rs b/src/models/users.rs index 78846ac8..259f03d6 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -125,13 +125,7 @@ impl User { .len() } - pub fn find_by_email(conn: &PgConnection, email: String) -> Option { - users::table.filter(users::email.eq(email)) - .limit(1) - .load::(conn) - .expect("Error loading user by email") - .into_iter().nth(0) - } + 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))