working and flexible to all kinds of formats, tested
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
// src/uctovnictvo/handlers/post_uctovnictvo.rs
|
// src/uctovnictvo/handlers/post_uctovnictvo.rs
|
||||||
use tonic::Status;
|
use tonic::Status;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use chrono::NaiveDate; // Using chrono directly!
|
use chrono::NaiveDate;
|
||||||
use crate::uctovnictvo::models::Uctovnictvo;
|
use crate::uctovnictvo::models::Uctovnictvo;
|
||||||
use crate::proto::multieko2::uctovnictvo::{PostUctovnictvoRequest, UctovnictvoResponse};
|
use crate::proto::multieko2::uctovnictvo::{PostUctovnictvoRequest, UctovnictvoResponse};
|
||||||
|
|
||||||
@@ -9,9 +9,9 @@ pub async fn post_uctovnictvo(
|
|||||||
db_pool: &PgPool,
|
db_pool: &PgPool,
|
||||||
request: PostUctovnictvoRequest,
|
request: PostUctovnictvoRequest,
|
||||||
) -> Result<UctovnictvoResponse, Status> {
|
) -> Result<UctovnictvoResponse, Status> {
|
||||||
// Parse the date string into a chrono::NaiveDate.
|
// Try multiple date formats commonly used in Slovakia
|
||||||
let datum = NaiveDate::parse_from_str(&request.datum, "%d:%m:%Y")
|
let datum = parse_date_with_multiple_formats(&request.datum)
|
||||||
.map_err(|e| Status::invalid_argument(format!("Invalid date: {}", e)))?;
|
.ok_or_else(|| Status::invalid_argument(format!("Invalid date format: {}", request.datum)))?;
|
||||||
|
|
||||||
// Pass the NaiveDate value directly.
|
// Pass the NaiveDate value directly.
|
||||||
let uctovnictvo = sqlx::query_as!(
|
let uctovnictvo = sqlx::query_as!(
|
||||||
@@ -56,13 +56,12 @@ pub async fn post_uctovnictvo(
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| Status::internal(e.to_string()))?;
|
.map_err(|e| Status::internal(e.to_string()))?;
|
||||||
|
|
||||||
// Convert the NaiveDate back to a String for the API response,
|
// Return the response with formatted date
|
||||||
// if that format fits your client requirements.
|
|
||||||
Ok(UctovnictvoResponse {
|
Ok(UctovnictvoResponse {
|
||||||
id: uctovnictvo.id,
|
id: uctovnictvo.id,
|
||||||
adresar_id: uctovnictvo.adresar_id,
|
adresar_id: uctovnictvo.adresar_id,
|
||||||
c_dokladu: uctovnictvo.c_dokladu,
|
c_dokladu: uctovnictvo.c_dokladu,
|
||||||
datum: uctovnictvo.datum.to_string(),
|
datum: uctovnictvo.datum.format("%d.%m.%Y").to_string(), // Standard Slovak format
|
||||||
c_faktury: uctovnictvo.c_faktury,
|
c_faktury: uctovnictvo.c_faktury,
|
||||||
obsah: uctovnictvo.obsah.unwrap_or_default(),
|
obsah: uctovnictvo.obsah.unwrap_or_default(),
|
||||||
stredisko: uctovnictvo.stredisko.unwrap_or_default(),
|
stredisko: uctovnictvo.stredisko.unwrap_or_default(),
|
||||||
@@ -74,3 +73,35 @@ pub async fn post_uctovnictvo(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user