Create a num_items_and_pages on the paginator (#768)

* Create a `num_items_and_pages` on the `paginator`

This method allows us to get both number of items and pages of a
paginator with only one database query.

* Improve documentation

* Create a dedicated struct for items and pages number
This commit is contained in:
Eric 2022-06-30 20:50:20 +02:00 committed by GitHub
parent b40ce6ee83
commit 5037dc99ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,6 +24,15 @@ where
pub(crate) selector: PhantomData<S>, pub(crate) selector: PhantomData<S>,
} }
/// Define a structure containing the numbers of items and pages of a Paginator
#[derive(Clone, Debug)]
pub struct ItemsAndPagesNumber {
/// The total number of items of a paginator
pub number_of_items: usize,
/// The total number of pages of a paginator
pub number_of_pages: usize,
}
// LINT: warn if paginator is used without an order by clause // LINT: warn if paginator is used without an order by clause
impl<'db, C, S> Paginator<'db, C, S> impl<'db, C, S> Paginator<'db, C, S>
@ -80,10 +89,23 @@ where
/// Get the total number of pages /// Get the total number of pages
pub async fn num_pages(&self) -> Result<usize, DbErr> { pub async fn num_pages(&self) -> Result<usize, DbErr> {
let num_items = self.num_items().await?; let num_items = self.num_items().await?;
let num_pages = (num_items / self.page_size) + (num_items % self.page_size > 0) as usize; let num_pages = self.compute_pages_number(num_items);
Ok(num_pages) Ok(num_pages)
} }
/// Get the total number of items and pages
pub async fn num_items_and_pages(&self) -> Result<ItemsAndPagesNumber, DbErr> {
let number_of_items = self.num_items().await?;
let number_of_pages = self.compute_pages_number(number_of_items);
Ok(ItemsAndPagesNumber { number_of_items, number_of_pages })
}
/// Compute the number of pages for the current page
fn compute_pages_number(&self, num_items: usize) -> usize {
(num_items / self.page_size) + (num_items % self.page_size > 0) as usize
}
/// Increment the page counter /// Increment the page counter
pub fn next(&mut self) { pub fn next(&mut self) {
self.page += 1; self.page += 1;