Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/model/query/paginate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ where
s.parse().map_err(serde::de::Error::custom)
}

use crate::controller::views::pagination::PagerMeta;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using controller views inside the model can break the separation between business logic and view responses.
I think introducing a dedicated struct and implementing Into<PagerMeta> for the relevant types would be a clean solution.


#[derive(Debug)]
pub struct PageResponse<T> {
pub page: Vec<T>,
pub total_pages: u64,
pub total_items: u64,
pub meta: PagerMeta,
}

use crate::Result as LocoResult;

/// Paginate function for fetching paginated data from the database.
Expand Down Expand Up @@ -165,8 +165,12 @@ where

let paginated_response = PageResponse {
page,
total_pages: total_pages_and_items.number_of_pages,
total_items: total_pages_and_items.number_of_items,
meta: PagerMeta {
page: pagination_query.page,
page_size: pagination_query.page_size,
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 @@ -213,7 +217,11 @@ where

Ok(PageResponse {
page,
total_pages: total_pages_and_items.number_of_pages,
total_items: total_pages_and_items.number_of_items,
meta: PagerMeta {
page: pagination_query.page,
page_size: pagination_query.page_size,
total_pages: total_pages_and_items.number_of_pages,
total_items: total_pages_and_items.number_of_items,
},
})
}
Loading