get all room endpoint + clean fil structure

This commit is contained in:
2025-09-26 05:40:13 +02:00
parent 91ae1e4e75
commit 32fe134743
7 changed files with 102 additions and 24 deletions

View File

@@ -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,