-impl<'a, 'b: 'a> PersonQuery<'a, 'b> {
- pub async fn list(self) -> Result<Vec<PersonView>, Error> {
- let conn = &mut get_conn(self.pool).await?;
- let mut query = person::table
- .inner_join(person_aggregates::table)
- .select((person::all_columns, person_aggregates::all_columns))
- .into_boxed();
-
- if let Some(search_term) = self.search_term {
- let searcher = fuzzy_search(&search_term);
- query = query
- .filter(person::name.ilike(searcher.clone()))
- .or_filter(person::display_name.ilike(searcher));
- }
-
- query = match self.sort.unwrap_or(SortType::Hot) {
- SortType::New | SortType::NewComments => query.order_by(person::published.desc()),
- SortType::Old => query.order_by(person::published.asc()),
- SortType::Hot | SortType::Active | SortType::TopAll => {
- query.order_by(person_aggregates::comment_score.desc())
- }
- SortType::MostComments => query.order_by(person_aggregates::comment_count.desc()),
- SortType::TopYear => query
- .filter(person::published.gt(now - 1.years()))
- .order_by(person_aggregates::comment_score.desc()),
- SortType::TopMonth => query
- .filter(person::published.gt(now - 1.months()))
- .order_by(person_aggregates::comment_score.desc()),
- SortType::TopWeek => query
- .filter(person::published.gt(now - 1.weeks()))
- .order_by(person_aggregates::comment_score.desc()),
- SortType::TopDay => query
- .filter(person::published.gt(now - 1.days()))
- .order_by(person_aggregates::comment_score.desc()),
- SortType::TopHour => query
- .filter(person::published.gt(now - 1.hours()))
- .order_by(person_aggregates::comment_score.desc()),
- SortType::TopSixHour => query
- .filter(person::published.gt(now - 6.hours()))
- .order_by(person_aggregates::comment_score.desc()),
- SortType::TopTwelveHour => query
- .filter(person::published.gt(now - 12.hours()))
- .order_by(person_aggregates::comment_score.desc()),
- SortType::TopThreeMonths => query
- .filter(person::published.gt(now - 3.months()))
- .order_by(person_aggregates::comment_score.desc()),
- SortType::TopSixMonths => query
- .filter(person::published.gt(now - 6.months()))
- .order_by(person_aggregates::comment_score.desc()),
- SortType::TopNineMonths => query
- .filter(person::published.gt(now - 9.months()))
- .order_by(person_aggregates::comment_score.desc()),
- };
-
- let (limit, offset) = limit_and_offset(self.page, self.limit)?;
- query = query.limit(limit).offset(offset);
-
- let res = query.load::<PersonViewTuple>(conn).await?;
-
- Ok(res.into_iter().map(PersonView::from_tuple).collect())