From dc2c294617f19f114344d535a3a354fa5d1808f5 Mon Sep 17 00:00:00 2001 From: Lumither Tao Date: Mon, 12 May 2025 11:24:22 +0800 Subject: [PATCH] feat(blog::api) update pagination api --- .../modules/blog/api/posts/pagination/mod.rs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/backend/server/src/modules/blog/api/posts/pagination/mod.rs b/backend/server/src/modules/blog/api/posts/pagination/mod.rs index 4512ef3..afed9b0 100644 --- a/backend/server/src/modules/blog/api/posts/pagination/mod.rs +++ b/backend/server/src/modules/blog/api/posts/pagination/mod.rs @@ -4,18 +4,31 @@ use crate::{ }; use axum::{extract::State, http::StatusCode, response::Response}; -use serde::Serialize; use serde_json::{Value, json}; -use sqlx::{FromRow, PgPool, Row, query_as}; +use sqlx::{FromRow, PgPool, query_as}; -#[derive(FromRow, Serialize)] +#[derive(FromRow)] struct DBPaginationRow { page_count: i32, total_count: i64, } -impl From for Value { +struct PaginationResponse { + page_count: i32, + total_count: i64, +} + +impl From for PaginationResponse { fn from(value: DBPaginationRow) -> Self { + Self { + page_count: value.page_count, + total_count: value.total_count, + } + } +} + +impl From for Value { + fn from(value: PaginationResponse) -> Self { json!({ "page_count": value.page_count, "total_count": value.total_count @@ -36,7 +49,7 @@ pub async fn handler(State(db_connection): State) -> Response { .fetch_one(&db_connection) .await { - Ok(res) => succ_resp(StatusCode::OK, res.into()), + Ok(res) => succ_resp(StatusCode::OK, PaginationResponse::from(res).into()), Err(e) => err_resp(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), } }