get count

This commit is contained in:
filipriec
2025-02-21 22:41:46 +01:00
parent 577492b03c
commit 4a2b5d0c12
5 changed files with 37 additions and 4 deletions

View File

@@ -8,6 +8,7 @@ import "adresar.proto";
service Uctovnictvo {
rpc PostUctovnictvo (PostUctovnictvoRequest) returns (UctovnictvoResponse);
rpc GetUctovnictvo (GetUctovnictvoRequest) returns (UctovnictvoResponse);
rpc GetUctovnictvoCount (multieko2.adresar.Empty) returns (multieko2.adresar.CountResponse);
}
message PostUctovnictvoRequest {

Binary file not shown.

View File

@@ -4,7 +4,7 @@ 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, get_uctovnictvo}; // Update this line
use crate::uctovnictvo::handlers::{post_uctovnictvo, get_uctovnictvo, get_uctovnictvo_count};
use crate::proto::multieko2::{
FILE_DESCRIPTOR_SET,
};
@@ -104,6 +104,14 @@ impl Uctovnictvo for UctovnictvoService {
let response = get_uctovnictvo(&self.db_pool, request.into_inner()).await?;
Ok(Response::new(response))
}
async fn get_uctovnictvo_count(
&self,
request: Request<Empty>,
) -> Result<Response<CountResponse>, Status> {
let response = get_uctovnictvo_count(&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>> {

View File

@@ -1,7 +1,8 @@
// src/uctovnictvo/handlers.rs
pub mod post_uctovnictvo;
pub mod get_uctovnictvo; // Add this line
pub mod get_uctovnictvo;
pub mod get_uctovnictvo_count; // Add this line
pub use post_uctovnictvo::post_uctovnictvo;
pub use get_uctovnictvo::get_uctovnictvo; // Add this line
pub use get_uctovnictvo::get_uctovnictvo;
pub use get_uctovnictvo_count::get_uctovnictvo_count; // Add this line

View File

@@ -0,0 +1,23 @@
// src/uctovnictvo/handlers/get_uctovnictvo_count.rs
use tonic::Status;
use sqlx::PgPool;
use crate::proto::multieko2::adresar::{CountResponse, Empty};
pub async fn get_uctovnictvo_count(
db_pool: &PgPool,
_request: Empty,
) -> Result<CountResponse, Status> {
let count: i64 = sqlx::query_scalar!(
r#"
SELECT COUNT(*) AS count
FROM uctovnictvo
WHERE deleted = FALSE
"#
)
.fetch_one(db_pool)
.await
.map_err(|e| Status::internal(e.to_string()))?
.unwrap_or(0);
Ok(CountResponse { count })
}