get hotel endpoint
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
use std::time::Duration;
|
||||
use axum::{
|
||||
body::{to_bytes, Body}, extract::{Extension, FromRequest, FromRequestParts, Path, State}, http::{header::{HeaderValue, SET_COOKIE}, request::Parts, Request as HttpRequest, StatusCode }, middleware::Next, response::{IntoResponse, IntoResponseParts, Response}, Json
|
||||
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}
|
||||
};
|
||||
|
||||
use axum_extra::extract::TypedHeader;
|
||||
//use axum_extra::TypedHeader;
|
||||
|
||||
use futures_util::future::TrySelect;
|
||||
use headers::{UserAgent, Cookie};
|
||||
|
||||
use axum::extract::FromRef;
|
||||
@@ -839,6 +840,72 @@ pub async fn logout_from_all_devices (
|
||||
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct HotelData {
|
||||
id: i32,
|
||||
hotel_name: String,
|
||||
}
|
||||
|
||||
pub async fn get_hotel(
|
||||
State(state): State<AppState>
|
||||
|
||||
)-> impl IntoResponse {
|
||||
|
||||
let try_conn = state.logs_pool.get();
|
||||
|
||||
let conn = match try_conn {
|
||||
Ok(conn)=> conn,
|
||||
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, "bruh").into_response()
|
||||
};
|
||||
|
||||
let try_stmt = conn.prepare("
|
||||
SELECT id, hotelname
|
||||
FROM hotels");
|
||||
|
||||
let mut stmt = match try_stmt {
|
||||
Ok(stmt) =>stmt ,
|
||||
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"failed buildin statement")
|
||||
.into_response()
|
||||
};
|
||||
|
||||
let try_hotels = stmt.query_map(params![],
|
||||
|row| {
|
||||
Ok(HotelData {
|
||||
id: row.get(0)?,
|
||||
hotel_name: row.get(1)?,
|
||||
})
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
let hotel_itter = match try_hotels {
|
||||
Ok(hotels) => hotels,
|
||||
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"error processing hotel list")
|
||||
.into_response()
|
||||
};
|
||||
|
||||
let hotels: Vec<HotelData> = match hotel_itter.collect::<Result<Vec<_>, _>>() {
|
||||
Ok(hotel) => hotel,
|
||||
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("failed collection of hotel : {e}"))
|
||||
.into_response()
|
||||
};
|
||||
|
||||
match serde_json::to_string(&hotels) {
|
||||
Ok(json)=> return (StatusCode::OK, json).into_response(),
|
||||
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization failed: {}", e)).into_response()
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
//.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB connection error".to_string()))?;
|
||||
//return (StatusCode::OK).into_response();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
fn internal_error<E: std::fmt::Display>(err: E) -> (StatusCode, String) {
|
||||
|
||||
Reference in New Issue
Block a user