newtypes::PersonId,
schema::{person, person_aggregates},
source::person::{Person, PersonSafe},
- traits::{MaybeOptional, ToSafe, ViewToVec},
+ traits::{ToSafe, ViewToVec},
utils::{fuzzy_search, limit_and_offset},
SortType,
};
+use typed_builder::TypedBuilder;
type PersonViewSafeTuple = (PersonSafe, PersonAggregates);
impl PersonViewSafe {
- pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
+ pub fn read(conn: &mut PgConnection, person_id: PersonId) -> Result<Self, Error> {
let (person, counts) = person::table
.find(person_id)
.inner_join(person_aggregates::table)
Ok(Self { person, counts })
}
- pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
+ pub fn admins(conn: &mut PgConnection) -> Result<Vec<Self>, Error> {
let admins = person::table
.inner_join(person_aggregates::table)
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
Ok(Self::from_tuple_to_vec(admins))
}
- pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
+ pub fn banned(conn: &mut PgConnection) -> Result<Vec<Self>, Error> {
let banned = person::table
.inner_join(person_aggregates::table)
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
}
}
-pub struct PersonQueryBuilder<'a> {
- conn: &'a PgConnection,
+#[derive(TypedBuilder)]
+#[builder(field_defaults(default))]
+pub struct PersonQuery<'a> {
+ #[builder(!default)]
+ conn: &'a mut PgConnection,
sort: Option<SortType>,
search_term: Option<String>,
page: Option<i64>,
limit: Option<i64>,
}
-impl<'a> PersonQueryBuilder<'a> {
- pub fn create(conn: &'a PgConnection) -> Self {
- PersonQueryBuilder {
- conn,
- search_term: None,
- sort: None,
- page: None,
- limit: None,
- }
- }
-
- pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
- self.sort = sort.get_optional();
- self
- }
-
- pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
- self.search_term = search_term.get_optional();
- self
- }
-
- pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
- self.page = page.get_optional();
- self
- }
-
- pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
- self.limit = limit.get_optional();
- self
- }
-
+impl<'a> PersonQuery<'a> {
pub fn list(self) -> Result<Vec<PersonViewSafe>, Error> {
let mut query = person::table
.inner_join(person_aggregates::table)
.into_boxed();
if let Some(search_term) = self.search_term {
- query = query.filter(person::name.ilike(fuzzy_search(&search_term)));
+ let searcher = fuzzy_search(&search_term);
+ query = query
+ .filter(person::name.ilike(searcher.to_owned()))
+ .or_filter(person::display_name.ilike(searcher));
}
query = match self.sort.unwrap_or(SortType::Hot) {
type DbTuple = PersonViewSafeTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
items
- .iter()
+ .into_iter()
.map(|a| Self {
- person: a.0.to_owned(),
- counts: a.1.to_owned(),
+ person: a.0,
+ counts: a.1,
})
.collect::<Vec<Self>>()
}