put uctovnictvo is now working
This commit is contained in:
@@ -10,6 +10,7 @@ service Uctovnictvo {
|
||||
rpc GetUctovnictvo (GetUctovnictvoRequest) returns (UctovnictvoResponse);
|
||||
rpc GetUctovnictvoCount (multieko2.adresar.Empty) returns (multieko2.adresar.CountResponse);
|
||||
rpc GetUctovnictvoByPosition (multieko2.adresar.PositionRequest) returns (UctovnictvoResponse);
|
||||
rpc PutUctovnictvo (PutUctovnictvoRequest) returns (UctovnictvoResponse);
|
||||
}
|
||||
|
||||
message PostUctovnictvoRequest {
|
||||
@@ -41,6 +42,21 @@ message UctovnictvoResponse {
|
||||
string firma = 12;
|
||||
}
|
||||
|
||||
message PutUctovnictvoRequest {
|
||||
int64 id = 1;
|
||||
int64 adresar_id = 2;
|
||||
string c_dokladu = 3;
|
||||
string datum = 4;
|
||||
string c_faktury = 5;
|
||||
string obsah = 6;
|
||||
string stredisko = 7;
|
||||
string c_uctu = 8;
|
||||
string md = 9;
|
||||
string identif = 10;
|
||||
string poznanka = 11;
|
||||
string firma = 12;
|
||||
}
|
||||
|
||||
message GetUctovnictvoRequest {
|
||||
int64 id = 1;
|
||||
}
|
||||
|
||||
@@ -5,3 +5,4 @@ pub mod server;
|
||||
pub mod proto;
|
||||
pub mod adresar;
|
||||
pub mod uctovnictvo;
|
||||
pub mod shared;
|
||||
|
||||
Binary file not shown.
@@ -4,7 +4,7 @@ use tonic_reflection::server::Builder as ReflectionBuilder;
|
||||
use crate::adresar::handlers::{
|
||||
post_adresar, get_adresar, put_adresar, delete_adresar, get_adresar_count, get_adresar_by_position, get_table_structure
|
||||
};
|
||||
use crate::uctovnictvo::handlers::{post_uctovnictvo, get_uctovnictvo, get_uctovnictvo_count, get_uctovnictvo_by_position};
|
||||
use crate::uctovnictvo::handlers::{post_uctovnictvo, get_uctovnictvo, get_uctovnictvo_count, get_uctovnictvo_by_position, put_uctovnictvo,};
|
||||
use crate::proto::multieko2::{
|
||||
FILE_DESCRIPTOR_SET,
|
||||
};
|
||||
@@ -16,7 +16,7 @@ use crate::proto::multieko2::adresar::{
|
||||
};
|
||||
use crate::proto::multieko2::uctovnictvo::{
|
||||
uctovnictvo_server::{Uctovnictvo, UctovnictvoServer},
|
||||
PostUctovnictvoRequest, UctovnictvoResponse, GetUctovnictvoRequest,
|
||||
PostUctovnictvoRequest, UctovnictvoResponse, GetUctovnictvoRequest, PutUctovnictvoRequest,
|
||||
};
|
||||
|
||||
pub struct AdresarService {
|
||||
@@ -120,6 +120,14 @@ impl Uctovnictvo for UctovnictvoService {
|
||||
let response = get_uctovnictvo_by_position(&self.db_pool, request.into_inner()).await?;
|
||||
Ok(Response::new(response))
|
||||
}
|
||||
|
||||
async fn put_uctovnictvo(
|
||||
&self,
|
||||
request: Request<PutUctovnictvoRequest>,
|
||||
) -> Result<Response<UctovnictvoResponse>, Status> {
|
||||
let response = put_uctovnictvo(&self.db_pool, request.into_inner()).await?;
|
||||
Ok(Response::new(response))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
34
src/shared/date_utils.rs
Normal file
34
src/shared/date_utils.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
// src/shared/date_utils.rs
|
||||
use chrono::NaiveDate;
|
||||
|
||||
/// Attempts to parse the date string with multiple commonly used formats in Slovakia
|
||||
pub fn parse_date_with_multiple_formats(date_str: &str) -> Option<NaiveDate> {
|
||||
// Define common date formats used in Slovakia
|
||||
let formats = [
|
||||
"%d:%m:%Y", // Original format: 01:01:2023
|
||||
"%d.%m.%Y", // Standard Slovak format: 01.01.2023
|
||||
"%d-%m-%Y", // Dash format: 01-01-2023
|
||||
"%d/%m/%Y", // Slash format: 01/01/2023
|
||||
"%Y-%m-%d", // ISO format: 2023-01-01
|
||||
"%Y/%m/%d", // Alternative ISO: 2023/01/01
|
||||
"%Y.%m.%d", // Dot ISO: 2023.01.01
|
||||
"%d.%m.%y", // Short year with dot: 01.01.23
|
||||
"%d-%m-%y", // Short year with dash: 01-01-23
|
||||
"%d/%m/%y", // Short year with slash: 01/01/23
|
||||
];
|
||||
|
||||
// Try each format until one works
|
||||
for format in formats {
|
||||
if let Ok(date) = NaiveDate::parse_from_str(date_str, format) {
|
||||
return Some(date);
|
||||
}
|
||||
}
|
||||
|
||||
// If none of the formats worked, try to handle potential whitespace issues
|
||||
let trimmed = date_str.trim();
|
||||
if trimmed != date_str {
|
||||
return parse_date_with_multiple_formats(trimmed);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
2
src/shared/mod.rs
Normal file
2
src/shared/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
// src/shared/mod.rs
|
||||
pub mod date_utils;
|
||||
@@ -3,8 +3,10 @@ pub mod post_uctovnictvo;
|
||||
pub mod get_uctovnictvo;
|
||||
pub mod get_uctovnictvo_count;
|
||||
pub mod get_uctovnictvo_by_position;
|
||||
pub mod put_uctovnictvo;
|
||||
|
||||
pub use post_uctovnictvo::post_uctovnictvo;
|
||||
pub use get_uctovnictvo::get_uctovnictvo;
|
||||
pub use get_uctovnictvo_count::get_uctovnictvo_count;
|
||||
pub use get_uctovnictvo_by_position::get_uctovnictvo_by_position;
|
||||
pub use put_uctovnictvo::put_uctovnictvo;
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
// src/uctovnictvo/handlers/post_uctovnictvo.rs
|
||||
use tonic::Status;
|
||||
use sqlx::PgPool;
|
||||
use chrono::NaiveDate;
|
||||
use crate::uctovnictvo::models::Uctovnictvo;
|
||||
use crate::proto::multieko2::uctovnictvo::{PostUctovnictvoRequest, UctovnictvoResponse};
|
||||
use crate::shared::date_utils::parse_date_with_multiple_formats; // Import from shared module
|
||||
|
||||
pub async fn post_uctovnictvo(
|
||||
db_pool: &PgPool,
|
||||
request: PostUctovnictvoRequest,
|
||||
) -> Result<UctovnictvoResponse, Status> {
|
||||
// Try multiple date formats commonly used in Slovakia
|
||||
let datum = parse_date_with_multiple_formats(&request.datum)
|
||||
.ok_or_else(|| Status::invalid_argument(format!("Invalid date format: {}", request.datum)))?;
|
||||
|
||||
@@ -72,36 +71,3 @@ pub async fn post_uctovnictvo(
|
||||
firma: uctovnictvo.firma,
|
||||
})
|
||||
}
|
||||
|
||||
/// TODO: adjust and unify in the future
|
||||
/// Attempts to parse the date string with multiple commonly used formats in Slovakia
|
||||
fn parse_date_with_multiple_formats(date_str: &str) -> Option<NaiveDate> {
|
||||
// Define common date formats used in Slovakia
|
||||
let formats = [
|
||||
"%d:%m:%Y", // Original format: 01:01:2023
|
||||
"%d.%m.%Y", // Standard Slovak format: 01.01.2023
|
||||
"%d-%m-%Y", // Dash format: 01-01-2023
|
||||
"%d/%m/%Y", // Slash format: 01/01/2023
|
||||
"%Y-%m-%d", // ISO format: 2023-01-01
|
||||
"%Y/%m/%d", // Alternative ISO: 2023/01/01
|
||||
"%Y.%m.%d", // Dot ISO: 2023.01.01
|
||||
"%d.%m.%y", // Short year with dot: 01.01.23
|
||||
"%d-%m-%y", // Short year with dash: 01-01-23
|
||||
"%d/%m/%y", // Short year with slash: 01/01/23
|
||||
];
|
||||
|
||||
// Try each format until one works
|
||||
for format in formats {
|
||||
if let Ok(date) = NaiveDate::parse_from_str(date_str, format) {
|
||||
return Some(date);
|
||||
}
|
||||
}
|
||||
|
||||
// If none of the formats worked, try to handle potential whitespace issues
|
||||
let trimmed = date_str.trim();
|
||||
if trimmed != date_str {
|
||||
return parse_date_with_multiple_formats(trimmed);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
78
src/uctovnictvo/handlers/put_uctovnictvo.rs
Normal file
78
src/uctovnictvo/handlers/put_uctovnictvo.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
// src/uctovnictvo/handlers/put_uctovnictvo.rs
|
||||
use tonic::Status;
|
||||
use sqlx::PgPool;
|
||||
use crate::uctovnictvo::models::Uctovnictvo;
|
||||
use crate::proto::multieko2::uctovnictvo::{PutUctovnictvoRequest, UctovnictvoResponse};
|
||||
use crate::shared::date_utils::parse_date_with_multiple_formats; // Import from shared module
|
||||
|
||||
pub async fn put_uctovnictvo(
|
||||
db_pool: &PgPool,
|
||||
request: PutUctovnictvoRequest,
|
||||
) -> Result<UctovnictvoResponse, Status> {
|
||||
let datum = parse_date_with_multiple_formats(&request.datum)
|
||||
.ok_or_else(|| Status::invalid_argument("Invalid date format"))?;
|
||||
|
||||
let uctovnictvo = sqlx::query_as!(
|
||||
Uctovnictvo,
|
||||
r#"
|
||||
UPDATE uctovnictvo
|
||||
SET
|
||||
adresar_id = $2,
|
||||
c_dokladu = $3,
|
||||
datum = $4,
|
||||
c_faktury = $5,
|
||||
obsah = $6,
|
||||
stredisko = $7,
|
||||
c_uctu = $8,
|
||||
md = $9,
|
||||
identif = $10,
|
||||
poznanka = $11,
|
||||
firma = $12
|
||||
WHERE id = $1 AND deleted = FALSE
|
||||
RETURNING
|
||||
id,
|
||||
deleted,
|
||||
adresar_id,
|
||||
c_dokladu,
|
||||
datum as "datum: chrono::NaiveDate",
|
||||
c_faktury,
|
||||
obsah,
|
||||
stredisko,
|
||||
c_uctu,
|
||||
md,
|
||||
identif,
|
||||
poznanka,
|
||||
firma
|
||||
"#,
|
||||
request.id,
|
||||
request.adresar_id,
|
||||
request.c_dokladu,
|
||||
datum as chrono::NaiveDate,
|
||||
request.c_faktury,
|
||||
request.obsah,
|
||||
request.stredisko,
|
||||
request.c_uctu,
|
||||
request.md,
|
||||
request.identif,
|
||||
request.poznanka,
|
||||
request.firma
|
||||
)
|
||||
.fetch_one(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(e.to_string()))?;
|
||||
|
||||
Ok(UctovnictvoResponse {
|
||||
id: uctovnictvo.id,
|
||||
adresar_id: uctovnictvo.adresar_id,
|
||||
c_dokladu: uctovnictvo.c_dokladu,
|
||||
datum: uctovnictvo.datum.format("%d.%m.%Y").to_string(),
|
||||
c_faktury: uctovnictvo.c_faktury,
|
||||
obsah: uctovnictvo.obsah.unwrap_or_default(),
|
||||
stredisko: uctovnictvo.stredisko.unwrap_or_default(),
|
||||
c_uctu: uctovnictvo.c_uctu.unwrap_or_default(),
|
||||
md: uctovnictvo.md.unwrap_or_default(),
|
||||
identif: uctovnictvo.identif.unwrap_or_default(),
|
||||
poznanka: uctovnictvo.poznanka.unwrap_or_default(),
|
||||
firma: uctovnictvo.firma,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user