reusable pagination types

This commit is contained in:
Brandon Thomas committed 2023-12-04 23:11:41 -05:00
1 parent e6f5ecb980
commit b9f604dd18
4 files changed
+95 -4

No files matched your search

@@ -1 +1,3 @@
pub mod pagination_cursors;
pub mod pagination_page;
pub mod user_details_lite;
@@ -0,0 +1,53 @@
/// Pagination by cursors
/// These types of pagination are for "infinite scrolling", which do not reveal a number of pages.
/// This is good so that investors and competitors cannot reveal how many database records we have.
/// This is typically used for "discovery" type pages, not user profiles.
///
/// See `PaginationPage` for the other type of pagination.
#[derive(Serialize)]
pub struct PaginationCursors {
/// The "next" cursor.
/// This is an opaque (typically even encrypted) handle.
pub maybe_next: Option<String>,
/// The "previous" cursor.
/// This is an opaque (typically even encrypted) handle.
pub maybe_previous: Option<String>,
/// Details whether we're walking forward or backward.
pub cursor_is_reversed: bool,
}
#[cfg(test)]
mod tests {
use crate::http_server::common_responses::pagination_cursors::PaginationCursors;
#[test]
fn test_serialize_empty_json() {
// Make sure this interface doesn't change.
let pagination = PaginationCursors {
maybe_next: None,
maybe_previous: None,
cursor_is_reversed: false,
};
let json = serde_json::to_string(&pagination).expect("serialization");
assert_eq!(json, "{\"maybe_next\":null,\"maybe_previous\":null,\"cursor_is_reversed\":false}")
}
#[test]
fn test_serialize_full_json() {
// Make sure this interface doesn't change.
let pagination = PaginationCursors {
maybe_next: Some("foo".to_string()),
maybe_previous: Some("bar".to_string()),
cursor_is_reversed: true,
};
let json = serde_json::to_string(&pagination).expect("serialization");
assert_eq!(json, "{\"maybe_next\":\"foo\",\"maybe_previous\":\"bar\",\"cursor_is_reversed\":true}")
}
}
@@ -0,0 +1,33 @@
/// Pagination by page id
/// This type of pagination is by "page id" and "page count".
/// This should never be used to walk the entire public database as competitors and investors
/// can use it to glean information about the scale of our service.
/// This is best used for user profiles and scoped down results.
///
/// See `PaginationCursors` for the other type of pagination.
#[derive(Serialize)]
pub struct PaginationPage {
/// The current page number
pub current: u64,
/// How many pages there are in total
pub total_page_count: u64,
}
#[cfg(test)]
mod tests {
use crate::http_server::common_responses::pagination_page::PaginationPage;
#[test]
fn test_serialize() {
let pagination = PaginationPage {
current: 5,
total_page_count: 123,
};
let json = serde_json::to_string(&pagination).expect("serialization");
assert_eq!(json, "{\"current\":5,\"total_page_count\":123}")
}
}
@@ -15,6 +15,7 @@ use enums::common::visibility::Visibility;
use mysql_queries::queries::media_files::list_media_files_for_user::{list_media_files_for_user, ListMediaFileForUserArgs, ViewAs};
use tokens::tokens::media_files::MediaFileToken;
use crate::http_server::common_responses::pagination_cursors::PaginationCursors;
use crate::http_server::web_utils::response_error_helpers::to_simple_json_error;
use crate::server_state::ServerState;
@@ -36,8 +37,7 @@ pub struct QueryParams {
pub struct SuccessResponse {
pub success: bool,
pub results: Vec<MediaFileListItem>,
pub cursor_next: Option<String>,
pub cursor_previous: Option<String>,
pub pagination: PaginationCursors,
}
#[derive(Serialize)]
@@ -202,8 +202,11 @@ pub async fn list_media_files_for_user_handler(
let response = SuccessResponse {
success: true,
results,
cursor_next,
cursor_previous,
pagination: PaginationCursors {
maybe_next: cursor_next,
maybe_previous: cursor_previous,
cursor_is_reversed,
}
};
let body = serde_json::to_string(&response)