This commit is contained in:
@@ -1,22 +1,20 @@
|
||||
use axum::{Json, extract::Path, extract::State };
|
||||
use axum::response::IntoResponse;
|
||||
use axum::http::StatusCode;
|
||||
use axum::extract::ws::Message;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::IntoResponse;
|
||||
use axum::{Json, extract::Path, extract::State};
|
||||
use serde::Serialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::rooms::extractor::UpdateRoomPayload;
|
||||
//use crate::utils::db_pool::*;
|
||||
use crate::utils::auth::AuthClaims;
|
||||
use crate::utils::db_pool::{HotelPool,AppState};
|
||||
use crate::utils::db_pool::{AppState, HotelPool};
|
||||
|
||||
use std::sync::Arc;
|
||||
use r2d2::{Pool};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use dashmap::DashMap;
|
||||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use rusqlite::params;
|
||||
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
pub async fn hello_rooms() -> String {
|
||||
"hello from rooms".to_string()
|
||||
@@ -28,11 +26,15 @@ pub async fn fake_db_update(
|
||||
Path(room_id): Path<i32>,
|
||||
UpdateRoomPayload(payload): UpdateRoomPayload,
|
||||
) -> 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}")),
|
||||
Err(err) => {
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Pool error: {err}"),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let result = conn.execute(
|
||||
@@ -41,9 +43,15 @@ pub async fn fake_db_update(
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(rows) if rows > 0 => (StatusCode::OK, format!("Updated room {room_id} in hotel {}", hotel_id)),
|
||||
Ok(rows) if rows > 0 => (
|
||||
StatusCode::OK,
|
||||
format!("Updated room {room_id} in hotel {}", hotel_id),
|
||||
),
|
||||
Ok(_) => (StatusCode::NOT_FOUND, "No room found".to_string()),
|
||||
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("DB error: {err}")),
|
||||
Err(err) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("DB error: {err}"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,16 +61,20 @@ pub async fn clean_db_update(
|
||||
AuthClaims { user_id, hotel_id }: AuthClaims,
|
||||
UpdateRoomPayload(payload): UpdateRoomPayload,
|
||||
) -> impl IntoResponse {
|
||||
|
||||
//TODO: make better error handling :
|
||||
// if wrong param collumn targeted,
|
||||
// if missing path param
|
||||
// if wrong param collumn targeted,
|
||||
// if missing path param
|
||||
|
||||
let pool = state.hotel_pools.get_pool(hotel_id);
|
||||
|
||||
let conn = match pool.get(){
|
||||
|
||||
let conn = match pool.get() {
|
||||
Ok(conn) => conn,
|
||||
Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Pool error: {err}")),
|
||||
Err(err) => {
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Pool error: {err}"),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let result: Result<String, rusqlite::Error> = conn.query_row(
|
||||
@@ -71,7 +83,6 @@ pub async fn clean_db_update(
|
||||
|row| row.get(0),
|
||||
);
|
||||
|
||||
|
||||
match result {
|
||||
Ok(room_number) => {
|
||||
// --- broadcast to all WS clients in the hotel ---
|
||||
@@ -79,7 +90,10 @@ pub async fn clean_db_update(
|
||||
"INSERT INTO room_history (room_id, room_number, status) VALUES (?1, ?2, ?3)",
|
||||
params![&room_id, &room_number, &payload.status],
|
||||
) {
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, format!("Failed to insert history: {err}"));
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Failed to insert history: {err}"),
|
||||
);
|
||||
}
|
||||
if let Some(hotel_users) = state.ws_map.get(&hotel_id) {
|
||||
let update_msg = json!({
|
||||
@@ -97,10 +111,19 @@ pub async fn clean_db_update(
|
||||
}
|
||||
}
|
||||
|
||||
(StatusCode::OK, format!("updated room {room_id} in hotel {hotel_id}, with status: {}", payload.status))
|
||||
(
|
||||
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}"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,50 +138,65 @@ 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(){
|
||||
|
||||
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)?,
|
||||
})
|
||||
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)),
|
||||
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)),
|
||||
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)),
|
||||
Err(e) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Serialization failed: {}", e),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct RoomRequest {
|
||||
item_id: i32,
|
||||
item_amount: i32,
|
||||
token: String,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user