Introduce a find_by! macro to avoid some code duplication
This commit is contained in:
parent
36bf2e114c
commit
3c9210a0ed
@ -62,19 +62,8 @@ impl Comment {
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
|
||||
pub fn find_by_post(conn: &PgConnection, post_id: i32) -> Vec<Comment> {
|
||||
comments::table.filter(comments::post_id.eq(post_id))
|
||||
.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)
|
||||
}
|
||||
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()
|
||||
|
@ -63,13 +63,7 @@ impl Instance {
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
|
||||
pub fn find_by_domain(conn: &PgConnection, domain: String) -> Option<Instance> {
|
||||
instances::table.filter(instances::public_domain.eq(domain))
|
||||
.limit(1)
|
||||
.load::<Instance>(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!()
|
||||
|
@ -58,13 +58,7 @@ impl Like {
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
|
||||
pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Like> {
|
||||
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)
|
||||
}
|
||||
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<Like> {
|
||||
likes::table.filter(likes::post_id.eq(post.id))
|
||||
|
@ -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 blogs;
|
||||
pub mod comments;
|
||||
|
@ -76,21 +76,8 @@ impl Post {
|
||||
.len()
|
||||
}
|
||||
|
||||
pub fn find_by_slug(conn: &PgConnection, slug: String) -> Option<Post> {
|
||||
posts::table.filter(posts::slug.eq(slug))
|
||||
.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)
|
||||
}
|
||||
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())
|
||||
|
@ -51,13 +51,7 @@ impl Reshare {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Reshare> {
|
||||
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)
|
||||
}
|
||||
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))
|
||||
|
@ -125,13 +125,7 @@ impl User {
|
||||
.len()
|
||||
}
|
||||
|
||||
pub fn find_by_email(conn: &PgConnection, email: String) -> Option<User> {
|
||||
users::table.filter(users::email.eq(email))
|
||||
.limit(1)
|
||||
.load::<User>(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<User> {
|
||||
users::table.filter(users::username.eq(username))
|
||||
|
Loading…
Reference in New Issue
Block a user