From 94af0b9a7d8e18b8fe91c228abf5b2cf0fbb6ae0 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 18 Jun 2018 14:44:23 +0100 Subject: [PATCH] Introduce a get! macro to avoid some code duplication --- src/models/blog_authors.rs | 8 +------- src/models/blogs.rs | 8 +------- src/models/comments.rs | 8 +------- src/models/follows.rs | 8 +------- src/models/instance.rs | 8 +------- src/models/likes.rs | 8 +------- src/models/mod.rs | 12 ++++++++++++ src/models/notifications.rs | 8 +------- src/models/post_authors.rs | 10 ++-------- src/models/posts.rs | 8 +------- src/models/reshares.rs | 8 +------- src/models/users.rs | 8 +------- 12 files changed, 24 insertions(+), 78 deletions(-) diff --git a/src/models/blog_authors.rs b/src/models/blog_authors.rs index 28fd3f4..04be669 100644 --- a/src/models/blog_authors.rs +++ b/src/models/blog_authors.rs @@ -26,11 +26,5 @@ impl BlogAuthor { .expect("Error saving new blog author") } - pub fn get(conn: &PgConnection, id: i32) -> Option { - blog_authors::table.filter(blog_authors::id.eq(id)) - .limit(1) - .load::(conn) - .expect("Error loading blog author by id") - .into_iter().nth(0) - } + get!(blog_authors); } diff --git a/src/models/blogs.rs b/src/models/blogs.rs index cf8bd07..ce253d3 100644 --- a/src/models/blogs.rs +++ b/src/models/blogs.rs @@ -63,13 +63,7 @@ impl Blog { .expect("Error saving new blog") } - pub fn get(conn: &PgConnection, id: i32) -> Option { - blogs::table.filter(blogs::id.eq(id)) - .limit(1) - .load::(conn) - .expect("Error loading blog by id") - .into_iter().nth(0) - } + get!(blogs); pub fn find_for_author(conn: &PgConnection, author_id: i32) -> Vec { use schema::blog_authors; diff --git a/src/models/comments.rs b/src/models/comments.rs index 116779a..be75b23 100644 --- a/src/models/comments.rs +++ b/src/models/comments.rs @@ -54,13 +54,7 @@ impl Comment { .expect("Error saving new comment") } - pub fn get(conn: &PgConnection, id: i32) -> Option { - comments::table.filter(comments::id.eq(id)) - .limit(1) - .load::(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); diff --git a/src/models/follows.rs b/src/models/follows.rs index 6ba9385..111cc6e 100644 --- a/src/models/follows.rs +++ b/src/models/follows.rs @@ -32,13 +32,7 @@ impl Follow { .expect("Unable to insert new follow") } - pub fn get(conn: &PgConnection, id: i32) -> Option { - follows::table.filter(follows::id.eq(id)) - .limit(1) - .load::(conn) - .expect("Unable to load follow by id") - .into_iter().nth(0) - } + get!(follows); pub fn accept_follow( conn: &PgConnection, diff --git a/src/models/instance.rs b/src/models/instance.rs index f058988..ac7ad9d 100644 --- a/src/models/instance.rs +++ b/src/models/instance.rs @@ -55,13 +55,7 @@ impl Instance { .expect("Error saving new instance") } - pub fn get(conn: &PgConnection, id: i32) -> Option { - instances::table.filter(instances::id.eq(id)) - .limit(1) - .load::(conn) - .expect("Error loading local instance infos") - .into_iter().nth(0) - } + get!(instances); find_by!(instances, find_by_domain, public_domain as String); diff --git a/src/models/likes.rs b/src/models/likes.rs index 4dcafee..e1ad3c1 100644 --- a/src/models/likes.rs +++ b/src/models/likes.rs @@ -50,13 +50,7 @@ impl Like { } } - pub fn get(conn: &PgConnection, id: i32) -> Option { - likes::table.filter(likes::id.eq(id)) - .limit(1) - .load::(conn) - .expect("Error loading like by ID") - .into_iter().nth(0) - } + get!(likes); find_by!(likes, find_by_ap_url, ap_url as String); diff --git a/src/models/mod.rs b/src/models/mod.rs index 455e3d3..1bab1a7 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -12,6 +12,18 @@ macro_rules! find_by { }; } +macro_rules! get { + ($table:ident) => { + pub fn get(conn: &PgConnection, id: i32) -> Option { + $table::table.filter($table::id.eq(id)) + .limit(1) + .load::(conn) + .expect("Error loading $table by id") + .into_iter().nth(0) + } + }; +} + pub mod blog_authors; pub mod blogs; pub mod comments; diff --git a/src/models/notifications.rs b/src/models/notifications.rs index e0a9acc..6857e1f 100644 --- a/src/models/notifications.rs +++ b/src/models/notifications.rs @@ -33,13 +33,7 @@ impl Notification { .expect("Couldn't save notification") } - pub fn get(conn: &PgConnection, id: i32) -> Option { - notifications::table.filter(notifications::id.eq(id)) - .limit(1) - .load::(conn) - .expect("Couldn't load notification by ID") - .into_iter().nth(0) - } + get!(notifications); pub fn find_for_user(conn: &PgConnection, user: &User) -> Vec { notifications::table.filter(notifications::user_id.eq(user.id)) diff --git a/src/models/post_authors.rs b/src/models/post_authors.rs index e5849c6..4806f02 100644 --- a/src/models/post_authors.rs +++ b/src/models/post_authors.rs @@ -23,18 +23,12 @@ pub struct NewPostAuthor { } impl PostAuthor { - pub fn insert (conn: &PgConnection, new: NewPostAuthor) -> PostAuthor { + pub fn insert(conn: &PgConnection, new: NewPostAuthor) -> PostAuthor { diesel::insert_into(post_authors::table) .values(new) .get_result(conn) .expect("Error saving new blog author") } - pub fn get(conn: &PgConnection, id: i32) -> Option { - post_authors::table.filter(post_authors::id.eq(id)) - .limit(1) - .load::(conn) - .expect("Error loading blog author by id") - .into_iter().nth(0) - } + get!(post_authors); } diff --git a/src/models/posts.rs b/src/models/posts.rs index bd85e23..dafbbbd 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -57,13 +57,7 @@ impl Post { .expect("Error saving new post") } - pub fn get(conn: &PgConnection, id: i32) -> Option { - posts::table.filter(posts::id.eq(id)) - .limit(1) - .load::(conn) - .expect("Error loading post by id") - .into_iter().nth(0) - } + get!(posts); pub fn count_local(conn: &PgConnection) -> usize { use schema::post_authors; diff --git a/src/models/reshares.rs b/src/models/reshares.rs index 2b11cc3..f694170 100644 --- a/src/models/reshares.rs +++ b/src/models/reshares.rs @@ -31,13 +31,7 @@ impl Reshare { .expect("Couldn't save reshare") } - pub fn get(conn: &PgConnection, id: i32) -> Option { - reshares::table.filter(reshares::id.eq(id)) - .limit(1) - .load::(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 { diff --git a/src/models/users.rs b/src/models/users.rs index 259f03d..2b7708d 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -110,13 +110,7 @@ impl User { .into_iter().nth(0).unwrap() } - pub fn get(conn: &PgConnection, id: i32) -> Option { - users::table.filter(users::id.eq(id)) - .limit(1) - .load::(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)))