get all room endpoint + clean fil structure
This commit is contained in:
@@ -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<AppState>,
|
||||
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<User> = match users_iter.collect::<Result<Vec<_>, _>>() {
|
||||
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)),
|
||||
}
|
||||
}
|
||||
@@ -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<AppState> {
|
||||
.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))
|
||||
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
@@ -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<AppState>,
|
||||
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<Room> = match room_iter.collect::<Result<Vec<_>, _>>() {
|
||||
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,
|
||||
|
||||
@@ -18,4 +18,5 @@ pub fn rooms_routes() -> Router<AppState> {
|
||||
.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))
|
||||
}
|
||||
Reference in New Issue
Block a user