perfectly working

This commit is contained in:
filipriec
2025-02-22 18:03:17 +01:00
parent 73ac6d16d3
commit 71babdaa09
2 changed files with 33 additions and 26 deletions

View File

@@ -8,19 +8,25 @@ use crossterm::cursor::{SetCursorStyle, EnableBlinking};
use ratatui::{backend::CrosstermBackend, Terminal}; use ratatui::{backend::CrosstermBackend, Terminal};
use std::io::{self, stdout}; use std::io::{self, stdout};
use tonic::transport::Channel; use tonic::transport::Channel;
use crate::proto::multieko2::{
adresar_client::AdresarClient, // Import the correct clients and proto messages from their respective modules
Empty, CountResponse, PositionRequest, use crate::proto::multieko2::adresar::adresar_client::AdresarClient;
AdresarResponse, PostAdresarRequest, PutAdresarRequest, use crate::proto::multieko2::adresar::{AdresarResponse, PostAdresarRequest, PutAdresarRequest};
}; use crate::proto::multieko2::common::{CountResponse, PositionRequest, Empty};
use crate::proto::multieko2::table_structure::table_structure_service_client::TableStructureServiceClient;
use crate::proto::multieko2::table_structure::TableStructureResponse;
pub struct AppTerminal { pub struct AppTerminal {
terminal: Terminal<CrosstermBackend<io::Stdout>>, terminal: Terminal<CrosstermBackend<io::Stdout>>,
grpc_client: AdresarClient<Channel>, // gRPC client adresar_client: AdresarClient<Channel>,
table_structure_client: TableStructureServiceClient<Channel>,
} }
impl AppTerminal { impl AppTerminal {
pub fn set_cursor_style(&mut self, style: SetCursorStyle) -> Result<(), Box<dyn std::error::Error>> { pub fn set_cursor_style(
&mut self,
style: SetCursorStyle,
) -> Result<(), Box<dyn std::error::Error>> {
execute!( execute!(
self.terminal.backend_mut(), self.terminal.backend_mut(),
style, style,
@@ -40,10 +46,11 @@ impl AppTerminal {
let backend = CrosstermBackend::new(stdout); let backend = CrosstermBackend::new(stdout);
let terminal = Terminal::new(backend)?; let terminal = Terminal::new(backend)?;
// Initialize gRPC client // Initialize both gRPC clients
let grpc_client = AdresarClient::connect("http://[::1]:50051").await?; let adresar_client = AdresarClient::connect("http://[::1]:50051").await?;
let table_structure_client = TableStructureServiceClient::connect("http://[::1]:50051").await?;
Ok(Self { terminal, grpc_client }) Ok(Self { terminal, adresar_client, table_structure_client })
} }
pub fn draw<F>(&mut self, f: F) -> Result<(), Box<dyn std::error::Error>> pub fn draw<F>(&mut self, f: F) -> Result<(), Box<dyn std::error::Error>>
@@ -93,25 +100,26 @@ impl AppTerminal {
} }
} }
// Add a method to get the total count of Adresar entries
// Adresar service methods use adresar_client
pub async fn get_adresar_count(&mut self) -> Result<u64, Box<dyn std::error::Error>> { pub async fn get_adresar_count(&mut self) -> Result<u64, Box<dyn std::error::Error>> {
let request = tonic::Request::new(Empty::default()); let request = tonic::Request::new(Empty::default());
let response: CountResponse = self.grpc_client.get_adresar_count(request).await?.into_inner(); let response: CountResponse = self.adresar_client.get_adresar_count(request).await?.into_inner();
Ok(response.count as u64) Ok(response.count as u64)
} }
// Add a method to get an Adresar entry by its position pub async fn get_adresar_by_position(&mut self, position: u64) -> Result<AdresarResponse, Box<dyn std::error::Error>> {
pub async fn get_adresar_by_position(&mut self, position: u64) -> Result<crate::proto::multieko2::AdresarResponse, Box<dyn std::error::Error>> {
let request = tonic::Request::new(PositionRequest { position: position as i64 }); let request = tonic::Request::new(PositionRequest { position: position as i64 });
let response: AdresarResponse = self.grpc_client.get_adresar_by_position(request).await?.into_inner(); let response: AdresarResponse = self.adresar_client.get_adresar_by_position(request).await?.into_inner();
Ok(response) Ok(response)
} }
pub async fn post_adresar(
pub async fn post_adresar(
&mut self, &mut self,
request: PostAdresarRequest, request: PostAdresarRequest,
) -> Result<tonic::Response<AdresarResponse>, Box<dyn std::error::Error>> { ) -> Result<tonic::Response<AdresarResponse>, Box<dyn std::error::Error>> {
let request = tonic::Request::new(request); let request = tonic::Request::new(request);
let response = self.grpc_client.post_adresar(request).await?; let response = self.adresar_client.post_adresar(request).await?;
Ok(response) Ok(response)
} }
@@ -120,19 +128,18 @@ impl AppTerminal {
request: PutAdresarRequest, request: PutAdresarRequest,
) -> Result<tonic::Response<AdresarResponse>, Box<dyn std::error::Error>> { ) -> Result<tonic::Response<AdresarResponse>, Box<dyn std::error::Error>> {
let request = tonic::Request::new(request); let request = tonic::Request::new(request);
let response = self.grpc_client.put_adresar(request).await?; let response = self.adresar_client.put_adresar(request).await?;
Ok(response) Ok(response)
} }
/// Fetch the table structure once at startup. // Table structure method uses table_structure_client
///
/// TODO: In the future, refactor to subscribe to changes rather than one-time fetch.
pub async fn get_table_structure( pub async fn get_table_structure(
&mut self, &mut self,
) -> Result<crate::proto::multieko2::TableStructureResponse, Box<dyn std::error::Error>> { ) -> Result<TableStructureResponse, Box<dyn std::error::Error>> {
// Note: Adjust the request according to your proto definitions. let request = tonic::Request::new(Empty::default());
let request = tonic::Request::new(crate::proto::multieko2::Empty::default()); let response = self.table_structure_client
let response = self.grpc_client.get_table_structure(request).await?; .get_adresar_table_structure(request)
.await?;
Ok(response.into_inner()) Ok(response.into_inner())
} }
} }

View File

@@ -4,7 +4,7 @@ use crossterm::event::{Event, KeyCode, KeyModifiers};
use crossterm::cursor::{SetCursorStyle}; use crossterm::cursor::{SetCursorStyle};
use crate::client::terminal::AppTerminal; use crate::client::terminal::AppTerminal;
use crate::client::config::Config; use crate::client::config::Config;
use crate::proto::multieko2::{PostAdresarRequest, PutAdresarRequest}; use crate::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
use super::form::FormState; use super::form::FormState;
pub struct EventHandler { pub struct EventHandler {