implemented simple chat conversation creation endpoint
This commit is contained in:
BIN
db/1.sqlite
BIN
db/1.sqlite
Binary file not shown.
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))
|
||||
}
|
||||
@@ -4,3 +4,4 @@
|
||||
pub mod utils;
|
||||
pub mod routes;
|
||||
pub mod rooms;
|
||||
pub mod chat;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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(){
|
||||
|
||||
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user