diff --git a/server/src/tables_data/handlers/post_table_data.rs b/server/src/tables_data/handlers/post_table_data.rs index f306ce0..6bdfa0a 100644 --- a/server/src/tables_data/handlers/post_table_data.rs +++ b/server/src/tables_data/handlers/post_table_data.rs @@ -4,6 +4,7 @@ use sqlx::{PgPool, Arguments}; use sqlx::postgres::PgArguments; use chrono::{DateTime, Utc}; use common::proto::multieko2::tables_data::{PostTableDataRequest, PostTableDataResponse}; +use std::collections::HashMap; pub async fn post_table_data( db_pool: &PgPool, @@ -11,7 +12,23 @@ pub async fn post_table_data( ) -> Result { let profile_name = request.profile_name; let table_name = request.table_name; - let data = request.data; + let mut data = HashMap::new(); + + // Process and validate all data values + for (key, value) in request.data { + let trimmed = value.trim().to_string(); + + // Handle firma specially - it cannot be empty + if key == "firma" && trimmed.is_empty() { + return Err(Status::invalid_argument("Firma cannot be empty")); + } + + // Add trimmed non-empty values to data map + // Empty optional fields will be skipped in SQL generation + if !trimmed.is_empty() || key == "firma" { + data.insert(key, trimmed); + } + } // Lookup profile let profile = sqlx::query!( @@ -110,7 +127,7 @@ pub async fn post_table_data( .and_then(|s| s.parse::().ok()) { if value.len() > max_len { - return Err(Status::invalid_argument(format!("Value too long for {}", col))); + return Err(Status::internal(format!("Value too long for {}", col))); } } params.add(value) @@ -136,6 +153,11 @@ pub async fn post_table_data( param_idx += 1; } + // Ensure we have at least one column to insert + if columns_list.is_empty() { + return Err(Status::invalid_argument("No valid columns to insert")); + } + let sql = format!( "INSERT INTO \"{}\" ({}) VALUES ({}) RETURNING id", table_name,