Merge branch 'teromene-maybeoptional'

This commit is contained in:
Dessalines 2019-12-10 22:59:04 -08:00
commit 50c953be61
10 changed files with 112 additions and 218 deletions

View File

@ -351,10 +351,10 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
let communities = CommunityQueryBuilder::create(&conn) let communities = CommunityQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.from_user_id_optional(user_id) .from_user_id(user_id)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
// Return the jwt // Return the jwt

View File

@ -180,7 +180,7 @@ impl Perform<GetPostResponse> for Oper<GetPost> {
let comments = CommentQueryBuilder::create(&conn) let comments = CommentQueryBuilder::create(&conn)
.for_post_id(data.id) .for_post_id(data.id)
.my_user_id_optional(user_id) .my_user_id(user_id)
.limit(9999) .limit(9999)
.list()?; .list()?;
@ -236,10 +236,10 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> {
.listing_type(type_) .listing_type(type_)
.sort(&sort) .sort(&sort)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.for_community_id_optional(data.community_id) .for_community_id(data.community_id)
.my_user_id_optional(user_id) .my_user_id(user_id)
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list() .list()
{ {
Ok(posts) => posts, Ok(posts) => posts,

View File

@ -322,75 +322,75 @@ impl Perform<SearchResponse> for Oper<Search> {
posts = PostQueryBuilder::create(&conn) posts = PostQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.show_nsfw(true) .show_nsfw(true)
.for_community_id_optional(data.community_id) .for_community_id(data.community_id)
.search_term(data.q.to_owned()) .search_term(data.q.to_owned())
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
} }
SearchType::Comments => { SearchType::Comments => {
comments = CommentQueryBuilder::create(&conn) comments = CommentQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.search_term(data.q.to_owned()) .search_term(data.q.to_owned())
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
} }
SearchType::Communities => { SearchType::Communities => {
communities = CommunityQueryBuilder::create(&conn) communities = CommunityQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.search_term(data.q.to_owned()) .search_term(data.q.to_owned())
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
} }
SearchType::Users => { SearchType::Users => {
users = UserQueryBuilder::create(&conn) users = UserQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.search_term(data.q.to_owned()) .search_term(data.q.to_owned())
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
} }
SearchType::All => { SearchType::All => {
posts = PostQueryBuilder::create(&conn) posts = PostQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.show_nsfw(true) .show_nsfw(true)
.for_community_id_optional(data.community_id) .for_community_id(data.community_id)
.search_term(data.q.to_owned()) .search_term(data.q.to_owned())
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
comments = CommentQueryBuilder::create(&conn) comments = CommentQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.search_term(data.q.to_owned()) .search_term(data.q.to_owned())
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
communities = CommunityQueryBuilder::create(&conn) communities = CommunityQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.search_term(data.q.to_owned()) .search_term(data.q.to_owned())
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
users = UserQueryBuilder::create(&conn) users = UserQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.search_term(data.q.to_owned()) .search_term(data.q.to_owned())
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
} }
SearchType::Url => { SearchType::Url => {
posts = PostQueryBuilder::create(&conn) posts = PostQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.show_nsfw(true) .show_nsfw(true)
.for_community_id_optional(data.community_id) .for_community_id(data.community_id)
.url_search(data.q.to_owned()) .url_search(data.q.to_owned())
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
} }
}; };

View File

@ -373,17 +373,17 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
.sort(&sort) .sort(&sort)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.saved_only(data.saved_only) .saved_only(data.saved_only)
.for_community_id_optional(data.community_id) .for_community_id(data.community_id)
.my_user_id_optional(user_id) .my_user_id(user_id)
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit); .limit(data.limit);
let mut comments_query = CommentQueryBuilder::create(&conn) let mut comments_query = CommentQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.saved_only(data.saved_only) .saved_only(data.saved_only)
.my_user_id_optional(user_id) .my_user_id(user_id)
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit); .limit(data.limit);
// If its saved only, you don't care what creator it was // 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 // Or, if its not saved, then you only want it for that specific creator
@ -561,8 +561,8 @@ impl Perform<GetRepliesResponse> for Oper<GetReplies> {
let replies = ReplyQueryBuilder::create(&conn, user_id) let replies = ReplyQueryBuilder::create(&conn, user_id)
.sort(&sort) .sort(&sort)
.unread_only(data.unread_only) .unread_only(data.unread_only)
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
Ok(GetRepliesResponse { Ok(GetRepliesResponse {
@ -589,8 +589,8 @@ impl Perform<GetUserMentionsResponse> for Oper<GetUserMentions> {
let mentions = UserMentionQueryBuilder::create(&conn, user_id) let mentions = UserMentionQueryBuilder::create(&conn, user_id)
.sort(&sort) .sort(&sort)
.unread_only(data.unread_only) .unread_only(data.unread_only)
.page_optional(data.page) .page(data.page)
.limit_optional(data.limit) .limit(data.limit)
.list()?; .list()?;
Ok(GetUserMentionsResponse { Ok(GetUserMentionsResponse {

View File

@ -92,43 +92,23 @@ impl<'a> CommentQueryBuilder<'a> {
self self
} }
pub fn for_post_id(mut self, for_post_id: i32) -> Self { pub fn for_post_id<T: MaybeOptional<i32>>(mut self, for_post_id: T) -> Self {
self.for_post_id = Some(for_post_id); self.for_post_id = for_post_id.get_optional();
self self
} }
pub fn for_post_id_optional(mut self, for_post_id: Option<i32>) -> Self { pub fn for_creator_id<T: MaybeOptional<i32>>(mut self, for_creator_id: T) -> Self {
self.for_post_id = for_post_id; self.for_creator_id = for_creator_id.get_optional();
self self
} }
pub fn for_creator_id(mut self, for_creator_id: i32) -> Self { pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
self.for_creator_id = Some(for_creator_id); self.search_term = search_term.get_optional();
self self
} }
pub fn for_creator_id_optional(mut self, for_creator_id: Option<i32>) -> Self { pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
self.for_creator_id = for_creator_id; self.my_user_id = my_user_id.get_optional();
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<String>) -> 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<i32>) -> Self {
self.my_user_id = my_user_id;
self self
} }
@ -137,23 +117,13 @@ impl<'a> CommentQueryBuilder<'a> {
self self
} }
pub fn page(mut self, page: i64) -> Self { pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
self.page = Some(page); self.page = page.get_optional();
self self
} }
pub fn page_optional(mut self, page: Option<i64>) -> Self { pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
self.page = page; self.limit = limit.get_optional();
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_optional(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self self
} }
@ -331,23 +301,13 @@ impl<'a> ReplyQueryBuilder<'a> {
self self
} }
pub fn page(mut self, page: i64) -> Self { pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
self.page = Some(page); self.page = page.get_optional();
self self
} }
pub fn page_optional(mut self, page: Option<i64>) -> Self { pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
self.page = page; self.limit = limit.get_optional();
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_optional(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self self
} }

View File

@ -135,50 +135,28 @@ impl<'a> CommunityQueryBuilder<'a> {
self self
} }
pub fn from_user_id(mut self, from_user_id: i32) -> Self { pub fn from_user_id<T: MaybeOptional<i32>>(mut self, from_user_id: T) -> Self {
self.from_user_id = Some(from_user_id); self.from_user_id = from_user_id.get_optional();
self self
} }
pub fn from_user_id_optional(self, from_user_id: Option<i32>) -> 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 { pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
self.show_nsfw = show_nsfw; self.show_nsfw = show_nsfw;
self self
} }
pub fn search_term(mut self, search_term: String) -> Self { pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
self.search_term = Some(search_term); self.search_term = search_term.get_optional();
self self
} }
pub fn search_term_optional(mut self, search_term: Option<String>) -> Self { pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
self.search_term = search_term; self.page = page.get_optional();
self self
} }
pub fn page(mut self, page: i64) -> Self { pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
self.page = Some(page); self.limit = limit.get_optional();
self
}
pub fn page_optional(mut self, page: Option<i64>) -> 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<i64>) -> Self {
self.limit = limit;
self self
} }

View File

@ -91,6 +91,22 @@ pub trait Readable<T> {
Self: Sized; Self: Sized;
} }
pub trait MaybeOptional<T> {
fn get_optional(self) -> Option<T>;
}
impl<T> MaybeOptional<T> for T {
fn get_optional(self) -> Option<T> {
return Some(self);
}
}
impl<T> MaybeOptional<T> for Option<T> {
fn get_optional(self) -> Option<T> {
return self;
}
}
pub fn establish_connection() -> PgConnection { pub fn establish_connection() -> PgConnection {
let db_url = Settings::get().db_url; let db_url = Settings::get().db_url;
PgConnection::establish(&db_url).expect(&format!("Error connecting to {}", db_url)) PgConnection::establish(&db_url).expect(&format!("Error connecting to {}", db_url))

View File

@ -120,65 +120,40 @@ impl<'a> PostQueryBuilder<'a> {
self self
} }
pub fn for_community_id(mut self, for_community_id: i32) -> Self { pub fn for_community_id<T: MaybeOptional<i32>>(mut self, for_community_id: T) -> Self {
use super::post_view::post_view::dsl::*; use super::post_view::post_view::dsl::*;
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.filter(community_id.eq(for_community_id));
self.query = self.query.then_order_by(stickied.desc()); self.query = self.query.then_order_by(stickied.desc());
}
self self
} }
pub fn for_community_id_optional(self, for_community_id: Option<i32>) -> Self { pub fn for_creator_id<T: MaybeOptional<i32>>(mut self, for_creator_id: T) -> Self {
match for_community_id { if let Some(for_creator_id) = for_creator_id.get_optional() {
Some(for_community_id) => self.for_community_id(for_community_id),
None => self,
}
}
pub fn for_creator_id(mut self, for_creator_id: i32) -> Self {
self.for_creator_id = Some(for_creator_id); self.for_creator_id = Some(for_creator_id);
}
self self
} }
pub fn for_creator_id_optional(self, for_creator_id: Option<i32>) -> Self { pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> 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 {
use super::post_view::post_view::dsl::*; use super::post_view::post_view::dsl::*;
if let Some(search_term) = search_term.get_optional() {
self.query = self.query.filter(name.ilike(fuzzy_search(&search_term))); self.query = self.query.filter(name.ilike(fuzzy_search(&search_term)));
}
self self
} }
pub fn search_term_optional(self, search_term: Option<String>) -> Self { pub fn url_search<T: MaybeOptional<String>>(mut self, url_search: T) -> Self {
match search_term {
Some(search_term) => self.search_term(search_term),
None => self,
}
}
pub fn url_search(mut self, url_search: String) -> Self {
use super::post_view::post_view::dsl::*; use super::post_view::post_view::dsl::*;
if let Some(url_search) = url_search.get_optional() {
self.query = self.query.filter(url.eq(url_search)); self.query = self.query.filter(url.eq(url_search));
}
self self
} }
pub fn url_search_optional(self, url_search: Option<String>) -> Self { pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
match url_search { self.my_user_id = my_user_id.get_optional();
Some(url_search) => self.url_search(url_search),
None => 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<i32>) -> Self {
self.my_user_id = my_user_id;
self self
} }
@ -197,23 +172,13 @@ impl<'a> PostQueryBuilder<'a> {
self self
} }
pub fn page(mut self, page: i64) -> Self { pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
self.page = Some(page); self.page = page.get_optional();
self self
} }
pub fn page_optional(mut self, page: Option<i64>) -> Self { pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
self.page = page; self.limit = limit.get_optional();
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_optional(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self self
} }

View File

@ -96,23 +96,13 @@ impl<'a> UserMentionQueryBuilder<'a> {
self self
} }
pub fn page(mut self, page: i64) -> Self { pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
self.page = Some(page); self.page = page.get_optional();
self self
} }
pub fn page_optional(mut self, page: Option<i64>) -> Self { pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
self.page = page; self.limit = limit.get_optional();
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_optional(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self self
} }

View File

@ -62,36 +62,21 @@ impl<'a> UserQueryBuilder<'a> {
self self
} }
pub fn search_term(mut self, search_term: String) -> Self { pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
use super::user_view::user_view::dsl::*; use super::user_view::user_view::dsl::*;
if let Some(search_term) = search_term.get_optional() {
self.query = self.query.filter(name.ilike(fuzzy_search(&search_term))); self.query = self.query.filter(name.ilike(fuzzy_search(&search_term)));
}
self self
} }
pub fn search_term_optional(self, search_term: Option<String>) -> Self { pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
match search_term { self.page = page.get_optional();
Some(search_term) => self.search_term(search_term),
None => self,
}
}
pub fn page(mut self, page: i64) -> Self {
self.page = Some(page);
self self
} }
pub fn page_optional(mut self, page: Option<i64>) -> Self { pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
self.page = page; self.limit = limit.get_optional();
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_optional(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self self
} }