Hi, I would like to suggest to add a field(meta: PagerMeta) to PageResponse struct.
because, currently, if we want to use pagination feature,
we have to pass same PaginationQuery over to both pagination function and response constructor.
if PageResponse(returned by paginate function) has PagerMeta(or datas for PagerMeta),
we may can use the pagination feature more concisely.
current
here is a pagination sample based on the guide
// src/controllers/auth.rs
#[debug_handler]
pub async fn all_users(State(ctx): State<AppContext>) -> Result<Response> {
// assign PaginationQuery to a variable, because it is used twice
let pagination_query: query::PaginationQuery = query::PaginationQuery {
page: 1,
page_size: 100,
};
// used here first for pagination
let paginated_users =
query::paginate(&ctx.db, users::Entity::find(), None, &pagination_query).await?;
// used here also for response constructor,
// because it takes `page`(current page) and `page_size` from PaginationQuery
// for creating `PagerMeta`
format::json(PaginatedAllUsersResponse::new(
paginated_users,
&pagination_query,
))
}
// src/views/auth.rs
// define filter struct
#[derive(Debug, Deserialize, Serialize)]
pub struct AllUsersResponseFilter {
pub pid: String,
pub name: String,
pub email: String,
}
// implement `From` trait for the filter struct
impl From<users::Model> for AllUsersResponseFilter {
fn from(user: users::Model) -> Self {
Self {
pid: user.pid.to_string(),
name: user.name,
email: user.email,
}
}
}
// define response struct
#[derive(Debug, Deserialize, Serialize)]
pub struct PaginatedAllUsersResponse {}
// define constructor of response struct which accepts PaginationQuery as 2nd param
impl PaginatedAllUsersResponse {
#[must_use]
pub fn new(
data: PageResponse<users::Model>,
pagination_query: &PaginationQuery,
) -> Pager<Vec<AllUsersResponseFilter>> {
Pager {
results: data
.page
.into_iter()
.map(AllUsersResponseFilter::from)
.collect::<Vec<AllUsersResponseFilter>>(),
info: PagerMeta {
page: pagination_query.page,
page_size: pagination_query.page_size,
total_pages: data.total_pages,
total_items: data.total_items,
},
}
}
}
suggest
if PageResponse has PagerMeta(field meta for example), then it may look like this
// src/controllers/auth.rs
#[debug_handler]
pub async fn all_users(State(ctx): State<AppContext>) -> Result<Response> {
format::json(PaginatedAllUsersResponse::new(
query::paginate(
&ctx.db,
users::Entity::find(),
None,
&query::PaginationQuery {
page: 1,
page_size: 100,
},
)
.await?,
))
}
// src/views/auth.rs
#[derive(Debug, Deserialize, Serialize)]
pub struct AllUsersResponseFilter {
pub pid: String,
pub name: String,
pub email: String,
}
impl From<users::Model> for AllUsersResponseFilter {
fn from(user: users::Model) -> Self {
Self {
pid: user.pid.to_string(),
name: user.name,
email: user.email,
}
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct PaginatedAllUsersResponse {}
impl PaginatedAllUsersResponse {
#[must_use]
pub fn new(
data: PageResponse<users::Model>,
) -> Pager<Vec<AllUsersResponseFilter>> {
Pager {
results: data
.page
.into_iter()
.map(AllUsersResponseFilter::from)
.collect::<Vec<AllUsersResponseFilter>>(),
info: data.meta
}
}
}
thanks
Hi, I would like to suggest to add a field(
meta: PagerMeta) toPageResponsestruct.because, currently, if we want to use pagination feature,
we have to pass same
PaginationQueryover to both pagination function and response constructor.if
PageResponse(returned bypaginatefunction) hasPagerMeta(or datas forPagerMeta),we may can use the pagination feature more concisely.
current
here is a pagination sample based on the guide
suggest
if
PageResponsehasPagerMeta(fieldmetafor example), then it may look like thisthanks