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 uuid::Uuid;
|
||||
use validator::Validate;
|
||||
|
||||
use crate::util::response::ApiErr;
|
||||
use crate::util::{response::ApiErr, pagination::{PaginatedResponse, PaginationParams}};
|
||||
use crate::{AppState};
|
||||
|
||||
/// 获取所有标签
|
||||
#[derive(Deserialize, Validate)]
|
||||
pub struct GetTagListQuery {
|
||||
#[serde(flatten)]
|
||||
pub pagination: PaginationParams,
|
||||
}
|
||||
|
||||
pub async fn get_tag_list(
|
||||
State(state): State<AppState>,
|
||||
Query(query): Query<GetTagListQuery>,
|
||||
) -> Result<impl IntoResponse, ApiErr> {
|
||||
let tags = crate::service::get_all_tags(&state.pool).await?;
|
||||
Ok(Json(tags))
|
||||
query.validate()?;
|
||||
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 相关服务函数 ====================
|
||||
|
||||
/// 获取所有标签
|
||||
pub async fn get_all_tags(
|
||||
/// 获取标签总数
|
||||
pub async fn get_tags_count(
|
||||
pool: &PgPool,
|
||||
) -> Result<Vec<crate::model::Tag>, sqlx::Error> {
|
||||
query_as::<_, crate::model::Tag>(
|
||||
r#"SELECT * FROM tag ORDER BY created_at"#
|
||||
) -> Result<i64, sqlx::Error> {
|
||||
sqlx::query_scalar::<_, i64>(
|
||||
r#"SELECT COUNT(*) FROM tag"#,
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.fetch_one(pool)
|
||||
.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获取标签
|
||||
pub async fn get_tag_by_id(
|
||||
pool: &PgPool,
|
||||
|
|
|
|||
Loading…
Reference in New Issue