add hotel to user endpoint
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,6 @@
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use axum::{
|
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;
|
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()))?;
|
//.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<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub async fn add_hotel_user(
|
||||||
|
|
||||||
|
State(state): State<AppState>,
|
||||||
|
Extension(keys): Extension<JwtKeys>,
|
||||||
|
Json(payload): Json<addHotelUser>
|
||||||
|
) -> 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<E: std::fmt::Display>(err: E) -> (StatusCode, String) {
|
fn internal_error<E: std::fmt::Display>(err: E) -> (StatusCode, String) {
|
||||||
(StatusCode::INTERNAL_SERVER_ERROR, format!("Internal error: {}", err))
|
(StatusCode::INTERNAL_SERVER_ERROR, format!("Internal error: {}", err))
|
||||||
}
|
}
|
||||||
@@ -30,6 +30,7 @@ pub fn utils_routes() -> Router<AppState> {
|
|||||||
|
|
||||||
.route("/force_update_password", put(force_update_password))
|
.route("/force_update_password", put(force_update_password))
|
||||||
.route("/get_hotels", get(get_hotel))
|
.route("/get_hotels", get(get_hotel))
|
||||||
|
.route("/add_hotel_user", put(add_hotel_user))
|
||||||
|
|
||||||
|
|
||||||
//.with_state(state)
|
//.with_state(state)
|
||||||
|
|||||||
Reference in New Issue
Block a user