Skip to content

Commit

Permalink
feat(routes): Add pagination to GET /product response
Browse files Browse the repository at this point in the history
  • Loading branch information
flo-ride committed Oct 18, 2024
1 parent e2b1916 commit c2cbded
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 224 deletions.
17 changes: 17 additions & 0 deletions src/models/response/product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,20 @@ impl TryFrom<entity::product::Model> for ProductResponse {
})
}
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, utoipa::ToSchema)]
#[schema(example = json!(
{
"total_page": 1,
"current_page": 0,
"products": [
{ "id": "1a731f58-18f1-4c95-8de5-611bde07f4f1", "image": "2fa4c8d3-fd93-4066-a7f3-68a35ab72288_water.png", "name": "water", "price": 0.80, "quantity": 27, "creation_time": "2024-10-09T17:55:30.795279Z" },
{ "id": "0a7e6dd2-2c98-44b1-9cd3-0d8a3d7666b3", "image": "377265f4-1aad-4b57-a6f2-4bb6387184c2_tea.png", "name": "tea", "price": 1.52, "quantity": 42, "creation_time": "2024-10-09T18:32:10.795279Z" }
]
}
))]
pub struct ProductListResponse {
pub total_page: u64,
pub current_page: u64,
pub products: Vec<ProductResponse>,
}
22 changes: 16 additions & 6 deletions src/routes/product/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use crate::{
error::AppError,
models::{
profile::admin::Admin, response::product::ProductResponse, utils::pagination::Pagination,
profile::admin::Admin,
response::product::{ProductListResponse, ProductResponse},
utils::pagination::Pagination,
},
};
use axum::{
Expand Down Expand Up @@ -82,14 +84,14 @@ pub async fn get_product(
responses(
(status = 500, description = "An internal error, most likely related to the database, occurred."),
(status = 400, description = "The request is improperly formatted."),
(status = 200, description = "Successfully retrieved a list of products.", body = Vec<ProductResponse>)
(status = 200, description = "Successfully retrieved a list of products.", body = ProductListResponse)
)
)]
pub async fn get_all_products(
admin: Option<Admin>,
Query(pagination): Query<Pagination>,
State(conn): State<Connection>,
) -> Result<Json<Vec<ProductResponse>>, AppError> {
) -> Result<Json<ProductListResponse>, AppError> {
let page = pagination.page.unwrap_or(0);
let per_page = pagination.per_page.unwrap_or(20);

Expand All @@ -100,11 +102,19 @@ pub async fn get_all_products(
};

let result =
service::Query::list_products_with_condition(&conn, condition, page, per_page).await?;
service::Query::list_products_with_condition(&conn, condition.clone(), page, per_page)
.await?;

let result = result
let total_page =
(service::Query::count_products_with_condition(&conn, condition).await? / per_page) + 1;

let products = result
.into_iter()
.map(|x| x.try_into())
.collect::<Result<_, AppError>>()?;
Ok(Json(result))
Ok(Json(ProductListResponse {
current_page: page,
total_page,
products,
}))
}
3 changes: 2 additions & 1 deletion src/routes/utils/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ use crate::routes::utils::upload::{FileSchema, __path_post_upload_files};
schemas(FileSchema),
schemas(NewProduct),
schemas(EditProduct),
schemas(ProductResponse)
schemas(ProductResponse),
schemas(ProductListResponse)
)
)]
struct ApiDoc;
Expand Down
Loading

0 comments on commit c2cbded

Please sign in to comment.