rebuild file structur + simple sqlite db endpoint

This commit is contained in:
2025-09-04 22:32:53 +02:00
parent 3db51cc805
commit 3450c216c0
20 changed files with 748 additions and 49 deletions

39
src/rooms/extractor.rs Normal file
View File

@@ -0,0 +1,39 @@
use axum::{
extract::{Request, FromRequest, Path},
body::{Body},
http::StatusCode,
Json, Router,
};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct UpdateRoomValues {
pub status: String,
pub token: String,
pub hotel_id: i32,
}
pub struct UpdateRoomPayload(pub UpdateRoomValues);
//#[async_trait]
impl<S> FromRequest<S> for UpdateRoomPayload
where S: Send + Sync,
{
type Rejection = (StatusCode, String);
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
let Json(payload) = Json::<UpdateRoomValues>::from_request(req, state)
.await
.map_err(|err| (StatusCode::BAD_REQUEST, format!("Invalid body: {}", err)))?;
Ok(UpdateRoomPayload(payload))
}
}
#[derive(Debug)]
pub struct RoomIdValue {
pub room_id: i32
}
pub type RoomIdPath = Path<i32>;

79
src/rooms/handler.rs Normal file
View 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))
}
*/

6
src/rooms/mod.rs Normal file
View File

@@ -0,0 +1,6 @@
//pub mod handler;
//pub mod routes;
mod handler;
mod extractor;
pub mod routes;

10
src/rooms/model.rs Normal file
View File

@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
pub struct Room{
pub id: i32,
pub room_number: i32,
pub status: String
}

17
src/rooms/routes.rs Normal file
View File

@@ -0,0 +1,17 @@
use axum::{
routing::{get, put,},
Router,
};
use crate::rooms::handler::*;
use crate::utils::db_pool::HotelPools;
// ROOTS
pub fn rooms_routes() -> Router<HotelPools> {
Router::new()
.route("/", get(hello_rooms) )
.route("/fakeUpdate/{room_id}", put(fake_room_update))
.route("/fake_db_update/{room_id}", put(fake_db_update))
}