working test passing
This commit is contained in:
@@ -4,6 +4,7 @@ use sqlx::{PgPool, Arguments, Postgres};
|
||||
use sqlx::postgres::PgArguments;
|
||||
use chrono::{DateTime, Utc};
|
||||
use common::proto::multieko2::tables_data::{PutTableDataRequest, PutTableDataResponse};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub async fn put_table_data(
|
||||
db_pool: &PgPool,
|
||||
@@ -12,9 +13,27 @@ pub async fn put_table_data(
|
||||
let profile_name = request.profile_name;
|
||||
let table_name = request.table_name;
|
||||
let record_id = request.id;
|
||||
let data = request.data;
|
||||
|
||||
// Preprocess and validate data
|
||||
let mut processed_data = HashMap::new();
|
||||
let mut null_fields = Vec::new();
|
||||
|
||||
for (key, value) in request.data {
|
||||
let trimmed = value.trim().to_string();
|
||||
|
||||
if key == "firma" && trimmed.is_empty() {
|
||||
return Err(Status::invalid_argument("Firma cannot be empty"));
|
||||
}
|
||||
|
||||
// Store fields that should be set to NULL
|
||||
if key != "firma" && trimmed.is_empty() {
|
||||
null_fields.push(key);
|
||||
} else {
|
||||
processed_data.insert(key, trimmed);
|
||||
}
|
||||
}
|
||||
|
||||
// Lookup profile (same as POST)
|
||||
// Lookup profile
|
||||
let profile = sqlx::query!(
|
||||
"SELECT id FROM profiles WHERE name = $1",
|
||||
profile_name
|
||||
@@ -25,7 +44,7 @@ pub async fn put_table_data(
|
||||
|
||||
let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id;
|
||||
|
||||
// Lookup table_definition (same as POST)
|
||||
// Lookup table_definition
|
||||
let table_def = sqlx::query!(
|
||||
r#"SELECT id, columns FROM table_definitions
|
||||
WHERE profile_id = $1 AND table_name = $2"#,
|
||||
@@ -38,7 +57,7 @@ pub async fn put_table_data(
|
||||
|
||||
let table_def = table_def.ok_or_else(|| Status::not_found("Table not found"))?;
|
||||
|
||||
// Parse columns from JSON (same as POST)
|
||||
// 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)))?;
|
||||
|
||||
@@ -58,7 +77,7 @@ pub async fn put_table_data(
|
||||
let user_columns: Vec<&String> = columns.iter().map(|(name, _)| name).collect();
|
||||
|
||||
// Validate input columns
|
||||
for key in data.keys() {
|
||||
for key in processed_data.keys() {
|
||||
if !system_columns.contains(&key.as_str()) && !user_columns.contains(&key) {
|
||||
return Err(Status::invalid_argument(format!("Invalid column: {}", key)));
|
||||
}
|
||||
@@ -69,8 +88,8 @@ pub async fn put_table_data(
|
||||
let mut set_clauses = Vec::new();
|
||||
let mut param_idx = 1;
|
||||
|
||||
// Add data parameters
|
||||
for (col, value) in &data {
|
||||
// Add data parameters for non-empty fields
|
||||
for (col, value) in &processed_data {
|
||||
let sql_type = if system_columns.contains(&col.as_str()) {
|
||||
match col.as_str() {
|
||||
"firma" => "TEXT",
|
||||
@@ -91,7 +110,7 @@ pub async fn put_table_data(
|
||||
.and_then(|s| s.parse::<usize>().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)
|
||||
@@ -116,6 +135,20 @@ pub async fn put_table_data(
|
||||
param_idx += 1;
|
||||
}
|
||||
|
||||
// Add NULL clauses for empty fields
|
||||
for field in null_fields {
|
||||
// Make sure the field is valid
|
||||
if !system_columns.contains(&field.as_str()) && !user_columns.contains(&&field) {
|
||||
return Err(Status::invalid_argument(format!("Invalid column to set NULL: {}", field)));
|
||||
}
|
||||
set_clauses.push(format!("\"{}\" = NULL", field));
|
||||
}
|
||||
|
||||
// Ensure we have at least one field to update
|
||||
if set_clauses.is_empty() {
|
||||
return Err(Status::invalid_argument("No valid fields to update"));
|
||||
}
|
||||
|
||||
// Add ID parameter at the end
|
||||
params.add(record_id)
|
||||
.map_err(|e| Status::internal(format!("Failed to add record_id parameter: {}", e)))?;
|
||||
|
||||
Reference in New Issue
Block a user