not working, adding uctovnictvo
This commit is contained in:
@@ -4,3 +4,4 @@ pub mod client;
|
||||
pub mod server;
|
||||
pub mod proto;
|
||||
pub mod adresar;
|
||||
pub mod uctovnictvo;
|
||||
|
||||
@@ -4,11 +4,13 @@ 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;
|
||||
use crate::proto::multieko2::{
|
||||
PostAdresarRequest, AdresarResponse, GetAdresarRequest, PutAdresarRequest,
|
||||
DeleteAdresarRequest, DeleteAdresarResponse, PositionRequest, CountResponse, Empty,
|
||||
TableStructureResponse, // Add this import
|
||||
TableStructureResponse,
|
||||
adresar_server::{Adresar, AdresarServer},
|
||||
uctovnictvo_server::{Uctovnictvo, UctovnictvoServer},
|
||||
FILE_DESCRIPTOR_SET,
|
||||
};
|
||||
|
||||
@@ -16,6 +18,10 @@ pub struct AdresarService {
|
||||
db_pool: sqlx::PgPool,
|
||||
}
|
||||
|
||||
pub struct UctovnictvoService {
|
||||
db_pool: sqlx::PgPool,
|
||||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl Adresar for AdresarService {
|
||||
async fn post_adresar(
|
||||
@@ -75,9 +81,21 @@ impl Adresar for AdresarService {
|
||||
}
|
||||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl Uctovnictvo for UctovnictvoService {
|
||||
async fn post_uctovnictvo(
|
||||
&self,
|
||||
request: Request<PostUctovnictvoRequest>,
|
||||
) -> Result<Response<UctovnictvoResponse>, Status> {
|
||||
let response = post_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>> {
|
||||
let addr = "[::1]:50051".parse()?;
|
||||
let adresar_service = AdresarService { db_pool: db_pool.clone() };
|
||||
let uctovnictvo_service = UctovnictvoService { db_pool: db_pool.clone() };
|
||||
|
||||
println!("Server listening on {}", addr);
|
||||
|
||||
@@ -88,6 +106,7 @@ pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error:
|
||||
|
||||
tonic::transport::Server::builder()
|
||||
.add_service(AdresarServer::new(adresar_service))
|
||||
.add_service(UctovnictvoServer::new(uctovnictvo_service))
|
||||
.add_service(reflection_service)
|
||||
.serve(addr)
|
||||
.await?;
|
||||
|
||||
5
src/uctovnictvo/handlers.rs
Normal file
5
src/uctovnictvo/handlers.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
// src/uctovnictvo/handlers.rs
|
||||
|
||||
pub mod post_uctovnictvo;
|
||||
|
||||
pub use post_uctovnictvo::post_uctovnictvo;
|
||||
61
src/uctovnictvo/handlers/post_uctovnictvo.rs
Normal file
61
src/uctovnictvo/handlers/post_uctovnictvo.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
// src/uctovnictvo/handlers/post_uctovnictvo.rs
|
||||
use tonic::Status;
|
||||
use sqlx::PgPool;
|
||||
use chrono::NaiveDate;
|
||||
use crate::uctovnictvo::models::Uctovnictvo;
|
||||
use crate::proto::multieko2::{PostUctovnictvoRequest, UctovnictvoResponse};
|
||||
|
||||
pub async fn post_uctovnictvo(
|
||||
db_pool: &PgPool,
|
||||
request: PostUctovnictvoRequest,
|
||||
) -> Result<UctovnictvoResponse, Status> {
|
||||
// Parse the date string into NaiveDate
|
||||
let datum = NaiveDate::parse_from_str(&request.datum, "%Y-%m-%d")
|
||||
.map_err(|e| Status::invalid_argument(format!("Invalid date format: {}", e)))?;
|
||||
|
||||
let uctovnictvo = sqlx::query_as!(
|
||||
Uctovnictvo,
|
||||
r#"
|
||||
INSERT INTO uctovnictvo (
|
||||
adresar_id, c_dokladu, datum, c_faktury, obsah, stredisko,
|
||||
c_uctu, md, identif, poznanka, firma, deleted
|
||||
)
|
||||
VALUES (
|
||||
$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
|
||||
"#,
|
||||
request.adresar_id,
|
||||
request.c_dokladu,
|
||||
datum,
|
||||
request.c_faktury,
|
||||
request.obsah,
|
||||
request.stredisko,
|
||||
request.c_uctu,
|
||||
request.md,
|
||||
request.identif,
|
||||
request.poznanka,
|
||||
request.firma,
|
||||
false // Set deleted to false by default
|
||||
)
|
||||
.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.to_string(), // Convert NaiveDate back 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,
|
||||
})
|
||||
}
|
||||
4
src/uctovnictvo/mod.rs
Normal file
4
src/uctovnictvo/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
// src/uctovnictvo/mod.rs
|
||||
|
||||
pub mod models;
|
||||
pub mod handlers;
|
||||
20
src/uctovnictvo/models.rs
Normal file
20
src/uctovnictvo/models.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
// src/uctovnictvo/models.rs
|
||||
use chrono::NaiveDate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Uctovnictvo {
|
||||
pub id: i64,
|
||||
pub deleted: bool,
|
||||
pub adresar_id: i64,
|
||||
pub c_dokladu: String,
|
||||
pub datum: NaiveDate, // Use chrono::NaiveDate for better date handling
|
||||
pub c_faktury: String,
|
||||
pub obsah: Option<String>,
|
||||
pub stredisko: Option<String>,
|
||||
pub c_uctu: Option<String>,
|
||||
pub md: Option<String>,
|
||||
pub identif: Option<String>,
|
||||
pub poznanka: Option<String>,
|
||||
pub firma: String,
|
||||
}
|
||||
Reference in New Issue
Block a user