implemented simple chat conversation creation endpoint
This commit is contained in:
44
src/chat/handlers.rs
Normal file
44
src/chat/handlers.rs
Normal file
@@ -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<AppState>,
|
||||
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}")),
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user