working get count endpoint
This commit is contained in:
61
server/src/tables_data/handlers/get_table_data_count.rs
Normal file
61
server/src/tables_data/handlers/get_table_data_count.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
// src/tables_data/handlers/get_table_data_count.rs
|
||||
use tonic::Status;
|
||||
use sqlx::PgPool;
|
||||
use common::proto::multieko2::common::{CountResponse, Empty};
|
||||
use common::proto::multieko2::tables_data::GetTableDataCountRequest;
|
||||
|
||||
pub async fn get_table_data_count(
|
||||
db_pool: &PgPool,
|
||||
request: GetTableDataCountRequest,
|
||||
) -> Result<CountResponse, Status> {
|
||||
let profile_name = request.profile_name;
|
||||
let table_name = request.table_name;
|
||||
|
||||
// Lookup profile
|
||||
let profile = sqlx::query!(
|
||||
"SELECT id FROM profiles WHERE name = $1",
|
||||
profile_name
|
||||
)
|
||||
.fetch_optional(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?;
|
||||
|
||||
let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id;
|
||||
|
||||
// Verify table exists and belongs to profile
|
||||
let table_exists = sqlx::query!(
|
||||
r#"SELECT EXISTS(
|
||||
SELECT 1 FROM table_definitions
|
||||
WHERE profile_id = $1 AND table_name = $2
|
||||
)"#,
|
||||
profile_id,
|
||||
table_name
|
||||
)
|
||||
.fetch_one(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Table verification error: {}", e)))?
|
||||
.exists
|
||||
.unwrap_or(false);
|
||||
|
||||
if !table_exists {
|
||||
return Err(Status::not_found("Table not found"));
|
||||
}
|
||||
|
||||
// Get count of non-deleted records
|
||||
let query = format!(
|
||||
r#"
|
||||
SELECT COUNT(*) AS count
|
||||
FROM "{}"
|
||||
WHERE deleted = FALSE
|
||||
"#,
|
||||
table_name
|
||||
);
|
||||
|
||||
let count: i64 = sqlx::query_scalar::<_, Option<i64>>(&query)
|
||||
.fetch_one(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Count query failed: {}", e)))?
|
||||
.unwrap_or(0);
|
||||
|
||||
Ok(CountResponse { count })
|
||||
}
|
||||
Reference in New Issue
Block a user