use crate::{ aggregates::user_aggregates::UserAggregates, fuzzy_search, limit_and_offset, schema::{user_, user_aggregates}, source::user::{UserSafe, User_}, views::ViewToVec, MaybeOptional, SortType, ToSafe, }; use diesel::{dsl::*, result::Error, *}; use serde::Serialize; #[derive(Debug, Serialize, Clone)] pub struct UserViewSafe { pub user: UserSafe, pub counts: UserAggregates, } type UserViewSafeTuple = (UserSafe, UserAggregates); #[derive(Debug, Serialize, Clone)] pub struct UserViewDangerous { pub user: User_, pub counts: UserAggregates, } type UserViewDangerousTuple = (User_, UserAggregates); impl UserViewDangerous { pub fn read(conn: &PgConnection, id: i32) -> Result { let (user, counts) = user_::table .find(id) .inner_join(user_aggregates::table) .first::(conn)?; Ok(Self { user, counts }) } } impl UserViewSafe { pub fn read(conn: &PgConnection, id: i32) -> Result { let (user, counts) = user_::table .find(id) .inner_join(user_aggregates::table) .select((User_::safe_columns_tuple(), user_aggregates::all_columns)) .first::(conn)?; Ok(Self { user, counts }) } pub fn admins(conn: &PgConnection) -> Result, Error> { let admins = user_::table .inner_join(user_aggregates::table) .select((User_::safe_columns_tuple(), user_aggregates::all_columns)) .filter(user_::admin.eq(true)) .order_by(user_::published) .load::(conn)?; Ok(Self::to_vec(admins)) } pub fn banned(conn: &PgConnection) -> Result, Error> { let banned = user_::table .inner_join(user_aggregates::table) .select((User_::safe_columns_tuple(), user_aggregates::all_columns)) .filter(user_::banned.eq(true)) .load::(conn)?; Ok(Self::to_vec(banned)) } } // TODO can get rid of this by not boxing the query before the list() mod join_types { use crate::schema::{user_, user_aggregates}; use diesel::{ pg::Pg, query_builder::BoxedSelectStatement, query_source::joins::{Inner, Join, JoinOn}, sql_types::*, }; /// TODO awful, but necessary because of the boxed join pub(super) type BoxedUserJoin<'a> = BoxedSelectStatement< 'a, ( // UserSafe column types ( Integer, Text, Nullable, Nullable, Bool, Bool, Timestamp, Nullable, Nullable, Text, Nullable, Bool, Nullable, Bool, ), // UserAggregates column types (Integer, Integer, BigInt, BigInt, BigInt, BigInt), ), JoinOn< Join, diesel::expression::operators::Eq< diesel::expression::nullable::Nullable, diesel::expression::nullable::Nullable, >, >, Pg, >; } pub struct UserQueryBuilder<'a> { conn: &'a PgConnection, query: join_types::BoxedUserJoin<'a>, sort: &'a SortType, page: Option, limit: Option, } impl<'a> UserQueryBuilder<'a> { pub fn create(conn: &'a PgConnection) -> Self { let query = user_::table .inner_join(user_aggregates::table) .select((User_::safe_columns_tuple(), user_aggregates::all_columns)) .into_boxed(); UserQueryBuilder { conn, query, sort: &SortType::Hot, page: None, limit: None, } } pub fn sort(mut self, sort: &'a SortType) -> Self { self.sort = sort; self } pub fn search_term>(mut self, search_term: T) -> Self { if let Some(search_term) = search_term.get_optional() { self.query = self .query .filter(user_::name.ilike(fuzzy_search(&search_term))); } self } pub fn page>(mut self, page: T) -> Self { self.page = page.get_optional(); self } pub fn limit>(mut self, limit: T) -> Self { self.limit = limit.get_optional(); self } pub fn list(self) -> Result, Error> { let mut query = self.query; query = match self.sort { SortType::Hot => query .order_by(user_aggregates::comment_score.desc()) .then_order_by(user_::published.desc()), SortType::Active => query .order_by(user_aggregates::comment_score.desc()) .then_order_by(user_::published.desc()), SortType::New => query.order_by(user_::published.desc()), SortType::TopAll => query.order_by(user_aggregates::comment_score.desc()), SortType::TopYear => query .filter(user_::published.gt(now - 1.years())) .order_by(user_aggregates::comment_score.desc()), SortType::TopMonth => query .filter(user_::published.gt(now - 1.months())) .order_by(user_aggregates::comment_score.desc()), SortType::TopWeek => query .filter(user_::published.gt(now - 1.weeks())) .order_by(user_aggregates::comment_score.desc()), SortType::TopDay => query .filter(user_::published.gt(now - 1.days())) .order_by(user_aggregates::comment_score.desc()), }; let (limit, offset) = limit_and_offset(self.page, self.limit); query = query.limit(limit).offset(offset); let res = query.load::(self.conn)?; Ok(UserViewSafe::to_vec(res)) } } impl ViewToVec for UserViewSafe { type DbTuple = UserViewSafeTuple; fn to_vec(users: Vec) -> Vec { users .iter() .map(|a| Self { user: a.0.to_owned(), counts: a.1.to_owned(), }) .collect::>() } }