websocket for single db update

This commit is contained in:
2025-09-26 04:05:22 +02:00
parent 65146fce91
commit a3b55f00df
6 changed files with 38 additions and 23 deletions

View File

@@ -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}")),
}
}