diff --git a/db/auth_copy_2.sqlite b/db/auth_copy_2.sqlite index c62e835..883fb23 100644 Binary files a/db/auth_copy_2.sqlite and b/db/auth_copy_2.sqlite differ diff --git a/db/auth_copy_2.sqlite-shm b/db/auth_copy_2.sqlite-shm index 14ec0b1..2b84086 100644 Binary files a/db/auth_copy_2.sqlite-shm and b/db/auth_copy_2.sqlite-shm differ diff --git a/db/auth_copy_2.sqlite-wal b/db/auth_copy_2.sqlite-wal index c3cb572..18c009b 100644 Binary files a/db/auth_copy_2.sqlite-wal and b/db/auth_copy_2.sqlite-wal differ diff --git a/src/utils/auth.rs b/src/utils/auth.rs index 90aa563..a1473d4 100644 --- a/src/utils/auth.rs +++ b/src/utils/auth.rs @@ -1,6 +1,6 @@ use std::time::Duration; use axum::{ - Json, body::{Body, to_bytes}, extract::{Extension, FromRequest, FromRequestParts, Path, State}, http::{Request as HttpRequest, StatusCode, header::{HeaderValue, SET_COOKIE}, request::Parts, status }, middleware::Next, response::{IntoResponse, IntoResponseParts, Response} + Json, body::{Body, to_bytes}, extract::{Extension, FromRequest, FromRequestParts, Path, State, ws::close_code::STATUS}, http::{Request as HttpRequest, StatusCode, header::{HeaderValue, SET_COOKIE}, request::Parts, status }, middleware::Next, response::{IntoResponse, IntoResponseParts, Response} }; use axum_extra::extract::TypedHeader; @@ -902,12 +902,101 @@ pub async fn get_hotel( //.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB connection error".to_string()))?; - //return (StatusCode::OK).into_response(); - - + //return (StatusCode::OK).into_response(); } +#[derive(Deserialize, Debug)] +pub struct addHotelUser{ + user_id:i32, + #[serde(default)] + hotel_ids: Vec, +} + + +pub async fn add_hotel_user( + + State(state): State, + Extension(keys): Extension, + Json(payload): Json +) -> impl IntoResponse { + + let conn = match state.logs_pool.get() { + Ok(c) => c, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, + "DB connection error").into_response() + }; + + let user_name: String= match conn.query_row( + "SELECT username FROM users WHERE id = ?1", + params![&payload.user_id], + |row| row.get(0), + ) { + Ok(name) => name, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, + format!("user not found {e} ")).into_response() + }; + + let mut get_hotel_name_stmt = match conn.prepare( + "SELECT hotelname FROM hotels WHERE id = ?1" + ) { + Ok(stmt) => stmt, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, + format!("could't prepare stmt for hotel : {e} ")).into_response() + }; + + let mut insert_hotel_link_stmt = match conn.prepare( + "INSERT INTO hotel_user_link + (user_id,hotel_id,username,hotelname) + VALUES (?1,?2,?3,?4)", + ) { + Ok(stmt) => stmt, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, + format!("could't prepare stmt to insert hotel : {e} ")).into_response() + }; + + for &hotel_id in &payload.hotel_ids{ + + let hotel_name: String = match get_hotel_name_stmt.query_row( + params![hotel_id], + |row| row.get(0), + ) { + Ok(name) => name, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, + format!("hotel not found {e} ")).into_response() + }; + + let add_link = match conn.execute( + "INSERT INTO hotel_user_link + (user_id,hotel_id,username,hotelname) + VALUES (?1,?2,?3,?4)", + params![ + payload.user_id, + hotel_id, + user_name, + hotel_name + ],) { + + Ok(_) => true, + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, + format!("hotel not found {e} ")).into_response() + + }; + +//TODO: still need to build the add hotel to user here + + }; + + + + + + + return(StatusCode::OK, "goo").into_response(); +} + + + fn internal_error(err: E) -> (StatusCode, String) { (StatusCode::INTERNAL_SERVER_ERROR, format!("Internal error: {}", err)) } \ No newline at end of file diff --git a/src/utils/routes.rs b/src/utils/routes.rs index a1774c6..2c74457 100644 --- a/src/utils/routes.rs +++ b/src/utils/routes.rs @@ -30,6 +30,7 @@ pub fn utils_routes() -> Router { .route("/force_update_password", put(force_update_password)) .route("/get_hotels", get(get_hotel)) + .route("/add_hotel_user", put(add_hotel_user)) //.with_state(state)