get adresar method is now working
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
// src/adresar/handlers.rs
|
||||
|
||||
pub mod create_adresar;
|
||||
pub mod get_adresar;
|
||||
|
||||
pub use create_adresar::create_adresar;
|
||||
pub use get_adresar::get_adresar;
|
||||
|
||||
42
src/adresar/handlers/get_adresar.rs
Normal file
42
src/adresar/handlers/get_adresar.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
// src/adresar/handlers/get_adresar.rs
|
||||
use tonic::{Request, Response, Status};
|
||||
use sqlx::PgPool;
|
||||
use crate::adresar::models::Adresar;
|
||||
use crate::proto::multieko2::{GetAdresarRequest, AdresarResponse};
|
||||
|
||||
pub async fn get_adresar(
|
||||
db_pool: &PgPool,
|
||||
request: GetAdresarRequest,
|
||||
) -> Result<AdresarResponse, Status> {
|
||||
let adresar = sqlx::query_as!(
|
||||
Adresar,
|
||||
r#"
|
||||
SELECT id, firma, kz, drc, ulica, psc, mesto, stat, banka, ucet, skladm, ico, kontakt, telefon, skladu, fax
|
||||
FROM adresar
|
||||
WHERE id = $1
|
||||
"#,
|
||||
request.id
|
||||
)
|
||||
.fetch_one(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::not_found(e.to_string()))?;
|
||||
|
||||
Ok(AdresarResponse {
|
||||
id: adresar.id,
|
||||
firma: adresar.firma,
|
||||
kz: adresar.kz.unwrap_or_default(),
|
||||
drc: adresar.drc.unwrap_or_default(),
|
||||
ulica: adresar.ulica.unwrap_or_default(),
|
||||
psc: adresar.psc.unwrap_or_default(),
|
||||
mesto: adresar.mesto.unwrap_or_default(),
|
||||
stat: adresar.stat.unwrap_or_default(),
|
||||
banka: adresar.banka.unwrap_or_default(),
|
||||
ucet: adresar.ucet.unwrap_or_default(),
|
||||
skladm: adresar.skladm.unwrap_or_default(),
|
||||
ico: adresar.ico.unwrap_or_default(),
|
||||
kontakt: adresar.kontakt.unwrap_or_default(),
|
||||
telefon: adresar.telefon.unwrap_or_default(),
|
||||
skladu: adresar.skladu.unwrap_or_default(),
|
||||
fax: adresar.fax.unwrap_or_default(),
|
||||
})
|
||||
}
|
||||
BIN
src/proto/descriptor.bin
Normal file
BIN
src/proto/descriptor.bin
Normal file
Binary file not shown.
@@ -1,3 +1,7 @@
|
||||
// src/proto/mod.rs
|
||||
pub mod multieko2 {
|
||||
tonic::include_proto!("multieko2");
|
||||
|
||||
// Include the file descriptor set
|
||||
pub const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!("descriptor.bin");
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
// src/server/mod.rs
|
||||
use tonic::{Request, Response, Status};
|
||||
use tonic_reflection::server::Builder as ReflectionBuilder; // Import tonic-reflection
|
||||
use crate::db;
|
||||
use crate::adresar::handlers::create_adresar;
|
||||
use crate::adresar::handlers::{create_adresar, get_adresar};
|
||||
use crate::proto::multieko2::{
|
||||
AdresarRequest, AdresarResponse,
|
||||
AdresarRequest, AdresarResponse, GetAdresarRequest,
|
||||
adresar_server::{Adresar, AdresarServer},
|
||||
FILE_DESCRIPTOR_SET, // Import the generated file descriptor set
|
||||
};
|
||||
|
||||
pub struct AdresarService {
|
||||
@@ -20,6 +22,14 @@ impl Adresar for AdresarService {
|
||||
let response = create_adresar(&self.db_pool, request.into_inner()).await?;
|
||||
Ok(Response::new(response))
|
||||
}
|
||||
|
||||
async fn get_adresar(
|
||||
&self,
|
||||
request: Request<GetAdresarRequest>,
|
||||
) -> Result<Response<AdresarResponse>, Status> {
|
||||
let response = get_adresar(&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>> {
|
||||
@@ -28,8 +38,15 @@ pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error:
|
||||
|
||||
println!("Server listening on {}", addr);
|
||||
|
||||
// Enable reflection
|
||||
let reflection_service = ReflectionBuilder::configure()
|
||||
.register_encoded_file_descriptor_set(FILE_DESCRIPTOR_SET) // Register the file descriptor set
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
tonic::transport::Server::builder()
|
||||
.add_service(AdresarServer::new(adresar_service))
|
||||
.add_service(reflection_service) // Add the reflection service
|
||||
.serve(addr)
|
||||
.await?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user