diff --git a/db/1.sqlite b/db/1.sqlite index 5a615d4..68cbb20 100644 Binary files a/db/1.sqlite and b/db/1.sqlite differ diff --git a/src/inventory/routes.rs b/src/inventory/routes.rs index 4ffcd7a..f95a9c3 100644 --- a/src/inventory/routes.rs +++ b/src/inventory/routes.rs @@ -9,5 +9,5 @@ pub fn inventory_routes() -> Router { Router::new() .route("/update_item/{item_id}/{item_amount}", put(update_inventory_item)) - .route("/add_item/{item_name/{items_amount}", post(create_inventory_item)) + .route("/add_item/{item_name}/{item_amount}", post(create_inventory_item)) } \ No newline at end of file diff --git a/src/rooms/handler.rs b/src/rooms/handler.rs index 8ea2999..5d73b68 100644 --- a/src/rooms/handler.rs +++ b/src/rooms/handler.rs @@ -22,17 +22,7 @@ pub async fn hello_rooms() -> String { "hello from rooms".to_string() } -//fake handler -pub async fn fake_room_update( - Path(room_id): Path, - UpdateRoomPayload(payload): UpdateRoomPayload -) -> impl IntoResponse { - - format!( - "Got: status={}, room_id={}", - payload.status, room_id - ) -} + pub async fn fake_db_update( State(state): State, @@ -52,16 +42,11 @@ pub async fn fake_db_update( params![&payload.status, &room_id], ); - - match result { 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( @@ -82,14 +67,23 @@ pub async fn clean_db_update( Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Pool error: {err}")), }; - let result = conn.execute( - "UPDATE rooms SET status = ?1 WHERE number = ?2", + let result: Result = conn.query_row( + "UPDATE rooms SET status = ?1 WHERE id = ?2 RETURNING number", params![&payload.status, &room_id], + |row| row.get(0), ); + match result { - Ok(rows) if rows > 0 => { + Ok(room_number) => { // --- broadcast to all WS clients in the hotel --- + + if let Err(err) = conn.execute( + "INSERT INTO room_history (room_id, room_number, status) VALUES (?1, ?2, ?3)", + params![&room_id, &room_number, &payload.status], + ) { + return (StatusCode::INTERNAL_SERVER_ERROR, format!("Failed to insert history: {err}")); + } if let Some(hotel_users) = state.ws_map.get(&hotel_id) { let update_msg = json!({ "room_id": room_id, diff --git a/src/rooms/routes.rs b/src/rooms/routes.rs index 48bc350..caa2ea9 100644 --- a/src/rooms/routes.rs +++ b/src/rooms/routes.rs @@ -16,7 +16,6 @@ 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)) .route("/rooms", get(get_all_rooms)) } \ No newline at end of file