diff --git a/src/chat/handlers.rs b/src/chat/handlers.rs index 33c188f..7ae0b0e 100644 --- a/src/chat/handlers.rs +++ b/src/chat/handlers.rs @@ -160,13 +160,6 @@ pub async fn send_message( Ok(_) => (StatusCode::NOT_FOUND, "Conversation not found".to_string()), Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error from DB: {err}")), } -/* - match result { - Ok(rows) if rows > 0 => (StatusCode::OK, "added message succesfully".to_string()), - Ok(_) => (StatusCode::NOT_FOUND, "not able to add the message, conversation may not exist".to_string() ), - Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error when adding the message : {err}")), - } -*/ } @@ -222,4 +215,51 @@ pub async fn get_message( Ok((StatusCode::OK, serde_json::to_string(&messages).unwrap())) +} + + +#[derive(Serialize)] +struct User { + id: i32, + username: String, + display_name: String, +} + + +pub async fn get_hotel_users( + State(state): State, + AuthClaims { hotel_id, .. }: AuthClaims, +) -> impl IntoResponse { + let conn = match state.logs_pool.get() { + Ok(c) => c, + Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "DB connection error".to_string()), + }; + + let mut stmt = match conn.prepare( + "SELECT id, username, displayname FROM users WHERE hotel_id = ?1", + ) { + Ok(s) => s, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Prepare failed: {}", e)), + }; + + let users_iter = match stmt.query_map(params![hotel_id], |row| { + Ok(User { + id: row.get(0)?, + username: row.get(1)?, + display_name: row.get(2)?, + }) + }) { + Ok(iter) => iter, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Query failed: {}", e)), + }; + + let users: Vec = match users_iter.collect::, _>>() { + Ok(u) => u, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Collect failed: {}", e)), + }; + + match serde_json::to_string(&users) { + Ok(json) => (StatusCode::OK, json), + Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization failed: {}", e)), + } } \ No newline at end of file diff --git a/src/chat/routes.rs b/src/chat/routes.rs index ca3f7af..8b98a76 100644 --- a/src/chat/routes.rs +++ b/src/chat/routes.rs @@ -6,9 +6,7 @@ use axum::{ }; use crate::utils::db_pool::AppState; -use crate::chat::handlers::{ - create_conversation,add_user_to_conv,send_message,get_message -}; +use crate::chat::handlers::*; @@ -21,5 +19,6 @@ pub fn chat_routes() -> Router { .route("/add_users_conv", put(add_user_to_conv)) .route("/send_message", post(send_message)) .route("/get_message", get(get_message)) - + .route("/hotel_users", get(get_hotel_users)) + } \ No newline at end of file diff --git a/src/dto/rooms.rs b/src/dto/rooms.rs deleted file mode 100644 index 3e49310..0000000 --- a/src/dto/rooms.rs +++ /dev/null @@ -1,13 +0,0 @@ -use serde::Deserialize; - -#[derive(Debug, Deserialize)] -pub struct UpdateRoomStatusDto{ - pub room_number: i32, - pub status: string, -} - -#[derive(Debug, Seserialize)] -pub struct RoomStatusIdDto{ - pub room_number: i32, - pub status: string, -} diff --git a/src/rooms/handler.rs b/src/rooms/handler.rs index 5429e57..8ea2999 100644 --- a/src/rooms/handler.rs +++ b/src/rooms/handler.rs @@ -2,6 +2,7 @@ use axum::{Json, extract::Path, extract::State }; use axum::response::IntoResponse; use axum::http::StatusCode; use axum::extract::ws::Message; +use serde::Serialize; use serde_json::json; use crate::rooms::extractor::UpdateRoomPayload; @@ -111,6 +112,56 @@ pub async fn clean_db_update( } } +#[derive(Serialize)] +struct Room { + id: i32, + number: String, + status: String, +} + +pub async fn get_all_rooms( + State(state): State, + AuthClaims { hotel_id, .. }: AuthClaims, +) -> impl IntoResponse { + + let pool = state.hotel_pools.get_pool(hotel_id); + + let conn = match pool.get(){ + Ok(conn) => conn, + Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Pool error: {err}")), + }; + + let mut stmt = match conn.prepare( + "SELECT id, number, status FROM rooms ", + ) { + Ok(s) => s, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Prepare failed: {}", e)), + }; + + let room_iter = match stmt.query_map( params![],|row| { + Ok(Room { + id: row.get(0)?, + number: row.get(1)?, + status: row.get(2)?, + }) + }) { + Ok(iter) => iter, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Query failed: {}", e)), + }; + + let rooms: Vec = match room_iter.collect::, _>>() { + Ok(u) => u, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Collect failed: {}", e)), + }; + + match serde_json::to_string(&rooms) { + Ok(json) => (StatusCode::OK, json), + Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization failed: {}", e)), + } +} + + + struct RoomRequest { item_id: i32, item_amount: i32, diff --git a/src/rooms/routes.rs b/src/rooms/routes.rs index 8dc4f3b..48bc350 100644 --- a/src/rooms/routes.rs +++ b/src/rooms/routes.rs @@ -18,4 +18,5 @@ pub fn rooms_routes() -> Router { .route("/", get(hello_rooms) ) .route("/fakeUpdate/{room_id}", put(fake_room_update)) .route("/clean_db_update/{room_id}", put(clean_db_update)) + .route("/rooms", get(get_all_rooms)) } \ No newline at end of file diff --git a/src/services/handlers.rs b/src/services/handlers.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/services/mod.rs b/src/services/mod.rs deleted file mode 100644 index e69de29..0000000