implemented simple chat conversation creation endpoint
This commit is contained in:
32
src/chat/extractor.rs
Normal file
32
src/chat/extractor.rs
Normal file
@@ -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<S> FromRequest<S> for CreateConversationPayload
|
||||
where S: Send + Sync,
|
||||
{
|
||||
type Rejection = (StatusCode, String);
|
||||
|
||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let Json(payload) = Json::<CreateConversationValues>::from_request(req, state)
|
||||
.await
|
||||
.map_err(|err| (StatusCode::BAD_REQUEST, format!("Invalid body: {}", err)))?;
|
||||
|
||||
Ok(CreateConversationPayload(payload))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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}")),
|
||||
|
||||
}
|
||||
}
|
||||
5
src/chat/mod.rs
Normal file
5
src/chat/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod routes;
|
||||
|
||||
|
||||
mod extractor;
|
||||
mod handlers;
|
||||
17
src/chat/routes.rs
Normal file
17
src/chat/routes.rs
Normal file
@@ -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<AppState> {
|
||||
|
||||
Router::new()
|
||||
.route("/create_conversation", put (create_conversation))
|
||||
}
|
||||
Reference in New Issue
Block a user