diff --git a/db/1.sqlite b/db/1.sqlite index 189b6e1..9dcef9e 100644 Binary files a/db/1.sqlite and b/db/1.sqlite differ diff --git a/db/auth.sqlite b/db/auth.sqlite index cd1d46d..555f62a 100644 Binary files a/db/auth.sqlite and b/db/auth.sqlite differ diff --git a/src/main.rs b/src/main.rs index 3344437..b0828a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,9 +21,12 @@ use crate::utils::auth::JwtKeys; + #[tokio::main] async fn main() -> std::io::Result<()> { + + let hotel_pools = HotelPool::new(); let logs_manager = SqliteConnectionManager::file("db/auth.sqlite"); let logs_pool = Pool::builder() @@ -31,13 +34,6 @@ async fn main() -> std::io::Result<()> { .build(logs_manager) .expect("Failed to build logs pool"); - type UserMap = DashMap>; - /// hotel_id → users - type HotelMap = DashMap>; - /// global map of all hotels - type WsMap = Arc; - - let state = AppState { hotel_pools, logs_pool, diff --git a/src/rooms/handler.rs b/src/rooms/handler.rs index 465e43e..5429e57 100644 --- a/src/rooms/handler.rs +++ b/src/rooms/handler.rs @@ -1,6 +1,8 @@ use axum::{Json, extract::Path, extract::State }; use axum::response::IntoResponse; use axum::http::StatusCode; +use axum::extract::ws::Message; +use serde_json::json; use crate::rooms::extractor::UpdateRoomPayload; //use crate::utils::db_pool::*; @@ -85,9 +87,27 @@ pub async fn clean_db_update( ); match result { - Ok(rows) if rows > 0 => (StatusCode::OK, format!("updated room {room_id} in hotel{}, with status : {}", hotel_id, payload.status )), + Ok(rows) if rows > 0 => { + // --- broadcast to all WS clients in the hotel --- + if let Some(hotel_users) = state.ws_map.get(&hotel_id) { + let update_msg = json!({ + "room_id": room_id, + "status": payload.status, + "updated_by": user_id, + }) + .to_string(); + + for entry in hotel_users.iter() { + let sender = entry.value(); + // ignore errors (client disconnected) + let _ = sender.send(Message::Text(update_msg.clone().into())); + } + } + + (StatusCode::OK, format!("updated room {room_id} in hotel {hotel_id}, with status: {}", payload.status)) + } Ok(_) => (StatusCode::NOT_FOUND, "No room found".to_string()), - Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error from DB : {err}")), + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error from DB: {err}")), } } diff --git a/src/utils/db_pool.rs b/src/utils/db_pool.rs index aa51e04..6be8c1f 100644 --- a/src/utils/db_pool.rs +++ b/src/utils/db_pool.rs @@ -5,19 +5,18 @@ use r2d2_sqlite::SqliteConnectionManager; use tokio::sync::mpsc; use axum::extract::{ws::{Message, WebSocket, WebSocketUpgrade}, State}; +use crate::utils::websocket::WsMap; + + #[derive(Clone)] + pub struct AppState { + pub hotel_pools: HotelPool, + pub logs_pool: Pool, + pub ws_map: WsMap, + } + + type HotelId = i32; // or i32 if you want numeric ids - - -#[derive(Clone)] -pub struct AppState { - pub hotel_pools: HotelPool, - pub logs_pool: Pool, - pub ws_map: WsMap, - -} - - #[derive(Clone)] pub struct HotelPool { hotel_pools: Arc>>, diff --git a/src/utils/websocket.rs b/src/utils/websocket.rs index 64867f4..793cdb5 100644 --- a/src/utils/websocket.rs +++ b/src/utils/websocket.rs @@ -12,11 +12,11 @@ use crate::utils::{auth::AuthClaims, db_pool::{AppState, HotelPool}}; /// Type alias: user_id → sender to that user -type UserMap = DashMap>; +pub type UserMap = DashMap>; /// hotel_id → users -type HotelMap = DashMap>; +pub type HotelMap = DashMap>; /// global map of all hotels -type WsMap = Arc; +pub type WsMap = Arc; /// Type alias: user_id → sender to that user