Introduce a find_by! macro to avoid some code duplication

This commit is contained in:
Bat 2018-06-18 14:37:49 +01:00
parent 36bf2e114c
commit 3c9210a0ed
7 changed files with 22 additions and 56 deletions

View File

@ -62,19 +62,8 @@ impl Comment {
.into_iter().nth(0) .into_iter().nth(0)
} }
pub fn find_by_post(conn: &PgConnection, post_id: i32) -> Vec<Comment> { find_by!(comments, find_by_post, post_id as i32);
comments::table.filter(comments::post_id.eq(post_id)) find_by!(comments, find_by_ap_url, ap_url as String);
.load::<Comment>(conn)
.expect("Error loading comment by post id")
}
pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Comment> {
comments::table.filter(comments::ap_url.eq(ap_url))
.limit(1)
.load::<Comment>(conn)
.expect("Error loading comment by AP URL")
.into_iter().nth(0)
}
pub fn get_author(&self, conn: &PgConnection) -> User { pub fn get_author(&self, conn: &PgConnection) -> User {
User::get(conn, self.author_id).unwrap() User::get(conn, self.author_id).unwrap()

View File

@ -63,13 +63,7 @@ impl Instance {
.into_iter().nth(0) .into_iter().nth(0)
} }
pub fn find_by_domain(conn: &PgConnection, domain: String) -> Option<Instance> { find_by!(instances, find_by_domain, public_domain as String);
instances::table.filter(instances::public_domain.eq(domain))
.limit(1)
.load::<Instance>(conn)
.expect("Couldn't load instance by domain")
.into_iter().nth(0)
}
pub fn block(&self) { pub fn block(&self) {
unimplemented!() unimplemented!()

View File

@ -58,13 +58,7 @@ impl Like {
.into_iter().nth(0) .into_iter().nth(0)
} }
pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Like> { find_by!(likes, find_by_ap_url, ap_url as String);
likes::table.filter(likes::ap_url.eq(ap_url))
.limit(1)
.load::<Like>(conn)
.expect("Error loading like by AP URL")
.into_iter().nth(0)
}
pub fn find_by_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option<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)) likes::table.filter(likes::post_id.eq(post.id))

View File

@ -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<Self> {
$table::table.filter($table::$col.eq(val))
.limit(1)
.load::<Self>(conn)
.expect("Error loading $table by $col")
.into_iter().nth(0)
}
};
}
pub mod blog_authors; pub mod blog_authors;
pub mod blogs; pub mod blogs;
pub mod comments; pub mod comments;

View File

@ -76,21 +76,8 @@ impl Post {
.len() .len()
} }
pub fn find_by_slug(conn: &PgConnection, slug: String) -> Option<Post> { find_by!(posts, find_by_slug, slug as String);
posts::table.filter(posts::slug.eq(slug)) find_by!(posts, find_by_ap_url, ap_url as String);
.limit(1)
.load::<Post>(conn)
.expect("Error loading post by slug")
.into_iter().nth(0)
}
pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Post> {
posts::table.filter(posts::ap_url.eq(ap_url))
.limit(1)
.load::<Post>(conn)
.expect("Error loading post by AP URL")
.into_iter().nth(0)
}
pub fn get_recents(conn: &PgConnection, limit: i64) -> Vec<Post> { pub fn get_recents(conn: &PgConnection, limit: i64) -> Vec<Post> {
posts::table.order(posts::creation_date.desc()) posts::table.order(posts::creation_date.desc())

View File

@ -51,13 +51,7 @@ impl Reshare {
} }
} }
pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Reshare> { find_by!(reshares, find_by_ap_url, ap_url as String);
reshares::table.filter(reshares::ap_url.eq(ap_url))
.limit(1)
.load::<Reshare>(conn)
.expect("Error loading reshare by AP URL")
.into_iter().nth(0)
}
pub fn find_by_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option<Reshare> { pub fn find_by_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option<Reshare> {
reshares::table.filter(reshares::post_id.eq(post.id)) reshares::table.filter(reshares::post_id.eq(post.id))

View File

@ -125,13 +125,7 @@ impl User {
.len() .len()
} }
pub fn find_by_email(conn: &PgConnection, email: String) -> Option<User> { find_by!(users, find_by_email, email as String);
users::table.filter(users::email.eq(email))
.limit(1)
.load::<User>(conn)
.expect("Error loading user by email")
.into_iter().nth(0)
}
pub fn find_by_name(conn: &PgConnection, username: String, instance_id: i32) -> Option<User> { pub fn find_by_name(conn: &PgConnection, username: String, instance_id: i32) -> Option<User> {
users::table.filter(users::username.eq(username)) users::table.filter(users::username.eq(username))