implemented use of auth on single room endpoint

This commit is contained in:
2025-09-25 06:00:09 +02:00
parent f315ffbe7b
commit 7828d4f515
6 changed files with 50 additions and 30 deletions

View File

@@ -10,8 +10,6 @@ use serde::Deserialize;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct UpdateRoomValues { pub struct UpdateRoomValues {
pub status: String, pub status: String,
pub token: String,
pub hotel_id: i32,
} }
pub struct UpdateRoomPayload(pub UpdateRoomValues); pub struct UpdateRoomPayload(pub UpdateRoomValues);

View File

@@ -3,8 +3,9 @@ use axum::response::IntoResponse;
use axum::http::StatusCode; use axum::http::StatusCode;
use crate::rooms::extractor::UpdateRoomPayload; 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 std::sync::Arc;
use r2d2::{Pool}; use r2d2::{Pool};
@@ -25,18 +26,19 @@ pub async fn fake_room_update(
) -> impl IntoResponse { ) -> impl IntoResponse {
format!( format!(
"Got: token={}, status={}, room_id={}", "Got: status={}, room_id={}",
payload.token, payload.status, room_id payload.status, room_id
) )
} }
pub async fn fake_db_update( pub async fn fake_db_update(
State(state): State<AppState>, State(state): State<AppState>,
AuthClaims { user_id, hotel_id, username }: AuthClaims,
Path(room_id): Path<i32>, Path(room_id): Path<i32>,
UpdateRoomPayload(payload): UpdateRoomPayload, UpdateRoomPayload(payload): UpdateRoomPayload,
) -> impl IntoResponse { ) -> 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() { let conn = match pool.get() {
Ok(conn) => conn, 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}")),
@@ -50,11 +52,39 @@ pub async fn fake_db_update(
match result { 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()), 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}")),
} }
}
pub async fn clean_db_update(
State(state): State<AppState>,
Path(room_id): Path<i32>,
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 { struct RoomRequest {

View File

@@ -12,10 +12,10 @@ use crate::utils::db_pool::{
// ROOTS // ROOTS
pub fn rooms_routes() -> Router { pub fn rooms_routes() -> Router<AppState> {
Router::new() Router::new()
.route("/", get(hello_rooms) ) .route("/", get(hello_rooms) )
.route("/fakeUpdate/{room_id}", put(fake_room_update)) .route("/fakeUpdate/{room_id}", put(fake_room_update))
.route("/clean_db_update/{room_id}", put(clean_db_update))
} }

View File

@@ -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"
}

View File

@@ -7,19 +7,26 @@ use r2d2::{Pool};
use crate::rooms::routes::rooms_routes; use crate::rooms::routes::rooms_routes;
use crate::utils::routes::utils_routes; use crate::utils::routes::utils_routes;
pub mod inventory;
use crate::utils::db_pool::{AppState}; use crate::utils::db_pool::{AppState};
//TODO: add secret fomr dotenv here //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 { pub fn create_router(state: AppState) -> Router {
Router::new() Router::new()
.nest("/auth", utils_routes()) .nest("/auth", utils_routes().with_state(state.clone()))
.with_state(state) // 👈 hotel_db is passed in as argument .nest("/rooms", rooms_routes().with_state(state.clone()))
.with_state(state)
} }

View File

@@ -32,7 +32,7 @@ pub struct JwtKeys {
pub async fn token_tester( pub async fn token_tester(
State(state): State<AppState>, State(state): State<AppState>,
Extension(keys): Extension<JwtKeys>, //Extension(keys): Extension<JwtKeys>,
AuthClaims { user_id, hotel_id, username }: AuthClaims, AuthClaims { user_id, hotel_id, username }: AuthClaims,
) -> impl IntoResponse { ) -> impl IntoResponse {
format!( format!(