diff --git a/src/rooms/extractor.rs b/src/rooms/extractor.rs index 61cf4af..d068ff9 100644 --- a/src/rooms/extractor.rs +++ b/src/rooms/extractor.rs @@ -10,8 +10,6 @@ use serde::Deserialize; #[derive(Deserialize, Debug)] pub struct UpdateRoomValues { pub status: String, - pub token: String, - pub hotel_id: i32, } pub struct UpdateRoomPayload(pub UpdateRoomValues); diff --git a/src/rooms/handler.rs b/src/rooms/handler.rs index f1158ec..38717e2 100644 --- a/src/rooms/handler.rs +++ b/src/rooms/handler.rs @@ -3,8 +3,9 @@ use axum::response::IntoResponse; use axum::http::StatusCode; use crate::rooms::extractor::UpdateRoomPayload; -use crate::utils::db_pool::*; - +//use crate::utils::db_pool::*; +use crate::utils::auth::AuthClaims; +use crate::utils::db_pool::{HotelPool,AppState}; use std::sync::Arc; use r2d2::{Pool}; @@ -25,18 +26,19 @@ pub async fn fake_room_update( ) -> impl IntoResponse { format!( - "Got: token={}, status={}, room_id={}", - payload.token, payload.status, room_id + "Got: status={}, room_id={}", + payload.status, room_id ) } pub async fn fake_db_update( State(state): State, + AuthClaims { user_id, hotel_id, username }: AuthClaims, Path(room_id): Path, UpdateRoomPayload(payload): UpdateRoomPayload, ) -> impl IntoResponse { - let pool = state.hotel_pools.get_pool(payload.hotel_id); + 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}")), @@ -50,11 +52,39 @@ pub async fn fake_db_update( match result { - Ok(rows) if rows > 0 => (StatusCode::OK, format!("Updated room {room_id} in hotel {}", payload.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}")), } + + +} + +pub async fn clean_db_update( + State(state): State, + Path(room_id): Path, + AuthClaims { user_id, hotel_id, username }: AuthClaims, + 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}")), + }; + + let result = conn.execute( + "UPDATE rooms SET status = ?1 WHERE number = ?2", + params![&payload.status, &room_id], + ); + + match result { + Ok(rows) if rows > 0 => (StatusCode::OK, format!("updated room {room_id} in hotel{}, with status : {}", hotel_id, payload.status )), + Ok(_) => (StatusCode::NOT_FOUND, "No room found".to_string()), + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error from DB : {err}")), + } } struct RoomRequest { diff --git a/src/rooms/routes.rs b/src/rooms/routes.rs index c541db1..8dc4f3b 100644 --- a/src/rooms/routes.rs +++ b/src/rooms/routes.rs @@ -12,10 +12,10 @@ use crate::utils::db_pool::{ // ROOTS -pub fn rooms_routes() -> Router { +pub fn rooms_routes() -> Router { Router::new() .route("/", get(hello_rooms) ) .route("/fakeUpdate/{room_id}", put(fake_room_update)) - + .route("/clean_db_update/{room_id}", put(clean_db_update)) } \ No newline at end of file diff --git a/src/routes/inventory.rs b/src/routes/inventory.rs deleted file mode 100644 index 430594b..0000000 --- a/src/routes/inventory.rs +++ /dev/null @@ -1,15 +0,0 @@ -use axum::{ - routing::{get, //post - }, - Router, -}; - -pub fn inventory_routes() -> Router { - - Router::new() - .route("/", get(hi_inventory) ) -} - -async fn hi_inventory() -> &'static str { - "Hiii from inventory.rs route module" -} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 70862cd..bd2f16d 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -7,19 +7,26 @@ use r2d2::{Pool}; use crate::rooms::routes::rooms_routes; use crate::utils::routes::utils_routes; -pub mod inventory; + use crate::utils::db_pool::{AppState}; //TODO: add secret fomr dotenv here +/* +Function to build our main router +that regroup all feature centered router + +*/ + pub fn create_router(state: AppState) -> Router { Router::new() - - .nest("/auth", utils_routes()) - .with_state(state) // 👈 hotel_db is passed in as argument + + .nest("/auth", utils_routes().with_state(state.clone())) + .nest("/rooms", rooms_routes().with_state(state.clone())) + .with_state(state) } diff --git a/src/utils/auth.rs b/src/utils/auth.rs index 160dfc5..d8af5ce 100644 --- a/src/utils/auth.rs +++ b/src/utils/auth.rs @@ -32,7 +32,7 @@ pub struct JwtKeys { pub async fn token_tester( State(state): State, - Extension(keys): Extension, + //Extension(keys): Extension, AuthClaims { user_id, hotel_id, username }: AuthClaims, ) -> impl IntoResponse { format!(