refactor: 优化点位列表查询功能
This commit is contained in:
parent
fdb4f10ba4
commit
606b57eb73
|
|
@ -4,7 +4,7 @@ use uuid::Uuid;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
use sqlx::Row;
|
use sqlx::Row;
|
||||||
|
|
||||||
use crate::util::response::ApiErr;
|
use crate::util::{response::ApiErr, pagination::{PaginatedResponse, PaginationParams}};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
AppState,
|
AppState,
|
||||||
|
|
@ -12,9 +12,11 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
/// List all points.
|
/// List all points.
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Validate)]
|
||||||
pub struct GetPointListQuery {
|
pub struct GetPointListQuery {
|
||||||
pub source_id: Option<Uuid>,
|
pub source_id: Option<Uuid>,
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub pagination: PaginationParams,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
|
@ -28,37 +30,26 @@ pub async fn get_point_list(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Query(query): Query<GetPointListQuery>,
|
Query(query): Query<GetPointListQuery>,
|
||||||
) -> Result<impl IntoResponse, ApiErr> {
|
) -> Result<impl IntoResponse, ApiErr> {
|
||||||
|
query.validate()?;
|
||||||
let pool = &state.pool;
|
let pool = &state.pool;
|
||||||
let points: Vec<Point> = match query.source_id {
|
|
||||||
Some(source_id) => {
|
// 获取总数
|
||||||
sqlx::query_as::<_, Point>(
|
let total = crate::service::get_points_count(pool, query.source_id).await?;
|
||||||
r#"
|
|
||||||
SELECT p.*
|
// 获取分页数据
|
||||||
FROM point p
|
let points = crate::service::get_points_paginated(
|
||||||
INNER JOIN node n ON p.node_id = n.id
|
pool,
|
||||||
WHERE n.source_id = $1
|
query.source_id,
|
||||||
ORDER BY p.created_at
|
query.pagination.page_size,
|
||||||
"#,
|
query.pagination.offset(),
|
||||||
)
|
).await?;
|
||||||
.bind(source_id)
|
|
||||||
.fetch_all(pool)
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
sqlx::query_as::<_, Point>(
|
|
||||||
r#"SELECT * FROM point ORDER BY created_at"#,
|
|
||||||
)
|
|
||||||
.fetch_all(pool)
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let monitor_guard = state
|
let monitor_guard = state
|
||||||
.connection_manager
|
.connection_manager
|
||||||
.get_point_monitor_data_read_guard()
|
.get_point_monitor_data_read_guard()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let resp: Vec<PointWithMonitor> = points
|
let data: Vec<PointWithMonitor> = points
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|point| {
|
.map(|point| {
|
||||||
let point_monitor = monitor_guard
|
let point_monitor = monitor_guard
|
||||||
|
|
@ -68,7 +59,9 @@ pub async fn get_point_list(
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(Json(resp))
|
let response = PaginatedResponse::new(data, total, query.pagination.page, query.pagination.page_size);
|
||||||
|
|
||||||
|
Ok(Json(response))
|
||||||
}
|
}
|
||||||
/// Get a point by id.
|
/// Get a point by id.
|
||||||
pub async fn get_point(
|
pub async fn get_point(
|
||||||
|
|
|
||||||
102
src/service.rs
102
src/service.rs
|
|
@ -115,6 +115,108 @@ pub async fn get_points_with_ids(
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==================== Point 相关服务函数 ====================
|
||||||
|
|
||||||
|
/// 获取点位总数
|
||||||
|
pub async fn get_points_count(
|
||||||
|
pool: &PgPool,
|
||||||
|
source_id: Option<uuid::Uuid>,
|
||||||
|
) -> Result<i64, sqlx::Error> {
|
||||||
|
match source_id {
|
||||||
|
Some(source_id) => {
|
||||||
|
sqlx::query_scalar::<_, i64>(
|
||||||
|
r#"
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM point p
|
||||||
|
INNER JOIN node n ON p.node_id = n.id
|
||||||
|
WHERE n.source_id = $1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(source_id)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
sqlx::query_scalar::<_, i64>(
|
||||||
|
r#"SELECT COUNT(*) FROM point"#,
|
||||||
|
)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 获取分页点位列表
|
||||||
|
pub async fn get_points_paginated(
|
||||||
|
pool: &PgPool,
|
||||||
|
source_id: Option<uuid::Uuid>,
|
||||||
|
page_size: i32,
|
||||||
|
offset: u32,
|
||||||
|
) -> Result<Vec<crate::model::Point>, sqlx::Error> {
|
||||||
|
match source_id {
|
||||||
|
Some(source_id) => {
|
||||||
|
if page_size == 0 {
|
||||||
|
Ok(vec![])
|
||||||
|
} else if page_size == -1 {
|
||||||
|
sqlx::query_as::<_, crate::model::Point>(
|
||||||
|
r#"
|
||||||
|
SELECT p.*
|
||||||
|
FROM point p
|
||||||
|
INNER JOIN node n ON p.node_id = n.id
|
||||||
|
WHERE n.source_id = $1
|
||||||
|
ORDER BY p.created_at
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(source_id)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
} else {
|
||||||
|
sqlx::query_as::<_, crate::model::Point>(
|
||||||
|
r#"
|
||||||
|
SELECT p.*
|
||||||
|
FROM point p
|
||||||
|
INNER JOIN node n ON p.node_id = n.id
|
||||||
|
WHERE n.source_id = $1
|
||||||
|
ORDER BY p.created_at
|
||||||
|
LIMIT $2 OFFSET $3
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(source_id)
|
||||||
|
.bind(page_size as i64)
|
||||||
|
.bind(offset as i64)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
if page_size == 0 {
|
||||||
|
Ok(vec![])
|
||||||
|
} else if page_size == -1 {
|
||||||
|
sqlx::query_as::<_, crate::model::Point>(
|
||||||
|
r#"
|
||||||
|
SELECT * FROM point
|
||||||
|
ORDER BY created_at
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
} else {
|
||||||
|
sqlx::query_as::<_, crate::model::Point>(
|
||||||
|
r#"
|
||||||
|
SELECT * FROM point
|
||||||
|
ORDER BY created_at
|
||||||
|
LIMIT $1 OFFSET $2
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(page_size as i64)
|
||||||
|
.bind(offset as i64)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== Tag 相关服务函数 ====================
|
// ==================== Tag 相关服务函数 ====================
|
||||||
|
|
||||||
/// 获取所有标签
|
/// 获取所有标签
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue