post to the user defined table, broken push
This commit is contained in:
151
server/src/tables_data/handlers/post_table_data.rs
Normal file
151
server/src/tables_data/handlers/post_table_data.rs
Normal file
@@ -0,0 +1,151 @@
|
||||
// src/tables_data/handlers/post_table_data.rs
|
||||
use tonic::{Status, Response};
|
||||
use sqlx::{PgPool, Postgres, Arguments, Row};
|
||||
use sqlx::postgres::PgArguments;
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::collections::HashMap;
|
||||
use common::proto::multieko2::tables_data::{PostTableDataRequest, PostTableDataResponse};
|
||||
|
||||
pub async fn post_table_data(
|
||||
db_pool: &PgPool,
|
||||
request: PostTableDataRequest,
|
||||
) -> Result<Response<PostTableDataResponse>, Status> {
|
||||
let profile_name = request.profile_name;
|
||||
let table_name = request.table_name;
|
||||
let data = request.data;
|
||||
|
||||
// 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;
|
||||
|
||||
// Lookup table_definition
|
||||
let table_def = sqlx::query!(
|
||||
r#"SELECT id, columns, linked_table_id FROM table_definitions
|
||||
WHERE profile_id = $1 AND table_name = $2"#,
|
||||
profile_id,
|
||||
table_name
|
||||
)
|
||||
.fetch_optional(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Table lookup error: {}", e)))?;
|
||||
|
||||
let table_def = table_def.ok_or_else(|| Status::not_found("Table not found"))?;
|
||||
|
||||
// Parse columns from JSON
|
||||
let columns_json: Vec<String> = serde_json::from_value(table_def.columns.clone())
|
||||
.map_err(|e| Status::internal(format!("Column parsing error: {}", e)))?;
|
||||
|
||||
let mut columns = Vec::new();
|
||||
for col_def in columns_json {
|
||||
let parts: Vec<&str> = col_def.splitn(2, ' ').collect();
|
||||
if parts.len() != 2 {
|
||||
return Err(Status::internal("Invalid column format"));
|
||||
}
|
||||
let name = parts[0].trim_matches('"').to_string();
|
||||
let sql_type = parts[1].to_string();
|
||||
columns.push((name, sql_type));
|
||||
}
|
||||
|
||||
// Check required system columns
|
||||
let mut required_columns = vec!["firma".to_string()];
|
||||
if let Some(linked_table_id) = table_def.linked_table_id {
|
||||
let linked_table = sqlx::query!(
|
||||
"SELECT table_name FROM table_definitions WHERE id = $1",
|
||||
linked_table_id
|
||||
)
|
||||
.fetch_one(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Linked table error: {}", e)))?;
|
||||
|
||||
let base_name = linked_table.table_name.splitn(2, '_').last().unwrap_or(&linked_table.table_name);
|
||||
required_columns.push(format!("{}_id", base_name));
|
||||
}
|
||||
|
||||
// Validate required columns
|
||||
for col in &required_columns {
|
||||
if !data.contains_key(col) {
|
||||
return Err(Status::invalid_argument(format!("Missing required column: {}", col)));
|
||||
}
|
||||
}
|
||||
|
||||
// Validate all data columns
|
||||
let system_columns = ["firma", "deleted"];
|
||||
let user_columns: Vec<&String> = columns.iter().map(|(name, _)| name).collect();
|
||||
for key in data.keys() {
|
||||
if !system_columns.contains(&key.as_str()) && !user_columns.contains(&key) {
|
||||
return Err(Status::invalid_argument(format!("Invalid column: {}", key)));
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare SQL parameters
|
||||
let mut params = PgArguments::default();
|
||||
let mut columns_list = Vec::new();
|
||||
let mut placeholders = Vec::new();
|
||||
let mut param_idx = 1;
|
||||
|
||||
for (col, value) in data {
|
||||
let sql_type = if system_columns.contains(&col.as_str()) {
|
||||
match col.as_str() {
|
||||
"firma" => "TEXT",
|
||||
"deleted" => "BOOLEAN",
|
||||
_ => return Err(Status::invalid_argument("Invalid system column")),
|
||||
}
|
||||
} else {
|
||||
columns.iter()
|
||||
.find(|(name, _)| name == &col)
|
||||
.map(|(_, sql_type)| sql_type.as_str())
|
||||
.ok_or_else(|| Status::invalid_argument(format!("Column not found: {}", col)))?
|
||||
};
|
||||
|
||||
match sql_type {
|
||||
"TEXT" | "VARCHAR(15)" | "VARCHAR(255)" => {
|
||||
if let Some(max_len) = sql_type.strip_prefix("VARCHAR(").and_then(|s| s.strip_suffix(')')).and_then(|s| s.parse::<usize>().ok()) {
|
||||
if value.len() > max_len {
|
||||
return Err(Status::invalid_argument(format!("Value too long for {}", col)));
|
||||
}
|
||||
}
|
||||
params.add(value);
|
||||
},
|
||||
"BOOLEAN" => {
|
||||
let val = value.parse::<bool>()
|
||||
.map_err(|_| Status::invalid_argument(format!("Invalid boolean for {}", col)))?;
|
||||
params.add(val);
|
||||
},
|
||||
"TIMESTAMPTZ" => {
|
||||
let dt = DateTime::parse_from_rfc3339(&value)
|
||||
.map_err(|_| Status::invalid_argument(format!("Invalid timestamp for {}", col)))?;
|
||||
params.add(dt.with_timezone(&Utc));
|
||||
},
|
||||
_ => return Err(Status::invalid_argument(format!("Unsupported type {}", sql_type))),
|
||||
}
|
||||
|
||||
columns_list.push(format!("\"{}\"", col));
|
||||
placeholders.push(format!("${}", param_idx));
|
||||
param_idx += 1;
|
||||
}
|
||||
|
||||
let sql = format!(
|
||||
"INSERT INTO \"{}\" ({}) VALUES ({}) RETURNING id",
|
||||
table_name,
|
||||
columns_list.join(", "),
|
||||
placeholders.join(", ")
|
||||
);
|
||||
|
||||
let inserted_id: i64 = sqlx::query_scalar_with(&sql, params)
|
||||
.fetch_one(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Insert failed: {}", e)))?;
|
||||
|
||||
Ok(Response::new(PostTableDataResponse {
|
||||
success: true,
|
||||
message: "Data inserted successfully".into(),
|
||||
inserted_id,
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user