diff --git a/db/1.sqlite b/db/1.sqlite index 919dd66..1258c17 100644 Binary files a/db/1.sqlite and b/db/1.sqlite differ diff --git a/src/chat/extractor.rs b/src/chat/extractor.rs new file mode 100644 index 0000000..2e79e14 --- /dev/null +++ b/src/chat/extractor.rs @@ -0,0 +1,32 @@ +use axum::{ + extract::{ + FromRequest, Request + }, + http::StatusCode, + Json, +}; +use serde::Deserialize; + +#[derive(Deserialize, Debug)] +pub struct CreateConversationValues{ + //pub creator_id: i32, // already in token ? + pub title: String, +} + +pub struct CreateConversationPayload(pub CreateConversationValues); + +impl FromRequest for CreateConversationPayload +where S: Send + Sync, +{ + type Rejection = (StatusCode, String); + + async fn from_request(req: Request, state: &S) -> Result { + let Json(payload) = Json::::from_request(req, state) + .await + .map_err(|err| (StatusCode::BAD_REQUEST, format!("Invalid body: {}", err)))?; + + Ok(CreateConversationPayload(payload)) + + } +} + diff --git a/src/chat/handlers.rs b/src/chat/handlers.rs new file mode 100644 index 0000000..12113ec --- /dev/null +++ b/src/chat/handlers.rs @@ -0,0 +1,44 @@ + +use axum::{ + extract::{ + FromRequest,FromRequestParts, State, + }, + response::IntoResponse, + http::StatusCode, +}; + +use rusqlite::params; + +use crate::chat::extractor::{ +CreateConversationPayload, +}; +use crate::utils::db_pool::{AppState}; +use crate::utils::auth::AuthClaims; + + + +pub async fn create_conversation( + State(state): State, + AuthClaims {user_id, hotel_id, username}: AuthClaims, + CreateConversationPayload(payload): CreateConversationPayload +) -> impl IntoResponse { + + let pool = state.hotel_pools.get_pool(hotel_id); + + let conn = match pool.get(){ + Ok(conn) => conn, + Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Pool error")) + }; + + let result = conn.execute( + "INSERT INTO conversation (creator_id, title) VALUES (?1, ?2)", + params![&user_id, &payload.title], +); + + match result { + Ok(rows) if rows > 0 => (StatusCode::OK, format!("Created conversation {}", payload.title)), + Ok(_) => (StatusCode::NOT_FOUND, "not able to create the conversation".to_string() ), + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error when creating the conversation : {err}")), + + } +} \ No newline at end of file diff --git a/src/chat/mod.rs b/src/chat/mod.rs new file mode 100644 index 0000000..4f1dd12 --- /dev/null +++ b/src/chat/mod.rs @@ -0,0 +1,5 @@ +pub mod routes; + + +mod extractor; +mod handlers; \ No newline at end of file diff --git a/src/chat/routes.rs b/src/chat/routes.rs new file mode 100644 index 0000000..d867310 --- /dev/null +++ b/src/chat/routes.rs @@ -0,0 +1,17 @@ +use axum::{ + Router, + routing::put +}; + +use crate::utils::db_pool::AppState; +use crate::chat::handlers::create_conversation; + + + + + +pub fn chat_routes() -> Router { + + Router::new() + .route("/create_conversation", put (create_conversation)) +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 2449eab..80763e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,3 +4,4 @@ pub mod utils; pub mod routes; pub mod rooms; +pub mod chat; diff --git a/src/main.rs b/src/main.rs index 60b150a..46b8c6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,10 @@ use tokio::net::TcpListener; mod utils; mod routes; mod rooms; +mod chat; use r2d2::{Pool}; use r2d2_sqlite::SqliteConnectionManager; + use crate::utils::db_pool::{HotelPool,AppState}; use routes::create_router; use crate::utils::auth::JwtKeys; diff --git a/src/rooms/handler.rs b/src/rooms/handler.rs index 38717e2..465e43e 100644 --- a/src/rooms/handler.rs +++ b/src/rooms/handler.rs @@ -68,6 +68,10 @@ pub async fn clean_db_update( UpdateRoomPayload(payload): UpdateRoomPayload, ) -> impl IntoResponse { + //TODO: make better error handling : + // if wrong param collumn targeted, + // if missing path param + let pool = state.hotel_pools.get_pool(hotel_id); let conn = match pool.get(){ diff --git a/src/routes/mod.rs b/src/routes/mod.rs index bd2f16d..13549a1 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -6,26 +6,21 @@ use r2d2::{Pool}; use crate::rooms::routes::rooms_routes; use crate::utils::routes::utils_routes; - +use crate::chat::routes::chat_routes; use crate::utils::db_pool::{AppState}; - //TODO: add secret fomr dotenv here - - - /* Function to build our main router that regroup all feature centered router - */ - pub fn create_router(state: AppState) -> Router { Router::new() .nest("/auth", utils_routes().with_state(state.clone())) .nest("/rooms", rooms_routes().with_state(state.clone())) + .nest("/chat", chat_routes().with_state(state.clone())) .with_state(state) }