Skip to content

Commit

Permalink
Added total_items to pagination view & response
Browse files Browse the repository at this point in the history
  • Loading branch information
DenuxPlays committed Jan 20, 2025
1 parent ae993b4 commit 01d8f9e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs-site/content/docs/the-app/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ impl PaginationResponse {
page: pagination_query.page,
page_size: pagination_query.page_size,
total_pages: data.total_pages,
total_items: data.total_items,
},
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/controller/views/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub struct PagerMeta {
pub page_size: u64,
#[serde(rename(serialize = "total_pages"))]
pub total_pages: u64,
#[serde(rename(serialize = "total_items"))]
pub total_items: u64,
}

impl<T> Pager<T> {
Expand Down
17 changes: 13 additions & 4 deletions src/model/query/paginate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ where
pub struct PageResponse<T> {
pub page: Vec<T>,
pub total_pages: u64,
pub total_items: u64,
}

use crate::Result as LocoResult;
Expand Down Expand Up @@ -163,10 +164,14 @@ where
};

let query = entity.paginate(db, pagination_query.page_size);
let total_pages = query.num_pages().await?;
let total_pages_and_items = query.num_items_and_pages().await?;
let page: Vec<<E as EntityTrait>::Model> = query.fetch_page(page).await?;

let paginated_response = PageResponse { page, total_pages };
let paginated_response = PageResponse {
page,
total_pages: total_pages_and_items.number_of_pages,
total_items: total_pages_and_items.number_of_items,
};

Ok(paginated_response)
}
Expand Down Expand Up @@ -211,8 +216,12 @@ where
};

let query = selector.paginate(db, pagination_query.page_size);
let total_pages = query.num_pages().await?;
let total_pages_and_items = query.num_items_and_pages().await?;
let page = query.fetch_page(page).await?;

Ok(PageResponse { page, total_pages })
Ok(PageResponse {
page,
total_pages: total_pages_and_items.number_of_pages,
total_items: total_pages_and_items.number_of_items,
})
}

0 comments on commit 01d8f9e

Please sign in to comment.