rebuild file structur + simple sqlite db endpoint
This commit is contained in:
79
src/rooms/handler.rs
Normal file
79
src/rooms/handler.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
use axum::{Json, extract::Path, extract::State };
|
||||
use axum::response::IntoResponse;
|
||||
use axum::http::StatusCode;
|
||||
|
||||
use crate::rooms::extractor::UpdateRoomPayload;
|
||||
|
||||
use std::sync::Arc;
|
||||
use r2d2::{Pool};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use dashmap::DashMap;
|
||||
use rusqlite::params;
|
||||
use crate::utils::db_pool::*;
|
||||
|
||||
|
||||
pub async fn hello_rooms() -> String {
|
||||
"hello from rooms".to_string()
|
||||
}
|
||||
|
||||
//fake handler
|
||||
pub async fn fake_room_update(
|
||||
Path(room_id): Path<i32>,
|
||||
UpdateRoomPayload(payload): UpdateRoomPayload
|
||||
) -> impl IntoResponse {
|
||||
|
||||
format!(
|
||||
"Got: token={}, status={}, room_id={}",
|
||||
payload.token, payload.status, room_id
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn fake_db_update(
|
||||
|
||||
State(hotel_pools): State<HotelPools>,
|
||||
Path(room_id): Path<i32>,
|
||||
UpdateRoomPayload(payload): UpdateRoomPayload,
|
||||
|
||||
) -> impl IntoResponse {
|
||||
|
||||
let pool = hotel_pools.get_pool(payload.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 {}", payload.hotel_id)),
|
||||
Ok(_) => (StatusCode::NOT_FOUND, "No room found".to_string()),
|
||||
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("DB error: {err}")),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
//fake db handler
|
||||
pub async fn update_room(UpdateRoomPayload(payload): UpdateRoom) -> impl IntoResponse {
|
||||
match fake_db_update(&payload.token, payload.room_id, &payload.status).await {
|
||||
Ok(msg) => msg,
|
||||
Err(err) => format!("Error: {}", err),
|
||||
}
|
||||
}
|
||||
|
||||
async fn fake_db_update(token: &str, room_id: i32, status: &str) -> Result<String, String> {
|
||||
// Pretend we check the token in the DB
|
||||
if token != "valid_token" {
|
||||
return Err("Invalid token".into());
|
||||
}
|
||||
|
||||
// Pretend we update the room status in the DB
|
||||
Ok(format!("Room {} updated to '{}'", room_id, status))
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user