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")
}
pub fn get(conn: &PgConnection, id: i32) -> Option<BlogAuthor> {
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)
}
get!(blog_authors);
}

View File

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

View File

@ -54,13 +54,7 @@ impl Comment {
.expect("Error saving new comment")
}
pub fn get(conn: &PgConnection, id: i32) -> Option<Comment> {
comments::table.filter(comments::id.eq(id))
.limit(1)
.load::<Comment>(conn)
.expect("Error loading comment by id")
.into_iter().nth(0)
}
get!(comments);
find_by!(comments, find_by_post, post_id as i32);
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")
}
pub fn get(conn: &PgConnection, id: i32) -> Option<Follow> {
follows::table.filter(follows::id.eq(id))
.limit(1)
.load::<Follow>(conn)
.expect("Unable to load follow by id")
.into_iter().nth(0)
}
get!(follows);
pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + WithInbox + Actor>(
conn: &PgConnection,

View File

@ -55,13 +55,7 @@ impl Instance {
.expect("Error saving new instance")
}
pub fn get(conn: &PgConnection, id: i32) -> Option<Instance> {
instances::table.filter(instances::id.eq(id))
.limit(1)
.load::<Instance>(conn)
.expect("Error loading local instance infos")
.into_iter().nth(0)
}
get!(instances);
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> {
likes::table.filter(likes::id.eq(id))
.limit(1)
.load::<Like>(conn)
.expect("Error loading like by ID")
.into_iter().nth(0)
}
get!(likes);
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 blogs;
pub mod comments;

View File

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

View File

@ -30,11 +30,5 @@ impl PostAuthor {
.expect("Error saving new blog author")
}
pub fn get(conn: &PgConnection, id: i32) -> Option<PostAuthor> {
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)
}
get!(post_authors);
}

View File

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

View File

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

View File

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