diff --git a/src/server/mod.rs b/src/server/mod.rs index a7c42f9..39776ea 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -8,7 +8,7 @@ use crate::uctovnictvo::handlers::post_uctovnictvo; use crate::proto::multieko2::{ PostAdresarRequest, AdresarResponse, GetAdresarRequest, PutAdresarRequest, DeleteAdresarRequest, DeleteAdresarResponse, PositionRequest, CountResponse, Empty, - TableStructureResponse, PostUctovnictvoRequest, UctovnictvoResponse, + TableStructureResponse, adresar_server::{Adresar, AdresarServer}, FILE_DESCRIPTOR_SET, }; diff --git a/src/uctovnictvo/handlers/post_uctovnictvo.rs b/src/uctovnictvo/handlers/post_uctovnictvo.rs index 7db0d11..2ede2c9 100644 --- a/src/uctovnictvo/handlers/post_uctovnictvo.rs +++ b/src/uctovnictvo/handlers/post_uctovnictvo.rs @@ -1,7 +1,7 @@ // src/uctovnictvo/handlers/post_uctovnictvo.rs use tonic::Status; use sqlx::PgPool; -use chrono::NaiveDate; +use chrono::NaiveDate; // Using chrono directly! use crate::uctovnictvo::models::Uctovnictvo; use crate::proto::multieko2::uctovnictvo::{PostUctovnictvoRequest, UctovnictvoResponse}; @@ -9,10 +9,11 @@ pub async fn post_uctovnictvo( db_pool: &PgPool, request: PostUctovnictvoRequest, ) -> Result { - // Parse the date string into NaiveDate - let datum = chrono::NaiveDate::parse_from_str(&request.datum, "%Y-%m-%d") + // Parse the date string into a chrono::NaiveDate. + let datum = NaiveDate::parse_from_str(&request.datum, "%Y-%m-%d") .map_err(|e| Status::invalid_argument(format!("Invalid date: {}", e)))?; + // Pass the NaiveDate value directly. let uctovnictvo = sqlx::query_as!( Uctovnictvo, r#" @@ -24,12 +25,23 @@ pub async fn post_uctovnictvo( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12 ) RETURNING - id, deleted, adresar_id, c_dokladu, datum, c_faktury, obsah, - stredisko, c_uctu, md, identif, poznanka, firma + id, + deleted, + adresar_id, + c_dokladu, + datum as "datum: chrono::NaiveDate", + c_faktury, + obsah, + stredisko, + c_uctu, + md, + identif, + poznanka, + firma "#, request.adresar_id, request.c_dokladu, - datum, + datum as chrono::NaiveDate, request.c_faktury, request.obsah, request.stredisko, @@ -38,17 +50,19 @@ pub async fn post_uctovnictvo( request.identif, request.poznanka, request.firma, - false // Set deleted to false by default + false ) .fetch_one(db_pool) .await .map_err(|e| Status::internal(e.to_string()))?; + // Convert the NaiveDate back to a String for the API response, + // if that format fits your client requirements. Ok(UctovnictvoResponse { id: uctovnictvo.id, adresar_id: uctovnictvo.adresar_id, c_dokladu: uctovnictvo.c_dokladu, - datum: uctovnictvo.datum.to_string(), // Convert NaiveDate back to string + datum: uctovnictvo.datum.to_string(), c_faktury: uctovnictvo.c_faktury, obsah: uctovnictvo.obsah.unwrap_or_default(), stredisko: uctovnictvo.stredisko.unwrap_or_default(), @@ -59,3 +73,4 @@ pub async fn post_uctovnictvo( firma: uctovnictvo.firma, }) } + diff --git a/src/uctovnictvo/models.rs b/src/uctovnictvo/models.rs index ad52b90..3c9e152 100644 --- a/src/uctovnictvo/models.rs +++ b/src/uctovnictvo/models.rs @@ -8,7 +8,7 @@ pub struct Uctovnictvo { pub deleted: bool, pub adresar_id: i64, pub c_dokladu: String, - pub datum: chrono::NaiveDate, + pub datum: NaiveDate, pub c_faktury: String, pub obsah: Option, pub stredisko: Option, @@ -18,3 +18,4 @@ pub struct Uctovnictvo { pub poznanka: Option, pub firma: String, } +