general endpoint delete
This commit is contained in:
@@ -3,9 +3,10 @@ use tonic::{Request, Response, Status};
|
||||
use common::proto::multieko2::tables_data::tables_data_server::TablesData;
|
||||
use common::proto::multieko2::tables_data::{
|
||||
PostTableDataRequest, PostTableDataResponse,
|
||||
PutTableDataRequest, PutTableDataResponse // Add this import
|
||||
PutTableDataRequest, PutTableDataResponse,
|
||||
DeleteTableDataRequest, DeleteTableDataResponse,
|
||||
};
|
||||
use crate::tables_data::handlers::{post_table_data, put_table_data}; // Add put_table_data
|
||||
use crate::tables_data::handlers::{post_table_data, put_table_data, delete_table_data,};
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -33,4 +34,13 @@ impl TablesData for TablesDataService {
|
||||
let response = put_table_data(&self.db_pool, request).await?;
|
||||
Ok(Response::new(response))
|
||||
}
|
||||
|
||||
async fn delete_table_data(
|
||||
&self,
|
||||
request: Request<DeleteTableDataRequest>,
|
||||
) -> Result<Response<DeleteTableDataResponse>, Status> {
|
||||
let request = request.into_inner();
|
||||
let response = delete_table_data(&self.db_pool, request).await?;
|
||||
Ok(Response::new(response))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// server/src/tables_data/handlers.rs
|
||||
pub mod post_table_data;
|
||||
pub mod put_table_data;
|
||||
pub mod delete_table_data;
|
||||
|
||||
pub use post_table_data::post_table_data;
|
||||
pub use put_table_data::put_table_data;
|
||||
pub use delete_table_data::delete_table_data;
|
||||
|
||||
57
server/src/tables_data/handlers/delete_table_data.rs
Normal file
57
server/src/tables_data/handlers/delete_table_data.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
// src/tables_data/handlers/delete_table_data.rs
|
||||
use tonic::Status;
|
||||
use sqlx::PgPool;
|
||||
use common::proto::multieko2::tables_data::{DeleteTableDataRequest, DeleteTableDataResponse};
|
||||
|
||||
pub async fn delete_table_data(
|
||||
db_pool: &PgPool,
|
||||
request: DeleteTableDataRequest,
|
||||
) -> Result<DeleteTableDataResponse, Status> {
|
||||
// Lookup profile
|
||||
let profile = sqlx::query!(
|
||||
"SELECT id FROM profiles WHERE name = $1",
|
||||
request.profile_name
|
||||
)
|
||||
.fetch_optional(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?;
|
||||
|
||||
let profile_id = match profile {
|
||||
Some(p) => p.id,
|
||||
None => return Err(Status::not_found("Profile not found")),
|
||||
};
|
||||
|
||||
// Verify table exists in profile
|
||||
let table_exists = sqlx::query!(
|
||||
"SELECT 1 AS exists FROM table_definitions
|
||||
WHERE profile_id = $1 AND table_name = $2",
|
||||
profile_id,
|
||||
request.table_name
|
||||
)
|
||||
.fetch_optional(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Table verification error: {}", e)))?;
|
||||
|
||||
if table_exists.is_none() {
|
||||
return Err(Status::not_found("Table not found in profile"));
|
||||
}
|
||||
|
||||
// Perform soft delete
|
||||
let query = format!(
|
||||
"UPDATE \"{}\"
|
||||
SET deleted = true
|
||||
WHERE id = $1 AND deleted = false",
|
||||
request.table_name
|
||||
);
|
||||
|
||||
let rows_affected = sqlx::query(&query)
|
||||
.bind(request.record_id)
|
||||
.execute(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Delete operation failed: {}", e)))?
|
||||
.rows_affected();
|
||||
|
||||
Ok(DeleteTableDataResponse {
|
||||
success: rows_affected > 0,
|
||||
})
|
||||
}
|
||||
@@ -252,63 +252,3 @@ async fn test_update_table_data_clear_optional_fields(
|
||||
assert!(db_record.telefon.is_none());
|
||||
assert!(db_record.ulica.is_none());
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_update_table_data_max_length_fields(
|
||||
#[future] existing_record: (PgPool, i64),
|
||||
valid_request_template: HashMap<String, String>,
|
||||
) {
|
||||
let (pool, id) = existing_record.await;
|
||||
|
||||
let mut data = valid_request_template;
|
||||
data.insert("firma".into(), "a".repeat(255));
|
||||
data.insert("telefon".into(), "1".repeat(20));
|
||||
|
||||
let request = PutTableDataRequest {
|
||||
profile_name: "default".into(),
|
||||
table_name: "2025_adresar".into(),
|
||||
id,
|
||||
data,
|
||||
};
|
||||
|
||||
let _response = put_table_data(&pool, request).await.unwrap();
|
||||
|
||||
let db_record = sqlx::query!(r#"SELECT firma, telefon FROM "2025_adresar" WHERE id = $1"#, id)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(db_record.firma.len(), 255);
|
||||
assert_eq!(db_record.telefon.unwrap().len(), 20);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_update_table_data_special_characters(
|
||||
#[future] existing_record: (PgPool, i64),
|
||||
valid_request_template: HashMap<String, String>,
|
||||
) {
|
||||
let (pool, id) = existing_record.await;
|
||||
|
||||
let mut data = valid_request_template;
|
||||
data.insert("ulica".into(), "Náměstí 28. října".into());
|
||||
data.insert("telefon".into(), "+420 123-456.789".into());
|
||||
|
||||
let request = PutTableDataRequest {
|
||||
profile_name: "default".into(),
|
||||
table_name: "2025_adresar".into(),
|
||||
id,
|
||||
data,
|
||||
};
|
||||
|
||||
let _response = put_table_data(&pool, request).await.unwrap();
|
||||
|
||||
let db_record = sqlx::query!(r#"SELECT ulica, telefon FROM "2025_adresar" WHERE id = $1"#, id)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(db_record.ulica.unwrap(), "Náměstí 28. října");
|
||||
assert_eq!(db_record.telefon.unwrap(), "+420 123-456.789");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user