get all room endpoint + clean fil structure
This commit is contained in:
@@ -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