feat: 为 get_tag_list 接口添加分页功能
This commit is contained in:
parent
f9284303f6
commit
d156108148
|
|
@ -1,17 +1,38 @@
|
||||||
use axum::{Json, extract::{Path, State}, http::StatusCode, response::IntoResponse};
|
use axum::{Json, extract::{Path, Query, State}, http::StatusCode, response::IntoResponse};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
use crate::util::response::ApiErr;
|
use crate::util::{response::ApiErr, pagination::{PaginatedResponse, PaginationParams}};
|
||||||
use crate::{AppState};
|
use crate::{AppState};
|
||||||
|
|
||||||
/// 获取所有标签
|
/// 获取所有标签
|
||||||
|
#[derive(Deserialize, Validate)]
|
||||||
|
pub struct GetTagListQuery {
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub pagination: PaginationParams,
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_tag_list(
|
pub async fn get_tag_list(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
|
Query(query): Query<GetTagListQuery>,
|
||||||
) -> Result<impl IntoResponse, ApiErr> {
|
) -> Result<impl IntoResponse, ApiErr> {
|
||||||
let tags = crate::service::get_all_tags(&state.pool).await?;
|
query.validate()?;
|
||||||
Ok(Json(tags))
|
let pool = &state.pool;
|
||||||
|
|
||||||
|
// 获取总数
|
||||||
|
let total = crate::service::get_tags_count(pool).await?;
|
||||||
|
|
||||||
|
// 获取分页数据
|
||||||
|
let tags = crate::service::get_tags_paginated(
|
||||||
|
pool,
|
||||||
|
query.pagination.page_size,
|
||||||
|
query.pagination.offset(),
|
||||||
|
).await?;
|
||||||
|
|
||||||
|
let response = PaginatedResponse::new(tags, total, query.pagination.page, query.pagination.page_size);
|
||||||
|
|
||||||
|
Ok(Json(response))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 获取标签下的点位信息
|
/// 获取标签下的点位信息
|
||||||
|
|
|
||||||
|
|
@ -219,17 +219,46 @@ pub async fn get_points_paginated(
|
||||||
|
|
||||||
// ==================== Tag 相关服务函数 ====================
|
// ==================== Tag 相关服务函数 ====================
|
||||||
|
|
||||||
/// 获取所有标签
|
/// 获取标签总数
|
||||||
pub async fn get_all_tags(
|
pub async fn get_tags_count(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
) -> Result<Vec<crate::model::Tag>, sqlx::Error> {
|
) -> Result<i64, sqlx::Error> {
|
||||||
query_as::<_, crate::model::Tag>(
|
sqlx::query_scalar::<_, i64>(
|
||||||
r#"SELECT * FROM tag ORDER BY created_at"#
|
r#"SELECT COUNT(*) FROM tag"#,
|
||||||
)
|
)
|
||||||
.fetch_all(pool)
|
.fetch_one(pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 获取分页标签列表
|
||||||
|
pub async fn get_tags_paginated(
|
||||||
|
pool: &PgPool,
|
||||||
|
page_size: i32,
|
||||||
|
offset: u32,
|
||||||
|
) -> Result<Vec<crate::model::Tag>, sqlx::Error> {
|
||||||
|
if page_size == 0 {
|
||||||
|
Ok(vec![])
|
||||||
|
} else if page_size == -1 {
|
||||||
|
sqlx::query_as::<_, crate::model::Tag>(
|
||||||
|
r#"SELECT * FROM tag ORDER BY created_at"#,
|
||||||
|
)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
} else {
|
||||||
|
sqlx::query_as::<_, crate::model::Tag>(
|
||||||
|
r#"
|
||||||
|
SELECT * FROM tag
|
||||||
|
ORDER BY created_at
|
||||||
|
LIMIT $1 OFFSET $2
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(page_size)
|
||||||
|
.bind(offset as i64)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 根据ID获取标签
|
/// 根据ID获取标签
|
||||||
pub async fn get_tag_by_id(
|
pub async fn get_tag_by_id(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue