working get count endpoint
This commit is contained in:
@@ -9,6 +9,8 @@ service TablesData {
|
|||||||
rpc PutTableData (PutTableDataRequest) returns (PutTableDataResponse);
|
rpc PutTableData (PutTableDataRequest) returns (PutTableDataResponse);
|
||||||
rpc DeleteTableData (DeleteTableDataRequest) returns (DeleteTableDataResponse);
|
rpc DeleteTableData (DeleteTableDataRequest) returns (DeleteTableDataResponse);
|
||||||
rpc GetTableData(GetTableDataRequest) returns (GetTableDataResponse);
|
rpc GetTableData(GetTableDataRequest) returns (GetTableDataResponse);
|
||||||
|
rpc GetTableDataCount(GetTableDataCountRequest) returns (multieko2.common.CountResponse);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message PostTableDataRequest {
|
message PostTableDataRequest {
|
||||||
@@ -55,3 +57,8 @@ message GetTableDataRequest {
|
|||||||
message GetTableDataResponse {
|
message GetTableDataResponse {
|
||||||
map<string, string> data = 1;
|
map<string, string> data = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message GetTableDataCountRequest {
|
||||||
|
string profile_name = 1;
|
||||||
|
string table_name = 2;
|
||||||
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -74,6 +74,13 @@ pub struct GetTableDataResponse {
|
|||||||
::prost::alloc::string::String,
|
::prost::alloc::string::String,
|
||||||
>,
|
>,
|
||||||
}
|
}
|
||||||
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
|
pub struct GetTableDataCountRequest {
|
||||||
|
#[prost(string, tag = "1")]
|
||||||
|
pub profile_name: ::prost::alloc::string::String,
|
||||||
|
#[prost(string, tag = "2")]
|
||||||
|
pub table_name: ::prost::alloc::string::String,
|
||||||
|
}
|
||||||
/// Generated client implementations.
|
/// Generated client implementations.
|
||||||
pub mod tables_data_client {
|
pub mod tables_data_client {
|
||||||
#![allow(
|
#![allow(
|
||||||
@@ -272,6 +279,35 @@ pub mod tables_data_client {
|
|||||||
);
|
);
|
||||||
self.inner.unary(req, path, codec).await
|
self.inner.unary(req, path, codec).await
|
||||||
}
|
}
|
||||||
|
pub async fn get_table_data_count(
|
||||||
|
&mut self,
|
||||||
|
request: impl tonic::IntoRequest<super::GetTableDataCountRequest>,
|
||||||
|
) -> std::result::Result<
|
||||||
|
tonic::Response<super::super::common::CountResponse>,
|
||||||
|
tonic::Status,
|
||||||
|
> {
|
||||||
|
self.inner
|
||||||
|
.ready()
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
tonic::Status::unknown(
|
||||||
|
format!("Service was not ready: {}", e.into()),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
let codec = tonic::codec::ProstCodec::default();
|
||||||
|
let path = http::uri::PathAndQuery::from_static(
|
||||||
|
"/multieko2.tables_data.TablesData/GetTableDataCount",
|
||||||
|
);
|
||||||
|
let mut req = request.into_request();
|
||||||
|
req.extensions_mut()
|
||||||
|
.insert(
|
||||||
|
GrpcMethod::new(
|
||||||
|
"multieko2.tables_data.TablesData",
|
||||||
|
"GetTableDataCount",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
self.inner.unary(req, path, codec).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Generated server implementations.
|
/// Generated server implementations.
|
||||||
@@ -315,6 +351,13 @@ pub mod tables_data_server {
|
|||||||
tonic::Response<super::GetTableDataResponse>,
|
tonic::Response<super::GetTableDataResponse>,
|
||||||
tonic::Status,
|
tonic::Status,
|
||||||
>;
|
>;
|
||||||
|
async fn get_table_data_count(
|
||||||
|
&self,
|
||||||
|
request: tonic::Request<super::GetTableDataCountRequest>,
|
||||||
|
) -> std::result::Result<
|
||||||
|
tonic::Response<super::super::common::CountResponse>,
|
||||||
|
tonic::Status,
|
||||||
|
>;
|
||||||
}
|
}
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TablesDataServer<T> {
|
pub struct TablesDataServer<T> {
|
||||||
@@ -572,6 +615,52 @@ pub mod tables_data_server {
|
|||||||
};
|
};
|
||||||
Box::pin(fut)
|
Box::pin(fut)
|
||||||
}
|
}
|
||||||
|
"/multieko2.tables_data.TablesData/GetTableDataCount" => {
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
struct GetTableDataCountSvc<T: TablesData>(pub Arc<T>);
|
||||||
|
impl<
|
||||||
|
T: TablesData,
|
||||||
|
> tonic::server::UnaryService<super::GetTableDataCountRequest>
|
||||||
|
for GetTableDataCountSvc<T> {
|
||||||
|
type Response = super::super::common::CountResponse;
|
||||||
|
type Future = BoxFuture<
|
||||||
|
tonic::Response<Self::Response>,
|
||||||
|
tonic::Status,
|
||||||
|
>;
|
||||||
|
fn call(
|
||||||
|
&mut self,
|
||||||
|
request: tonic::Request<super::GetTableDataCountRequest>,
|
||||||
|
) -> Self::Future {
|
||||||
|
let inner = Arc::clone(&self.0);
|
||||||
|
let fut = async move {
|
||||||
|
<T as TablesData>::get_table_data_count(&inner, request)
|
||||||
|
.await
|
||||||
|
};
|
||||||
|
Box::pin(fut)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let accept_compression_encodings = self.accept_compression_encodings;
|
||||||
|
let send_compression_encodings = self.send_compression_encodings;
|
||||||
|
let max_decoding_message_size = self.max_decoding_message_size;
|
||||||
|
let max_encoding_message_size = self.max_encoding_message_size;
|
||||||
|
let inner = self.inner.clone();
|
||||||
|
let fut = async move {
|
||||||
|
let method = GetTableDataCountSvc(inner);
|
||||||
|
let codec = tonic::codec::ProstCodec::default();
|
||||||
|
let mut grpc = tonic::server::Grpc::new(codec)
|
||||||
|
.apply_compression_config(
|
||||||
|
accept_compression_encodings,
|
||||||
|
send_compression_encodings,
|
||||||
|
)
|
||||||
|
.apply_max_message_size_config(
|
||||||
|
max_decoding_message_size,
|
||||||
|
max_encoding_message_size,
|
||||||
|
);
|
||||||
|
let res = grpc.unary(method, req).await;
|
||||||
|
Ok(res)
|
||||||
|
};
|
||||||
|
Box::pin(fut)
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let mut response = http::Response::new(empty_body());
|
let mut response = http::Response::new(empty_body());
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
// src/server/services/tables_data_service.rs
|
// src/server/services/tables_data_service.rs
|
||||||
use tonic::{Request, Response, Status};
|
use tonic::{Request, Response, Status};
|
||||||
use common::proto::multieko2::tables_data::tables_data_server::TablesData;
|
use common::proto::multieko2::tables_data::tables_data_server::TablesData;
|
||||||
|
use common::proto::multieko2::common::CountResponse;
|
||||||
use common::proto::multieko2::tables_data::{
|
use common::proto::multieko2::tables_data::{
|
||||||
PostTableDataRequest, PostTableDataResponse,
|
PostTableDataRequest, PostTableDataResponse,
|
||||||
PutTableDataRequest, PutTableDataResponse,
|
PutTableDataRequest, PutTableDataResponse,
|
||||||
DeleteTableDataRequest, DeleteTableDataResponse,
|
DeleteTableDataRequest, DeleteTableDataResponse,
|
||||||
GetTableDataRequest, GetTableDataResponse,
|
GetTableDataRequest, GetTableDataResponse,
|
||||||
|
GetTableDataCountRequest,
|
||||||
};
|
};
|
||||||
use crate::tables_data::handlers::{post_table_data, put_table_data, delete_table_data, get_table_data,};
|
use crate::tables_data::handlers::{post_table_data, put_table_data, delete_table_data, get_table_data, get_table_data_count};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -53,4 +55,13 @@ impl TablesData for TablesDataService {
|
|||||||
let response = get_table_data(&self.db_pool, request).await?;
|
let response = get_table_data(&self.db_pool, request).await?;
|
||||||
Ok(Response::new(response))
|
Ok(Response::new(response))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_table_data_count(
|
||||||
|
&self,
|
||||||
|
request: Request<GetTableDataCountRequest>,
|
||||||
|
) -> Result<Response<CountResponse>, Status> {
|
||||||
|
let request = request.into_inner();
|
||||||
|
let response = get_table_data_count(&self.db_pool, request).await?;
|
||||||
|
Ok(Response::new(response))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
server/src/tables_data/docs/get_data_count.txt
Normal file
5
server/src/tables_data/docs/get_data_count.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
grpcurl -plaintext -d '{"profile_name": "default", "table_name": "2025_adresar"}' localhost:50051 multieko2.tables_data.TablesData/GetTableDataCount
|
||||||
|
|
||||||
|
{
|
||||||
|
"count": "1"
|
||||||
|
}
|
||||||
@@ -3,8 +3,10 @@ pub mod post_table_data;
|
|||||||
pub mod put_table_data;
|
pub mod put_table_data;
|
||||||
pub mod delete_table_data;
|
pub mod delete_table_data;
|
||||||
pub mod get_table_data;
|
pub mod get_table_data;
|
||||||
|
pub mod get_table_data_count;
|
||||||
|
|
||||||
pub use post_table_data::post_table_data;
|
pub use post_table_data::post_table_data;
|
||||||
pub use put_table_data::put_table_data;
|
pub use put_table_data::put_table_data;
|
||||||
pub use delete_table_data::delete_table_data;
|
pub use delete_table_data::delete_table_data;
|
||||||
pub use get_table_data::get_table_data;
|
pub use get_table_data::get_table_data;
|
||||||
|
pub use get_table_data_count::get_table_data_count;
|
||||||
|
|||||||
61
server/src/tables_data/handlers/get_table_data_count.rs
Normal file
61
server/src/tables_data/handlers/get_table_data_count.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
// src/tables_data/handlers/get_table_data_count.rs
|
||||||
|
use tonic::Status;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use common::proto::multieko2::common::{CountResponse, Empty};
|
||||||
|
use common::proto::multieko2::tables_data::GetTableDataCountRequest;
|
||||||
|
|
||||||
|
pub async fn get_table_data_count(
|
||||||
|
db_pool: &PgPool,
|
||||||
|
request: GetTableDataCountRequest,
|
||||||
|
) -> Result<CountResponse, Status> {
|
||||||
|
let profile_name = request.profile_name;
|
||||||
|
let table_name = request.table_name;
|
||||||
|
|
||||||
|
// Lookup profile
|
||||||
|
let profile = sqlx::query!(
|
||||||
|
"SELECT id FROM profiles WHERE name = $1",
|
||||||
|
profile_name
|
||||||
|
)
|
||||||
|
.fetch_optional(db_pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?;
|
||||||
|
|
||||||
|
let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id;
|
||||||
|
|
||||||
|
// Verify table exists and belongs to profile
|
||||||
|
let table_exists = sqlx::query!(
|
||||||
|
r#"SELECT EXISTS(
|
||||||
|
SELECT 1 FROM table_definitions
|
||||||
|
WHERE profile_id = $1 AND table_name = $2
|
||||||
|
)"#,
|
||||||
|
profile_id,
|
||||||
|
table_name
|
||||||
|
)
|
||||||
|
.fetch_one(db_pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| Status::internal(format!("Table verification error: {}", e)))?
|
||||||
|
.exists
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
|
if !table_exists {
|
||||||
|
return Err(Status::not_found("Table not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get count of non-deleted records
|
||||||
|
let query = format!(
|
||||||
|
r#"
|
||||||
|
SELECT COUNT(*) AS count
|
||||||
|
FROM "{}"
|
||||||
|
WHERE deleted = FALSE
|
||||||
|
"#,
|
||||||
|
table_name
|
||||||
|
);
|
||||||
|
|
||||||
|
let count: i64 = sqlx::query_scalar::<_, Option<i64>>(&query)
|
||||||
|
.fetch_one(db_pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| Status::internal(format!("Count query failed: {}", e)))?
|
||||||
|
.unwrap_or(0);
|
||||||
|
|
||||||
|
Ok(CountResponse { count })
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user