grpc endpoint showing database schema for the frontend, now working

This commit is contained in:
filipriec
2025-02-21 01:55:33 +01:00
parent d105d06230
commit 5d9fffce13
5 changed files with 144 additions and 1 deletions

View File

@@ -14,8 +14,10 @@ service Adresar {
rpc DeleteAdresar (DeleteAdresarRequest) returns (DeleteAdresarResponse);
rpc GetAdresarCount (Empty) returns (CountResponse); // New endpoint
rpc GetAdresarByPosition (PositionRequest) returns (AdresarResponse); // New endpoint
rpc GetTableStructure (Empty) returns (TableStructureResponse); // New endpoint for table structure
}
// Existing messages
message GetAdresarRequest {
int64 id = 1; // The ID of the Adresar entry to retrieve
}
@@ -102,3 +104,15 @@ message CountResponse {
message PositionRequest {
int64 position = 1; // Request with the position of the item to retrieve
}
// New messages for the table structure endpoint
message TableStructureResponse {
repeated TableColumn columns = 1; // List of columns in the table
}
message TableColumn {
string name = 1; // Name of the column
string data_type = 2; // Data type of the column (e.g., TEXT, INT, etc.)
bool is_nullable = 3; // Whether the column allows NULL values
bool is_primary_key = 4; // Whether the column is a primary key
}

View File

@@ -6,6 +6,7 @@ pub mod put_adresar;
pub mod delete_adresar;
pub mod get_adresar_count;
pub mod get_adresar_by_position;
pub mod get_table_structure;
pub use post_adresar::post_adresar;
pub use get_adresar::get_adresar;
@@ -13,3 +14,4 @@ pub use put_adresar::put_adresar;
pub use delete_adresar::delete_adresar;
pub use get_adresar_count::get_adresar_count;
pub use get_adresar_by_position::get_adresar_by_position;
pub use get_table_structure::get_table_structure;

View File

@@ -0,0 +1,116 @@
// src/adresar/handlers/get_table_structure.rs
use tonic::Status;
use sqlx::PgPool;
use crate::proto::multieko2::{TableStructureResponse, TableColumn, Empty};
pub async fn get_table_structure(
db_pool: &PgPool,
_request: Empty,
) -> Result<TableStructureResponse, Status> {
let columns = vec![
TableColumn {
name: "id".to_string(),
data_type: "BIGSERIAL".to_string(), // Updated field name
is_nullable: false,
is_primary_key: true,
},
TableColumn {
name: "firma".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: false,
is_primary_key: false,
},
TableColumn {
name: "kz".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "drc".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "ulica".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "psc".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "mesto".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "stat".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "banka".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "ucet".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "skladm".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "ico".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "kontakt".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "telefon".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "skladu".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "fax".to_string(),
data_type: "TEXT".to_string(), // Updated field name
is_nullable: true,
is_primary_key: false,
},
TableColumn {
name: "created_at".to_string(),
data_type: "TIMESTAMPTZ".to_string(), // Updated field name
is_nullable: false,
is_primary_key: false,
},
];
Ok(TableStructureResponse { columns })
}

Binary file not shown.

View File

@@ -1,10 +1,13 @@
// src/server/mod.rs
use tonic::{Request, Response, Status};
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};
use crate::adresar::handlers::{
post_adresar, get_adresar, put_adresar, delete_adresar, get_adresar_count, get_adresar_by_position, get_table_structure
};
use crate::proto::multieko2::{
PostAdresarRequest, AdresarResponse, GetAdresarRequest, PutAdresarRequest,
DeleteAdresarRequest, DeleteAdresarResponse, PositionRequest, CountResponse, Empty,
TableStructureResponse, // Add this import
adresar_server::{Adresar, AdresarServer},
FILE_DESCRIPTOR_SET,
};
@@ -62,6 +65,14 @@ impl Adresar for AdresarService {
let response = get_adresar_by_position(&self.db_pool, request.into_inner()).await?;
Ok(Response::new(response))
}
async fn get_table_structure(
&self,
request: Request<Empty>,
) -> Result<Response<TableStructureResponse>, Status> {
let response = get_table_structure(&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>> {