implemented use of auth on single room endpoint
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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<AppState>,
|
||||
AuthClaims { user_id, hotel_id, username }: AuthClaims,
|
||||
Path(room_id): Path<i32>,
|
||||
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<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 {
|
||||
|
||||
@@ -12,10 +12,10 @@ use crate::utils::db_pool::{
|
||||
|
||||
|
||||
// ROOTS
|
||||
pub fn rooms_routes() -> Router {
|
||||
pub fn rooms_routes() -> Router<AppState> {
|
||||
|
||||
Router::new()
|
||||
.route("/", get(hello_rooms) )
|
||||
.route("/fakeUpdate/{room_id}", put(fake_room_update))
|
||||
|
||||
.route("/clean_db_update/{room_id}", put(clean_db_update))
|
||||
}
|
||||
Reference in New Issue
Block a user