From da62f13714c14cbecb01fcaa5ed8493ff67b801a Mon Sep 17 00:00:00 2001 From: flo-ride <43076999+flo-ride@users.noreply.github.com> Date: Sat, 25 Jan 2025 16:23:57 +0100 Subject: [PATCH] fix(get): Fix miscalculated total_page on get_all_x requests --- api/src/location/get.rs | 4 ++-- api/src/product/get.rs | 4 ++-- api/src/recipe/get.rs | 4 ++-- api/src/refill/get.rs | 4 ++-- api/src/user/get.rs | 4 ++-- api/src/warehouse/get.rs | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/api/src/location/get.rs b/api/src/location/get.rs index 2d33736..23fc063 100644 --- a/api/src/location/get.rs +++ b/api/src/location/get.rs @@ -117,8 +117,8 @@ pub async fn get_all_locations( service::Query::list_locations_with_condition(&conn, filter.clone(), sort, page, per_page) .await?; - let total_page = - (service::Query::count_locations_with_condition(&conn, filter).await? / per_page) + 1; + let total_locations = service::Query::count_locations_with_condition(&conn, filter).await?; + let total_page = ((total_locations - 1) / per_page) + 1; let locations = result.into_iter().map(Into::into).collect(); Ok(Json(LocationListResponse { diff --git a/api/src/product/get.rs b/api/src/product/get.rs index 9e692d8..0bf2370 100644 --- a/api/src/product/get.rs +++ b/api/src/product/get.rs @@ -117,8 +117,8 @@ pub async fn get_all_products( service::Query::list_products_with_condition(&conn, filter.clone(), sort, page, per_page) .await?; - let total_page = - (service::Query::count_products_with_condition(&conn, filter).await? / per_page) + 1; + let total_products = service::Query::count_products_with_condition(&conn, filter).await?; + let total_page = ((total_products.max(1) - 1) / per_page) + 1; let products = result .into_iter() diff --git a/api/src/recipe/get.rs b/api/src/recipe/get.rs index 6507bcf..7b611f4 100644 --- a/api/src/recipe/get.rs +++ b/api/src/recipe/get.rs @@ -100,8 +100,8 @@ pub async fn get_all_recipes( service::Query::list_recipes_with_condition(&conn, filter.clone(), sort, page, per_page) .await?; - let total_page = - (service::Query::count_recipes_with_condition(&conn, filter).await? / per_page) + 1; + let total_recipes = service::Query::count_recipes_with_condition(&conn, filter).await?; + let total_page = ((total_recipes.max(1) - 1) / per_page) + 1; let recipes = result .into_iter() diff --git a/api/src/refill/get.rs b/api/src/refill/get.rs index 880dfba..e54de1a 100644 --- a/api/src/refill/get.rs +++ b/api/src/refill/get.rs @@ -116,8 +116,8 @@ pub async fn get_all_refills( service::Query::list_refills_with_condition(&conn, filter.clone(), sort, page, per_page) .await?; - let total_page = - (service::Query::count_refills_with_condition(&conn, filter).await? / per_page) + 1; + let total_refills = service::Query::count_refills_with_condition(&conn, filter).await?; + let total_page = ((total_refills.max(1) - 1) / per_page) + 1; let refills: Result, RefillResponseError> = result.into_iter().map(TryInto::try_into).collect(); diff --git a/api/src/user/get.rs b/api/src/user/get.rs index e6a7260..3d906ad 100644 --- a/api/src/user/get.rs +++ b/api/src/user/get.rs @@ -113,8 +113,8 @@ pub async fn get_all_users( service::Query::list_users_with_condition(&conn, filter.clone(), sort, page, per_page) .await?; - let total_page = - (service::Query::count_users_with_condition(&conn, filter).await? / per_page) + 1; + let total_users = service::Query::count_users_with_condition(&conn, filter).await?; + let total_page = ((total_users.max(1) - 1) / per_page) + 1; let users = result.into_iter().map(|x| x.into()).collect(); Ok(Json(UserListResponse { diff --git a/api/src/warehouse/get.rs b/api/src/warehouse/get.rs index d2fb513..c389125 100644 --- a/api/src/warehouse/get.rs +++ b/api/src/warehouse/get.rs @@ -100,8 +100,8 @@ pub async fn get_all_warehouses( service::Query::list_warehouses_with_condition(&conn, filter.clone(), sort, page, per_page) .await?; - let total_page = - (service::Query::count_warehouses_with_condition(&conn, filter).await? / per_page) + 1; + let total_warehouses = service::Query::count_warehouses_with_condition(&conn, filter).await?; + let total_page = ((total_warehouses.max(1) - 1) / per_page) + 1; let warehouses = result.into_iter().map(Into::into).collect(); Ok(Json(WarehouseListResponse {