diff --git a/db/1.sqlite-shm b/db/1.sqlite-shm index b2face3..a0e2931 100644 Binary files a/db/1.sqlite-shm and b/db/1.sqlite-shm differ diff --git a/db/1.sqlite-wal b/db/1.sqlite-wal index 7af4a3a..d35d8df 100644 Binary files a/db/1.sqlite-wal and b/db/1.sqlite-wal differ diff --git a/src/chat/extractor.rs b/src/chat/extractor.rs index 4041c4a..8d7fb45 100644 --- a/src/chat/extractor.rs +++ b/src/chat/extractor.rs @@ -13,7 +13,7 @@ use serde::Deserialize; #[derive(Deserialize, Debug)] pub struct CreateConversationValues{ //pub creator_id: i32, // already in token ? - pub title: String, + pub name: String, } pub struct CreateConversationPayload(pub CreateConversationValues); diff --git a/src/chat/handlers.rs b/src/chat/handlers.rs index 6945c7e..c60b24e 100644 --- a/src/chat/handlers.rs +++ b/src/chat/handlers.rs @@ -22,7 +22,8 @@ use crate::utils::db_pool::{AppState}; use crate::utils::auth::AuthClaims; - +//TODO: update conversation title +//FIXME: make a default title if empty pub async fn create_conversation( State(state): State, AuthClaims {user_id, hotel_id}: AuthClaims, @@ -37,18 +38,51 @@ pub async fn create_conversation( }; let result = conn.execute( - "INSERT INTO conversation (creator_id, title) VALUES (?1, ?2)", - params![&user_id, &payload.title], -); + "INSERT INTO conversation (creator_id, name) VALUES (?1, ?2)", + params![&user_id, &payload.name], + ); - 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}")), - + let rows = match result { + Ok(rows) => rows, + Err(err) => { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Error when creating the conversation: {err}") + ); + } + }; + + if rows == 0 { + return ( + StatusCode::NOT_FOUND, + "not able to create the conversation".to_string() + ); } + + + let conv_id = conn.last_insert_rowid(); + + let user_conv_rel = conn.execute( + "INSERT INTO conversation_participants (conversation_id, user_id, name) + VALUES (?1, ?2, ?3)", + params![conv_id, user_id, payload.name] + ); + + match user_conv_rel { + Ok(r) => return (StatusCode::OK, format!("Created conversation {}", payload.name)), + Err(err) => return ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Error when creating the conversation: {err}") + ) + } + + // extra logic here (more queries, validations, etc.) + + //(StatusCode::OK, format!("Created conversation {}", payload.name)) + } +//FIXME: add title to conv pub async fn add_user_to_conv( State(state): State, AuthClaims {user_id, hotel_id}: AuthClaims, @@ -62,16 +96,15 @@ pub async fn add_user_to_conv( Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Pool error")) }; - let is_creator = match conn + let creator_name: Option = match conn .query_row( - "SELECT 1 FROM conversation WHERE creator_id = ?1 AND id = ?2", + "SELECT name FROM conversation WHERE creator_id = ?1 AND id = ?2", params![user_id, payload.conv_id], - |_| Ok(()), + |row| row.get(0), ) - .optional() - { - Ok(Some(_)) => true, - Ok(None) => false, + .optional() { + Ok(name) => name, + //Ok(None) => false, Err(_) => { return ( StatusCode::INTERNAL_SERVER_ERROR, @@ -80,6 +113,8 @@ pub async fn add_user_to_conv( } }; + let is_creator = creator_name.is_some(); + if !is_creator { return ( StatusCode::FORBIDDEN, @@ -87,9 +122,8 @@ pub async fn add_user_to_conv( ); } - //fix this -let existing: HashSet = { + let existing: HashSet = { let mut stmt = match conn.prepare( "SELECT user_id FROM conversation_participants WHERE conversation_id = ?1", ) { @@ -104,14 +138,14 @@ let existing: HashSet = { match stmt.query_map(params![payload.conv_id], |row| row.get(0)) { Ok(rows) => rows.filter_map(Result::ok).collect(), - Err(_) => { - return ( - StatusCode::INTERNAL_SERVER_ERROR, - "Query participants failed".to_string(), - ) + Err(_) => { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + "Query participants failed".to_string(), + ) + } } - } -}; // ← stmt dropped HERE + }; // ← stmt dropped HERE let payload_users: HashSet = payload.users.into_iter().collect(); @@ -137,9 +171,9 @@ let existing: HashSet = { for user_id in to_add { if let Err(err) = tx.execute( - "INSERT INTO conversation_participants (conversation_id, user_id) - VALUES (?1, ?2)", - params![payload.conv_id, user_id], + "INSERT INTO conversation_participants (conversation_id, user_id, name) + VALUES (?1, ?2, ?3)", + params![payload.conv_id, user_id, creator_name], ) { return ( StatusCode::INTERNAL_SERVER_ERROR, @@ -421,6 +455,7 @@ struct Conversation { title: String, } +//FIXME: allow null conv name ? default to persons name pub async fn get_convs( State(state): State, //Path((item_name, item_amount)): Path<(String, i32)>,