fixed post request now we are not failing any tests

This commit is contained in:
filipriec
2025-03-04 14:55:33 +01:00
parent b2e29eb8c8
commit bbda6ea1c8

View File

@@ -4,6 +4,7 @@ use sqlx::{PgPool, Arguments};
use sqlx::postgres::PgArguments; use sqlx::postgres::PgArguments;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use common::proto::multieko2::tables_data::{PostTableDataRequest, PostTableDataResponse}; use common::proto::multieko2::tables_data::{PostTableDataRequest, PostTableDataResponse};
use std::collections::HashMap;
pub async fn post_table_data( pub async fn post_table_data(
db_pool: &PgPool, db_pool: &PgPool,
@@ -11,7 +12,23 @@ pub async fn post_table_data(
) -> Result<PostTableDataResponse, Status> { ) -> Result<PostTableDataResponse, Status> {
let profile_name = request.profile_name; let profile_name = request.profile_name;
let table_name = request.table_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 // Lookup profile
let profile = sqlx::query!( let profile = sqlx::query!(
@@ -110,7 +127,7 @@ pub async fn post_table_data(
.and_then(|s| s.parse::<usize>().ok()) .and_then(|s| s.parse::<usize>().ok())
{ {
if value.len() > max_len { 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) params.add(value)
@@ -136,6 +153,11 @@ pub async fn post_table_data(
param_idx += 1; 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!( let sql = format!(
"INSERT INTO \"{}\" ({}) VALUES ({}) RETURNING id", "INSERT INTO \"{}\" ({}) VALUES ({}) RETURNING id",
table_name, table_name,