From c19ace7e6a0350a1f257018166769957568fabe2 Mon Sep 17 00:00:00 2001 From: Lyra Date: Wed, 11 Dec 2019 00:10:39 +0100 Subject: [PATCH] Introduce the "MaybeOptional" trait. This trait is a generic type that allows us to get an Option from any type and from another option. It can be used to remove the ugly "do_something", "do_something_optional" that we previously had. --- server/src/api/community.rs | 6 +-- server/src/api/post.rs | 10 ++-- server/src/api/site.rs | 42 ++++++++-------- server/src/api/user.rs | 22 ++++----- server/src/db/comment_view.rs | 72 +++++++--------------------- server/src/db/community_view.rs | 38 ++++----------- server/src/db/mod.rs | 16 +++++++ server/src/db/post_view.rs | 77 ++++++++---------------------- server/src/db/user_mention_view.rs | 18 ++----- server/src/db/user_view.rs | 29 +++-------- 10 files changed, 112 insertions(+), 218 deletions(-) diff --git a/server/src/api/community.rs b/server/src/api/community.rs index 9492d75d3..5c97f0886 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -351,10 +351,10 @@ impl Perform for Oper { let communities = CommunityQueryBuilder::create(&conn) .sort(&sort) - .from_user_id_optional(user_id) + .from_user_id(user_id) .show_nsfw(show_nsfw) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; // Return the jwt diff --git a/server/src/api/post.rs b/server/src/api/post.rs index 4855865b0..0b54840f4 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -180,7 +180,7 @@ impl Perform for Oper { let comments = CommentQueryBuilder::create(&conn) .for_post_id(data.id) - .my_user_id_optional(user_id) + .my_user_id(user_id) .limit(9999) .list()?; @@ -236,10 +236,10 @@ impl Perform for Oper { .listing_type(type_) .sort(&sort) .show_nsfw(show_nsfw) - .for_community_id_optional(data.community_id) - .my_user_id_optional(user_id) - .page_optional(data.page) - .limit_optional(data.limit) + .for_community_id(data.community_id) + .my_user_id(user_id) + .page(data.page) + .limit(data.limit) .list() { Ok(posts) => posts, diff --git a/server/src/api/site.rs b/server/src/api/site.rs index 261114676..cb6edfd58 100644 --- a/server/src/api/site.rs +++ b/server/src/api/site.rs @@ -322,75 +322,75 @@ impl Perform for Oper { posts = PostQueryBuilder::create(&conn) .sort(&sort) .show_nsfw(true) - .for_community_id_optional(data.community_id) + .for_community_id(data.community_id) .search_term(data.q.to_owned()) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; } SearchType::Comments => { comments = CommentQueryBuilder::create(&conn) .sort(&sort) .search_term(data.q.to_owned()) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; } SearchType::Communities => { communities = CommunityQueryBuilder::create(&conn) .sort(&sort) .search_term(data.q.to_owned()) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; } SearchType::Users => { users = UserQueryBuilder::create(&conn) .sort(&sort) .search_term(data.q.to_owned()) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; } SearchType::All => { posts = PostQueryBuilder::create(&conn) .sort(&sort) .show_nsfw(true) - .for_community_id_optional(data.community_id) + .for_community_id(data.community_id) .search_term(data.q.to_owned()) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; comments = CommentQueryBuilder::create(&conn) .sort(&sort) .search_term(data.q.to_owned()) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; communities = CommunityQueryBuilder::create(&conn) .sort(&sort) .search_term(data.q.to_owned()) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; users = UserQueryBuilder::create(&conn) .sort(&sort) .search_term(data.q.to_owned()) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; } SearchType::Url => { posts = PostQueryBuilder::create(&conn) .sort(&sort) .show_nsfw(true) - .for_community_id_optional(data.community_id) + .for_community_id(data.community_id) .url_search(data.q.to_owned()) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; } }; diff --git a/server/src/api/user.rs b/server/src/api/user.rs index ebbe48a2f..3047a0d3f 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -373,17 +373,17 @@ impl Perform for Oper { .sort(&sort) .show_nsfw(show_nsfw) .saved_only(data.saved_only) - .for_community_id_optional(data.community_id) - .my_user_id_optional(user_id) - .page_optional(data.page) - .limit_optional(data.limit); + .for_community_id(data.community_id) + .my_user_id(user_id) + .page(data.page) + .limit(data.limit); let mut comments_query = CommentQueryBuilder::create(&conn) .sort(&sort) .saved_only(data.saved_only) - .my_user_id_optional(user_id) - .page_optional(data.page) - .limit_optional(data.limit); + .my_user_id(user_id) + .page(data.page) + .limit(data.limit); // If its saved only, you don't care what creator it was // Or, if its not saved, then you only want it for that specific creator @@ -561,8 +561,8 @@ impl Perform for Oper { let replies = ReplyQueryBuilder::create(&conn, user_id) .sort(&sort) .unread_only(data.unread_only) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; Ok(GetRepliesResponse { @@ -589,8 +589,8 @@ impl Perform for Oper { let mentions = UserMentionQueryBuilder::create(&conn, user_id) .sort(&sort) .unread_only(data.unread_only) - .page_optional(data.page) - .limit_optional(data.limit) + .page(data.page) + .limit(data.limit) .list()?; Ok(GetUserMentionsResponse { diff --git a/server/src/db/comment_view.rs b/server/src/db/comment_view.rs index f9c00b209..5c321e2e2 100644 --- a/server/src/db/comment_view.rs +++ b/server/src/db/comment_view.rs @@ -92,43 +92,23 @@ impl<'a> CommentQueryBuilder<'a> { self } - pub fn for_post_id(mut self, for_post_id: i32) -> Self { - self.for_post_id = Some(for_post_id); + pub fn for_post_id>(mut self, for_post_id: T) -> Self { + self.for_post_id = for_post_id.get_optional(); self } - pub fn for_post_id_optional(mut self, for_post_id: Option) -> Self { - self.for_post_id = for_post_id; + pub fn for_creator_id>(mut self, for_creator_id: T) -> Self { + self.for_creator_id = for_creator_id.get_optional(); self } - pub fn for_creator_id(mut self, for_creator_id: i32) -> Self { - self.for_creator_id = Some(for_creator_id); + pub fn search_term>(mut self, search_term: T) -> Self { + self.search_term = search_term.get_optional(); self } - pub fn for_creator_id_optional(mut self, for_creator_id: Option) -> Self { - self.for_creator_id = for_creator_id; - self - } - - pub fn search_term(mut self, search_term: String) -> Self { - self.search_term = Some(search_term); - self - } - - pub fn search_term_optional(mut self, search_term: Option) -> Self { - self.search_term = search_term; - self - } - - pub fn my_user_id(mut self, my_user_id: i32) -> Self { - self.my_user_id = Some(my_user_id); - self - } - - pub fn my_user_id_optional(mut self, my_user_id: Option) -> Self { - self.my_user_id = my_user_id; + pub fn my_user_id>(mut self, my_user_id: T) -> Self { + self.my_user_id = my_user_id.get_optional(); self } @@ -137,23 +117,13 @@ impl<'a> CommentQueryBuilder<'a> { self } - pub fn page(mut self, page: i64) -> Self { - self.page = Some(page); + pub fn page>(mut self, page: T) -> Self { + self.page = page.get_optional(); self } - pub fn page_optional(mut self, page: Option) -> Self { - self.page = page; - self - } - - pub fn limit(mut self, limit: i64) -> Self { - self.limit = Some(limit); - self - } - - pub fn limit_optional(mut self, limit: Option) -> Self { - self.limit = limit; + pub fn limit>(mut self, limit: T) -> Self { + self.limit = limit.get_optional(); self } @@ -331,23 +301,13 @@ impl<'a> ReplyQueryBuilder<'a> { self } - pub fn page(mut self, page: i64) -> Self { - self.page = Some(page); + pub fn page>(mut self, page: T) -> Self { + self.page = page.get_optional(); self } - pub fn page_optional(mut self, page: Option) -> Self { - self.page = page; - self - } - - pub fn limit(mut self, limit: i64) -> Self { - self.limit = Some(limit); - self - } - - pub fn limit_optional(mut self, limit: Option) -> Self { - self.limit = limit; + pub fn limit>(mut self, limit: T) -> Self { + self.limit = limit.get_optional(); self } diff --git a/server/src/db/community_view.rs b/server/src/db/community_view.rs index c90beaba5..04b68441a 100644 --- a/server/src/db/community_view.rs +++ b/server/src/db/community_view.rs @@ -135,50 +135,28 @@ impl<'a> CommunityQueryBuilder<'a> { self } - pub fn from_user_id(mut self, from_user_id: i32) -> Self { - self.from_user_id = Some(from_user_id); + pub fn from_user_id>(mut self, from_user_id: T) -> Self { + self.from_user_id = from_user_id.get_optional(); self } - pub fn from_user_id_optional(self, from_user_id: Option) -> Self { - match from_user_id { - Some(from_user_id) => self.from_user_id(from_user_id), - None => self, - } - } - pub fn show_nsfw(mut self, show_nsfw: bool) -> Self { self.show_nsfw = show_nsfw; self } - pub fn search_term(mut self, search_term: String) -> Self { - self.search_term = Some(search_term); + pub fn search_term>(mut self, search_term: T) -> Self { + self.search_term = search_term.get_optional(); self } - pub fn search_term_optional(mut self, search_term: Option) -> Self { - self.search_term = search_term; + pub fn page>(mut self, page: T) -> Self { + self.page = page.get_optional(); self } - pub fn page(mut self, page: i64) -> Self { - self.page = Some(page); - self - } - - pub fn page_optional(mut self, page: Option) -> Self { - self.page = page; - self - } - - pub fn limit(mut self, limit: i64) -> Self { - self.limit = Some(limit); - self - } - - pub fn limit_optional(mut self, limit: Option) -> Self { - self.limit = limit; + pub fn limit>(mut self, limit: T) -> Self { + self.limit = limit.get_optional(); self } diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs index 09640e22c..3501fcda6 100644 --- a/server/src/db/mod.rs +++ b/server/src/db/mod.rs @@ -91,6 +91,22 @@ pub trait Readable { Self: Sized; } +pub trait MaybeOptional { + fn get_optional(self) -> Option; +} + +impl MaybeOptional for T { + fn get_optional(self) -> Option { + return Some(self); + } +} + +impl MaybeOptional for Option { + fn get_optional(self) -> Option { + return self; + } +} + pub fn establish_connection() -> PgConnection { let db_url = Settings::get().db_url; PgConnection::establish(&db_url).expect(&format!("Error connecting to {}", db_url)) diff --git a/server/src/db/post_view.rs b/server/src/db/post_view.rs index 4fcd8e47e..615b9b0db 100644 --- a/server/src/db/post_view.rs +++ b/server/src/db/post_view.rs @@ -120,65 +120,40 @@ impl<'a> PostQueryBuilder<'a> { self } - pub fn for_community_id(mut self, for_community_id: i32) -> Self { + pub fn for_community_id>(mut self, for_community_id: T) -> Self { use super::post_view::post_view::dsl::*; - self.query = self.query.filter(community_id.eq(for_community_id)); - self.query = self.query.then_order_by(stickied.desc()); + if let Some(for_community_id) = for_community_id.get_optional() { + self.query = self.query.filter(community_id.eq(for_community_id)); + self.query = self.query.then_order_by(stickied.desc()); + } self } - pub fn for_community_id_optional(self, for_community_id: Option) -> Self { - match for_community_id { - Some(for_community_id) => self.for_community_id(for_community_id), - None => self, + pub fn for_creator_id>(mut self, for_creator_id: T) -> Self { + if let Some(for_creator_id) = for_creator_id.get_optional() { + self.for_creator_id = Some(for_creator_id); } - } - - pub fn for_creator_id(mut self, for_creator_id: i32) -> Self { - self.for_creator_id = Some(for_creator_id); self } - pub fn for_creator_id_optional(self, for_creator_id: Option) -> Self { - match for_creator_id { - Some(for_creator_id) => self.for_creator_id(for_creator_id), - None => self, - } - } - - pub fn search_term(mut self, search_term: String) -> Self { + pub fn search_term>(mut self, search_term: T) -> Self { use super::post_view::post_view::dsl::*; - self.query = self.query.filter(name.ilike(fuzzy_search(&search_term))); + if let Some(search_term) = search_term.get_optional() { + self.query = self.query.filter(name.ilike(fuzzy_search(&search_term))); + } self } - pub fn search_term_optional(self, search_term: Option) -> Self { - match search_term { - Some(search_term) => self.search_term(search_term), - None => self, - } - } - - pub fn url_search(mut self, url_search: String) -> Self { + pub fn url_search>(mut self, url_search: T) -> Self { use super::post_view::post_view::dsl::*; - self.query = self.query.filter(url.eq(url_search)); - self - } - - pub fn url_search_optional(self, url_search: Option) -> Self { - match url_search { - Some(url_search) => self.url_search(url_search), - None => self, + if let Some(url_search) = url_search.get_optional() { + self.query = self.query.filter(url.eq(url_search)); } - } - - pub fn my_user_id(mut self, my_user_id: i32) -> Self { - self.my_user_id = Some(my_user_id); self } - pub fn my_user_id_optional(mut self, my_user_id: Option) -> Self { - self.my_user_id = my_user_id; + pub fn my_user_id>(mut self, my_user_id: T) -> Self { + self.my_user_id = my_user_id.get_optional(); self } @@ -197,23 +172,13 @@ impl<'a> PostQueryBuilder<'a> { self } - pub fn page(mut self, page: i64) -> Self { - self.page = Some(page); + pub fn page>(mut self, page: T) -> Self { + self.page = page.get_optional(); self } - pub fn page_optional(mut self, page: Option) -> Self { - self.page = page; - self - } - - pub fn limit(mut self, limit: i64) -> Self { - self.limit = Some(limit); - self - } - - pub fn limit_optional(mut self, limit: Option) -> Self { - self.limit = limit; + pub fn limit>(mut self, limit: T) -> Self { + self.limit = limit.get_optional(); self } diff --git a/server/src/db/user_mention_view.rs b/server/src/db/user_mention_view.rs index 19b803da8..45541861e 100644 --- a/server/src/db/user_mention_view.rs +++ b/server/src/db/user_mention_view.rs @@ -96,23 +96,13 @@ impl<'a> UserMentionQueryBuilder<'a> { self } - pub fn page(mut self, page: i64) -> Self { - self.page = Some(page); + pub fn page>(mut self, page: T) -> Self { + self.page = page.get_optional(); self } - pub fn page_optional(mut self, page: Option) -> Self { - self.page = page; - self - } - - pub fn limit(mut self, limit: i64) -> Self { - self.limit = Some(limit); - self - } - - pub fn limit_optional(mut self, limit: Option) -> Self { - self.limit = limit; + pub fn limit>(mut self, limit: T) -> Self { + self.limit = limit.get_optional(); self } diff --git a/server/src/db/user_view.rs b/server/src/db/user_view.rs index 0637d647e..0ed95eefd 100644 --- a/server/src/db/user_view.rs +++ b/server/src/db/user_view.rs @@ -62,36 +62,21 @@ impl<'a> UserQueryBuilder<'a> { self } - pub fn search_term(mut self, search_term: String) -> Self { + pub fn search_term>(mut self, search_term: T) -> Self { use super::user_view::user_view::dsl::*; - self.query = self.query.filter(name.ilike(fuzzy_search(&search_term))); - self - } - - pub fn search_term_optional(self, search_term: Option) -> Self { - match search_term { - Some(search_term) => self.search_term(search_term), - None => self, + if let Some(search_term) = search_term.get_optional() { + self.query = self.query.filter(name.ilike(fuzzy_search(&search_term))); } - } - - pub fn page(mut self, page: i64) -> Self { - self.page = Some(page); self } - pub fn page_optional(mut self, page: Option) -> Self { - self.page = page; + pub fn page>(mut self, page: T) -> Self { + self.page = page.get_optional(); self } - pub fn limit(mut self, limit: i64) -> Self { - self.limit = Some(limit); - self - } - - pub fn limit_optional(mut self, limit: Option) -> Self { - self.limit = limit; + pub fn limit>(mut self, limit: T) -> Self { + self.limit = limit.get_optional(); self }