also get tree added, not working, breaks everything

This commit is contained in:
filipriec
2025-03-01 20:10:23 +01:00
parent 4070348bee
commit e53ed7bdc0
7 changed files with 191 additions and 5 deletions

View File

@@ -4,8 +4,10 @@ use common::proto::multieko2::table_definition::{
table_definition_server::TableDefinition,
PostTableDefinitionRequest, TableDefinitionResponse
};
use common::proto::multieko2::table_definition::ProfileTreeResponse;
use sqlx::PgPool;
use crate::table_definition::handlers::post_table_definition;
use crate::table_definition::handlers::get_profile_tree;
#[derive(Debug)]
pub struct TableDefinitionService {
@@ -21,4 +23,10 @@ impl TableDefinition for TableDefinitionService {
let response = post_table_definition(&self.db_pool, request.into_inner()).await?;
Ok(Response::new(response))
}
async fn get_profile_tree(
&self,
request: Request<()>,
) -> Result<Response<ProfileTreeResponse>, Status> {
get_profile_tree::get_profile_tree(&self.db_pool, request).await
}
}

View File

@@ -1,8 +1,6 @@
// src/table_definition/handlers.rs
pub mod post_table_definition;
// pub mod get_table_definition;
// pub mod list_table_definitions;
pub mod get_profile_tree;
pub use post_table_definition::post_table_definition;
// pub use get_table_definition::get_table_definition;
// pub use list_table_definitions::list_table_definitions;
pub use get_profile_tree::get_profile_tree;

View File

@@ -0,0 +1,61 @@
// server/src/table_definition/handlers/get_profile_tree.rs
use tonic::{Request, Response, Status};
use sqlx::PgPool;
use common::proto::multieko2::table_definition::ProfileTreeResponse;
pub async fn get_profile_tree(
db_pool: &PgPool,
_request: Request<()>,
) -> Result<Response<ProfileTreeResponse>, Status> {
let mut profiles = Vec::new();
// Get all profiles
let profile_records = sqlx::query!("SELECT id, name FROM profiles")
.fetch_all(db_pool)
.await
.map_err(|e| Status::internal(format!("Failed to fetch profiles: {}", e)))?;
for profile in profile_records {
// Get all tables with their dependencies
let tables = sqlx::query!(
r#"
SELECT
td.table_name,
ltd.table_name as "linked_table_name?"
FROM table_definitions td
LEFT JOIN table_definitions ltd ON td.linked_table_id = ltd.id
WHERE td.profile_id = $1
"#,
profile.id
)
.fetch_all(db_pool)
.await
.map_err(|e| Status::internal(format!("Failed to fetch tables: {}", e)))?;
// Group dependencies per table
let mut table_map = std::collections::HashMap::new();
for table in tables {
let entry = table_map.entry(table.table_name)
.or_insert(Vec::new());
if let Some(linked) = table.linked_table_name {
entry.push(linked);
}
}
// Convert to protobuf format
let proto_tables = table_map.into_iter()
.map(|(name, depends_on)| ProfileTreeResponse::Table {
name,
depends_on
})
.collect();
profiles.push(ProfileTreeResponse::Profile {
name: profile.name,
tables: proto_tables
});
}
Ok(Response::new(ProfileTreeResponse { profiles }))
}