From 71babdaa09c1018373c3b826554b64cec0b05656 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sat, 22 Feb 2025 18:03:17 +0100 Subject: [PATCH] perfectly working --- src/client/terminal.rs | 57 ++++++++++++++++++--------------- src/client/ui/handlers/event.rs | 2 +- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/client/terminal.rs b/src/client/terminal.rs index 89fd015..0452104 100644 --- a/src/client/terminal.rs +++ b/src/client/terminal.rs @@ -8,19 +8,25 @@ use crossterm::cursor::{SetCursorStyle, EnableBlinking}; use ratatui::{backend::CrosstermBackend, Terminal}; use std::io::{self, stdout}; use tonic::transport::Channel; -use crate::proto::multieko2::{ - adresar_client::AdresarClient, - Empty, CountResponse, PositionRequest, - AdresarResponse, PostAdresarRequest, PutAdresarRequest, -}; + +// Import the correct clients and proto messages from their respective modules +use crate::proto::multieko2::adresar::adresar_client::AdresarClient; +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 { terminal: Terminal>, - grpc_client: AdresarClient, // gRPC client + adresar_client: AdresarClient, + table_structure_client: TableStructureServiceClient, } impl AppTerminal { - pub fn set_cursor_style(&mut self, style: SetCursorStyle) -> Result<(), Box> { + pub fn set_cursor_style( + &mut self, + style: SetCursorStyle, + ) -> Result<(), Box> { execute!( self.terminal.backend_mut(), style, @@ -40,10 +46,11 @@ impl AppTerminal { let backend = CrosstermBackend::new(stdout); let terminal = Terminal::new(backend)?; - // Initialize gRPC client - let grpc_client = AdresarClient::connect("http://[::1]:50051").await?; + // Initialize both gRPC clients + 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(&mut self, f: F) -> Result<(), Box> @@ -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> { 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) } - // Add a method to get an Adresar entry by its position - pub async fn get_adresar_by_position(&mut self, position: u64) -> Result> { + pub async fn get_adresar_by_position(&mut self, position: u64) -> Result> { 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) } - pub async fn post_adresar( + + pub async fn post_adresar( &mut self, request: PostAdresarRequest, ) -> Result, Box> { 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) } @@ -120,19 +128,18 @@ impl AppTerminal { request: PutAdresarRequest, ) -> Result, Box> { 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) } - /// Fetch the table structure once at startup. - /// - /// TODO: In the future, refactor to subscribe to changes rather than one-time fetch. + // Table structure method uses table_structure_client pub async fn get_table_structure( &mut self, - ) -> Result> { - // Note: Adjust the request according to your proto definitions. - let request = tonic::Request::new(crate::proto::multieko2::Empty::default()); - let response = self.grpc_client.get_table_structure(request).await?; + ) -> Result> { + let request = tonic::Request::new(Empty::default()); + let response = self.table_structure_client + .get_adresar_table_structure(request) + .await?; Ok(response.into_inner()) } } diff --git a/src/client/ui/handlers/event.rs b/src/client/ui/handlers/event.rs index a8a0b5c..aa14c4f 100644 --- a/src/client/ui/handlers/event.rs +++ b/src/client/ui/handlers/event.rs @@ -4,7 +4,7 @@ use crossterm::event::{Event, KeyCode, KeyModifiers}; use crossterm::cursor::{SetCursorStyle}; use crate::client::terminal::AppTerminal; use crate::client::config::Config; -use crate::proto::multieko2::{PostAdresarRequest, PutAdresarRequest}; +use crate::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest}; use super::form::FormState; pub struct EventHandler {