not working, adding uctovnictvo
This commit is contained in:
3
Cargo.lock
generated
3
Cargo.lock
generated
@@ -305,8 +305,10 @@ checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"android-tzdata",
|
"android-tzdata",
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
|
"js-sys",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"serde",
|
"serde",
|
||||||
|
"wasm-bindgen",
|
||||||
"windows-targets 0.52.6",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -1362,6 +1364,7 @@ dependencies = [
|
|||||||
name = "multieko2"
|
name = "multieko2"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"console",
|
"console",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
chrono = "0.4.39"
|
||||||
clap = { version = "4.5.29", features = ["derive"] }
|
clap = { version = "4.5.29", features = ["derive"] }
|
||||||
console = "0.15.10"
|
console = "0.15.10"
|
||||||
crossterm = "0.28.1"
|
crossterm = "0.28.1"
|
||||||
|
|||||||
6
build.rs
6
build.rs
@@ -2,7 +2,9 @@
|
|||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
tonic_build::configure()
|
tonic_build::configure()
|
||||||
.build_server(true)
|
.build_server(true)
|
||||||
.file_descriptor_set_path("src/proto/descriptor.bin") // Generate the file descriptor set
|
.compile_protos(
|
||||||
.compile_protos(&["proto/api.proto"], &["proto"])?; // Use compile_protos() instead of compile()
|
&["proto/api.proto", "proto/uctovnictvo.proto"], // Include both proto files
|
||||||
|
&["proto"],
|
||||||
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
37
proto/uctovnictvo.proto
Normal file
37
proto/uctovnictvo.proto
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
// proto/uctovnictvo.proto
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package multieko2;
|
||||||
|
|
||||||
|
service Uctovnictvo {
|
||||||
|
rpc PostUctovnictvo (PostUctovnictvoRequest) returns (UctovnictvoResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
message PostUctovnictvoRequest {
|
||||||
|
int64 adresar_id = 1;
|
||||||
|
string c_dokladu = 2;
|
||||||
|
string datum = 3; // Use string for simplicity, or use google.protobuf.Timestamp for better date handling
|
||||||
|
string c_faktury = 4;
|
||||||
|
string obsah = 5;
|
||||||
|
string stredisko = 6;
|
||||||
|
string c_uctu = 7;
|
||||||
|
string md = 8;
|
||||||
|
string identif = 9;
|
||||||
|
string poznanka = 10;
|
||||||
|
string firma = 11;
|
||||||
|
}
|
||||||
|
|
||||||
|
message UctovnictvoResponse {
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -4,3 +4,4 @@ pub mod client;
|
|||||||
pub mod server;
|
pub mod server;
|
||||||
pub mod proto;
|
pub mod proto;
|
||||||
pub mod adresar;
|
pub mod adresar;
|
||||||
|
pub mod uctovnictvo;
|
||||||
|
|||||||
@@ -4,11 +4,13 @@ use tonic_reflection::server::Builder as ReflectionBuilder;
|
|||||||
use crate::adresar::handlers::{
|
use crate::adresar::handlers::{
|
||||||
post_adresar, get_adresar, put_adresar, delete_adresar, get_adresar_count, get_adresar_by_position, get_table_structure
|
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::{
|
use crate::proto::multieko2::{
|
||||||
PostAdresarRequest, AdresarResponse, GetAdresarRequest, PutAdresarRequest,
|
PostAdresarRequest, AdresarResponse, GetAdresarRequest, PutAdresarRequest,
|
||||||
DeleteAdresarRequest, DeleteAdresarResponse, PositionRequest, CountResponse, Empty,
|
DeleteAdresarRequest, DeleteAdresarResponse, PositionRequest, CountResponse, Empty,
|
||||||
TableStructureResponse, // Add this import
|
TableStructureResponse,
|
||||||
adresar_server::{Adresar, AdresarServer},
|
adresar_server::{Adresar, AdresarServer},
|
||||||
|
uctovnictvo_server::{Uctovnictvo, UctovnictvoServer},
|
||||||
FILE_DESCRIPTOR_SET,
|
FILE_DESCRIPTOR_SET,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -16,6 +18,10 @@ pub struct AdresarService {
|
|||||||
db_pool: sqlx::PgPool,
|
db_pool: sqlx::PgPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct UctovnictvoService {
|
||||||
|
db_pool: sqlx::PgPool,
|
||||||
|
}
|
||||||
|
|
||||||
#[tonic::async_trait]
|
#[tonic::async_trait]
|
||||||
impl Adresar for AdresarService {
|
impl Adresar for AdresarService {
|
||||||
async fn post_adresar(
|
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>> {
|
pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let addr = "[::1]:50051".parse()?;
|
let addr = "[::1]:50051".parse()?;
|
||||||
let adresar_service = AdresarService { db_pool: db_pool.clone() };
|
let adresar_service = AdresarService { db_pool: db_pool.clone() };
|
||||||
|
let uctovnictvo_service = UctovnictvoService { db_pool: db_pool.clone() };
|
||||||
|
|
||||||
println!("Server listening on {}", addr);
|
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()
|
tonic::transport::Server::builder()
|
||||||
.add_service(AdresarServer::new(adresar_service))
|
.add_service(AdresarServer::new(adresar_service))
|
||||||
|
.add_service(UctovnictvoServer::new(uctovnictvo_service))
|
||||||
.add_service(reflection_service)
|
.add_service(reflection_service)
|
||||||
.serve(addr)
|
.serve(addr)
|
||||||
.await?;
|
.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