From 53227361e8633f7194e8d9eaf90b5a3d3eda011a Mon Sep 17 00:00:00 2001 From: Timon Klinkert <83671398+DenuxPlays@users.noreply.github.com> Date: Thu, 23 Jan 2025 16:16:49 +0100 Subject: [PATCH] Added `total_items` to pagination view & response (#1197) Co-authored-by: Elad Kaplan --- docs-site/content/docs/the-app/controller.md | 1 + examples/demo/src/views/notes.rs | 1 + .../snapshots/get_notes@notes_request.snap | 3 ++- .../get_notes_with_filters@notes_request.snap | 3 ++- .../get_notes_with_page_size@notes_request.snap | 3 ++- ..._notes_with_size_and_page@notes_request.snap | 3 ++- src/controller/views/pagination.rs | 2 ++ src/model/query/paginate/mod.rs | 17 +++++++++++++---- 8 files changed, 25 insertions(+), 8 deletions(-) diff --git a/docs-site/content/docs/the-app/controller.md b/docs-site/content/docs/the-app/controller.md index 8dfd3ed6e..566699dec 100644 --- a/docs-site/content/docs/the-app/controller.md +++ b/docs-site/content/docs/the-app/controller.md @@ -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, }, } } diff --git a/examples/demo/src/views/notes.rs b/examples/demo/src/views/notes.rs index 7fa32bfa9..a10ec320e 100644 --- a/examples/demo/src/views/notes.rs +++ b/examples/demo/src/views/notes.rs @@ -42,6 +42,7 @@ impl PaginationResponse { page: pagination_query.page, page_size: pagination_query.page_size, total_pages: data.total_pages, + total_items: data.total_items, }, } } diff --git a/examples/demo/tests/requests/snapshots/get_notes@notes_request.snap b/examples/demo/tests/requests/snapshots/get_notes@notes_request.snap index c0634f659..4a103a03e 100644 --- a/examples/demo/tests/requests/snapshots/get_notes@notes_request.snap +++ b/examples/demo/tests/requests/snapshots/get_notes@notes_request.snap @@ -1,8 +1,9 @@ --- source: tests/requests/notes.rs +assertion_line: 41 expression: "(notes.status_code(), notes.text())" --- ( 200, - "{\"results\":[{\"id\":ID,\"title\":null,\"content\":\"content 1\"},{\"id\":ID,\"title\":\"Loco note 2\",\"content\":null},{\"id\":ID,\"title\":\"Loco note 3\",\"content\":\"content 3\"},{\"id\":ID,\"title\":\"Loco note 4\",\"content\":\"content 4\"},{\"id\":ID,\"title\":\"Loco note 5\",\"content\":\"content 5\"},{\"id\":ID,\"title\":\"Loco 6\",\"content\":\"content 6\"},{\"id\":ID,\"title\":\"Loco 7\",\"content\":\"content 7\"},{\"id\":ID,\"title\":\"Loco 8\",\"content\":\"content 8\"},{\"id\":ID,\"title\":\"Loco 9\",\"content\":\"content 9\"},{\"id\":ID,\"title\":\"Loco 10\",\"content\":\"content 10\"},{\"id\":ID,\"title\":\"Loco 11\",\"content\":\"content 11\"}],\"pagination\":{\"page\":1,\"page_size\":25,\"total_pages\":1}}", + "{\"results\":[{\"id\":ID,\"title\":null,\"content\":\"content 1\"},{\"id\":ID,\"title\":\"Loco note 2\",\"content\":null},{\"id\":ID,\"title\":\"Loco note 3\",\"content\":\"content 3\"},{\"id\":ID,\"title\":\"Loco note 4\",\"content\":\"content 4\"},{\"id\":ID,\"title\":\"Loco note 5\",\"content\":\"content 5\"},{\"id\":ID,\"title\":\"Loco 6\",\"content\":\"content 6\"},{\"id\":ID,\"title\":\"Loco 7\",\"content\":\"content 7\"},{\"id\":ID,\"title\":\"Loco 8\",\"content\":\"content 8\"},{\"id\":ID,\"title\":\"Loco 9\",\"content\":\"content 9\"},{\"id\":ID,\"title\":\"Loco 10\",\"content\":\"content 10\"},{\"id\":ID,\"title\":\"Loco 11\",\"content\":\"content 11\"}],\"pagination\":{\"page\":1,\"page_size\":25,\"total_pages\":1,\"total_items\":11}}", ) diff --git a/examples/demo/tests/requests/snapshots/get_notes_with_filters@notes_request.snap b/examples/demo/tests/requests/snapshots/get_notes_with_filters@notes_request.snap index 8ebdd4584..f9cb22e48 100644 --- a/examples/demo/tests/requests/snapshots/get_notes_with_filters@notes_request.snap +++ b/examples/demo/tests/requests/snapshots/get_notes_with_filters@notes_request.snap @@ -1,8 +1,9 @@ --- source: tests/requests/notes.rs +assertion_line: 41 expression: "(notes.status_code(), notes.text())" --- ( 200, - "{\"results\":[{\"id\":ID,\"title\":\"Loco note 2\",\"content\":null},{\"id\":ID,\"title\":\"Loco note 3\",\"content\":\"content 3\"}],\"pagination\":{\"page\":1,\"page_size\":2,\"total_pages\":2}}", + "{\"results\":[{\"id\":ID,\"title\":\"Loco note 2\",\"content\":null},{\"id\":ID,\"title\":\"Loco note 3\",\"content\":\"content 3\"}],\"pagination\":{\"page\":1,\"page_size\":2,\"total_pages\":2,\"total_items\":4}}", ) diff --git a/examples/demo/tests/requests/snapshots/get_notes_with_page_size@notes_request.snap b/examples/demo/tests/requests/snapshots/get_notes_with_page_size@notes_request.snap index a9d8ef437..42447a072 100644 --- a/examples/demo/tests/requests/snapshots/get_notes_with_page_size@notes_request.snap +++ b/examples/demo/tests/requests/snapshots/get_notes_with_page_size@notes_request.snap @@ -1,8 +1,9 @@ --- source: tests/requests/notes.rs +assertion_line: 41 expression: "(notes.status_code(), notes.text())" --- ( 200, - "{\"results\":[{\"id\":ID,\"title\":null,\"content\":\"content 1\"}],\"pagination\":{\"page\":1,\"page_size\":1,\"total_pages\":11}}", + "{\"results\":[{\"id\":ID,\"title\":null,\"content\":\"content 1\"}],\"pagination\":{\"page\":1,\"page_size\":1,\"total_pages\":11,\"total_items\":11}}", ) diff --git a/examples/demo/tests/requests/snapshots/get_notes_with_size_and_page@notes_request.snap b/examples/demo/tests/requests/snapshots/get_notes_with_size_and_page@notes_request.snap index 429dd3de6..94b3c8993 100644 --- a/examples/demo/tests/requests/snapshots/get_notes_with_size_and_page@notes_request.snap +++ b/examples/demo/tests/requests/snapshots/get_notes_with_size_and_page@notes_request.snap @@ -1,8 +1,9 @@ --- source: tests/requests/notes.rs +assertion_line: 41 expression: "(notes.status_code(), notes.text())" --- ( 200, - "{\"results\":[{\"id\":ID,\"title\":\"Loco 6\",\"content\":\"content 6\"},{\"id\":ID,\"title\":\"Loco 7\",\"content\":\"content 7\"},{\"id\":ID,\"title\":\"Loco 8\",\"content\":\"content 8\"},{\"id\":ID,\"title\":\"Loco 9\",\"content\":\"content 9\"},{\"id\":ID,\"title\":\"Loco 10\",\"content\":\"content 10\"}],\"pagination\":{\"page\":2,\"page_size\":5,\"total_pages\":3}}", + "{\"results\":[{\"id\":ID,\"title\":\"Loco 6\",\"content\":\"content 6\"},{\"id\":ID,\"title\":\"Loco 7\",\"content\":\"content 7\"},{\"id\":ID,\"title\":\"Loco 8\",\"content\":\"content 8\"},{\"id\":ID,\"title\":\"Loco 9\",\"content\":\"content 9\"},{\"id\":ID,\"title\":\"Loco 10\",\"content\":\"content 10\"}],\"pagination\":{\"page\":2,\"page_size\":5,\"total_pages\":3,\"total_items\":11}}", ) diff --git a/src/controller/views/pagination.rs b/src/controller/views/pagination.rs index 09d53f966..8fb31fcf2 100644 --- a/src/controller/views/pagination.rs +++ b/src/controller/views/pagination.rs @@ -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 Pager { diff --git a/src/model/query/paginate/mod.rs b/src/model/query/paginate/mod.rs index 04ef388b9..af2a19a9b 100644 --- a/src/model/query/paginate/mod.rs +++ b/src/model/query/paginate/mod.rs @@ -78,6 +78,7 @@ where pub struct PageResponse { pub page: Vec, pub total_pages: u64, + pub total_items: u64, } use crate::Result as LocoResult; @@ -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<::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) } @@ -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, + }) }