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 0f5d5de
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 8 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
1 change: 1 addition & 0 deletions examples/demo/src/views/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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}}",
)
Original file line number Diff line number Diff line change
@@ -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}}",
)
Original file line number Diff line number Diff line change
@@ -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}}",
)
Original file line number Diff line number Diff line change
@@ -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}}",
)
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 0f5d5de

Please sign in to comment.