working finally

This commit is contained in:
filipriec
2025-02-21 20:57:22 +01:00
parent 4b10b0b213
commit d37edb63f7
3 changed files with 26 additions and 10 deletions

View File

@@ -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,
};

View File

@@ -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<UctovnictvoResponse, Status> {
// 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,
})
}

View File

@@ -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<String>,
pub stredisko: Option<String>,
@@ -18,3 +18,4 @@ pub struct Uctovnictvo {
pub poznanka: Option<String>,
pub firma: String,
}