multi-hotel-refactor #3
BIN
db/1.sqlite-shm
BIN
db/1.sqlite-shm
Binary file not shown.
BIN
db/1.sqlite-wal
BIN
db/1.sqlite-wal
Binary file not shown.
@@ -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);
|
||||
|
||||
@@ -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<AppState>,
|
||||
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<AppState>,
|
||||
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<String> = 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,7 +122,6 @@ pub async fn add_user_to_conv(
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
//fix this
|
||||
let existing: HashSet<u32> = {
|
||||
let mut stmt = match conn.prepare(
|
||||
@@ -137,9 +171,9 @@ let existing: HashSet<u32> = {
|
||||
|
||||
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<AppState>,
|
||||
//Path((item_name, item_amount)): Path<(String, i32)>,
|
||||
|
||||
Reference in New Issue
Block a user