Introduce a get! macro to avoid some code duplication

This commit is contained in:
Bat 2018-06-18 14:44:23 +01:00
parent 3c9210a0ed
commit 94af0b9a7d
12 changed files with 24 additions and 78 deletions

View File

@ -26,11 +26,5 @@ impl BlogAuthor {
.expect("Error saving new blog author") .expect("Error saving new blog author")
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<BlogAuthor> { get!(blog_authors);
blog_authors::table.filter(blog_authors::id.eq(id))
.limit(1)
.load::<BlogAuthor>(conn)
.expect("Error loading blog author by id")
.into_iter().nth(0)
}
} }

View File

@ -63,13 +63,7 @@ impl Blog {
.expect("Error saving new blog") .expect("Error saving new blog")
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<Blog> { get!(blogs);
blogs::table.filter(blogs::id.eq(id))
.limit(1)
.load::<Blog>(conn)
.expect("Error loading blog by id")
.into_iter().nth(0)
}
pub fn find_for_author(conn: &PgConnection, author_id: i32) -> Vec<Blog> { pub fn find_for_author(conn: &PgConnection, author_id: i32) -> Vec<Blog> {
use schema::blog_authors; use schema::blog_authors;

View File

@ -54,13 +54,7 @@ impl Comment {
.expect("Error saving new comment") .expect("Error saving new comment")
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<Comment> { get!(comments);
comments::table.filter(comments::id.eq(id))
.limit(1)
.load::<Comment>(conn)
.expect("Error loading comment by id")
.into_iter().nth(0)
}
find_by!(comments, find_by_post, post_id as i32); find_by!(comments, find_by_post, post_id as i32);
find_by!(comments, find_by_ap_url, ap_url as String); find_by!(comments, find_by_ap_url, ap_url as String);

View File

@ -32,13 +32,7 @@ impl Follow {
.expect("Unable to insert new follow") .expect("Unable to insert new follow")
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<Follow> { get!(follows);
follows::table.filter(follows::id.eq(id))
.limit(1)
.load::<Follow>(conn)
.expect("Unable to load follow by id")
.into_iter().nth(0)
}
pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + WithInbox + Actor>( pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + WithInbox + Actor>(
conn: &PgConnection, conn: &PgConnection,

View File

@ -55,13 +55,7 @@ impl Instance {
.expect("Error saving new instance") .expect("Error saving new instance")
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<Instance> { get!(instances);
instances::table.filter(instances::id.eq(id))
.limit(1)
.load::<Instance>(conn)
.expect("Error loading local instance infos")
.into_iter().nth(0)
}
find_by!(instances, find_by_domain, public_domain as String); find_by!(instances, find_by_domain, public_domain as String);

View File

@ -50,13 +50,7 @@ impl Like {
} }
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<Like> { get!(likes);
likes::table.filter(likes::id.eq(id))
.limit(1)
.load::<Like>(conn)
.expect("Error loading like by ID")
.into_iter().nth(0)
}
find_by!(likes, find_by_ap_url, ap_url as String); find_by!(likes, find_by_ap_url, ap_url as String);

View File

@ -12,6 +12,18 @@ macro_rules! find_by {
}; };
} }
macro_rules! get {
($table:ident) => {
pub fn get(conn: &PgConnection, id: i32) -> Option<Self> {
$table::table.filter($table::id.eq(id))
.limit(1)
.load::<Self>(conn)
.expect("Error loading $table by id")
.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

@ -33,13 +33,7 @@ impl Notification {
.expect("Couldn't save notification") .expect("Couldn't save notification")
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<Notification> { get!(notifications);
notifications::table.filter(notifications::id.eq(id))
.limit(1)
.load::<Notification>(conn)
.expect("Couldn't load notification by ID")
.into_iter().nth(0)
}
pub fn find_for_user(conn: &PgConnection, user: &User) -> Vec<Notification> { pub fn find_for_user(conn: &PgConnection, user: &User) -> Vec<Notification> {
notifications::table.filter(notifications::user_id.eq(user.id)) notifications::table.filter(notifications::user_id.eq(user.id))

View File

@ -23,18 +23,12 @@ pub struct NewPostAuthor {
} }
impl PostAuthor { impl PostAuthor {
pub fn insert (conn: &PgConnection, new: NewPostAuthor) -> PostAuthor { pub fn insert(conn: &PgConnection, new: NewPostAuthor) -> PostAuthor {
diesel::insert_into(post_authors::table) diesel::insert_into(post_authors::table)
.values(new) .values(new)
.get_result(conn) .get_result(conn)
.expect("Error saving new blog author") .expect("Error saving new blog author")
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<PostAuthor> { get!(post_authors);
post_authors::table.filter(post_authors::id.eq(id))
.limit(1)
.load::<PostAuthor>(conn)
.expect("Error loading blog author by id")
.into_iter().nth(0)
}
} }

View File

@ -57,13 +57,7 @@ impl Post {
.expect("Error saving new post") .expect("Error saving new post")
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<Post> { get!(posts);
posts::table.filter(posts::id.eq(id))
.limit(1)
.load::<Post>(conn)
.expect("Error loading post by id")
.into_iter().nth(0)
}
pub fn count_local(conn: &PgConnection) -> usize { pub fn count_local(conn: &PgConnection) -> usize {
use schema::post_authors; use schema::post_authors;

View File

@ -31,13 +31,7 @@ impl Reshare {
.expect("Couldn't save reshare") .expect("Couldn't save reshare")
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<Reshare> { get!(reshares);
reshares::table.filter(reshares::id.eq(id))
.limit(1)
.load::<Reshare>(conn)
.expect("Could'nt load reshare")
.into_iter().nth(0)
}
pub fn update_ap_url(&self, conn: &PgConnection) { pub fn update_ap_url(&self, conn: &PgConnection) {
if self.ap_url.len() == 0 { if self.ap_url.len() == 0 {

View File

@ -110,13 +110,7 @@ impl User {
.into_iter().nth(0).unwrap() .into_iter().nth(0).unwrap()
} }
pub fn get(conn: &PgConnection, id: i32) -> Option<User> { get!(users);
users::table.filter(users::id.eq(id))
.limit(1)
.load::<User>(conn)
.expect("Error loading user by id")
.into_iter().nth(0)
}
pub fn count_local(conn: &PgConnection) -> usize { pub fn count_local(conn: &PgConnection) -> usize {
users::table.filter(users::instance_id.eq(Instance::local_id(conn))) users::table.filter(users::instance_id.eq(Instance::local_id(conn)))