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)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct CreateConversationValues{
|
pub struct CreateConversationValues{
|
||||||
//pub creator_id: i32, // already in token ?
|
//pub creator_id: i32, // already in token ?
|
||||||
pub title: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CreateConversationPayload(pub CreateConversationValues);
|
pub struct CreateConversationPayload(pub CreateConversationValues);
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ use crate::utils::db_pool::{AppState};
|
|||||||
use crate::utils::auth::AuthClaims;
|
use crate::utils::auth::AuthClaims;
|
||||||
|
|
||||||
|
|
||||||
|
//TODO: update conversation title
|
||||||
|
//FIXME: make a default title if empty
|
||||||
pub async fn create_conversation(
|
pub async fn create_conversation(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
AuthClaims {user_id, hotel_id}: AuthClaims,
|
AuthClaims {user_id, hotel_id}: AuthClaims,
|
||||||
@@ -37,18 +38,51 @@ pub async fn create_conversation(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let result = conn.execute(
|
let result = conn.execute(
|
||||||
"INSERT INTO conversation (creator_id, title) VALUES (?1, ?2)",
|
"INSERT INTO conversation (creator_id, name) VALUES (?1, ?2)",
|
||||||
params![&user_id, &payload.title],
|
params![&user_id, &payload.name],
|
||||||
);
|
);
|
||||||
|
|
||||||
match result {
|
let rows = match result {
|
||||||
Ok(rows) if rows > 0 => (StatusCode::OK, format!("Created conversation {}", payload.title)),
|
Ok(rows) => rows,
|
||||||
Ok(_) => (StatusCode::NOT_FOUND, "not able to create the conversation".to_string() ),
|
Err(err) => {
|
||||||
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error when creating the conversation : {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(
|
pub async fn add_user_to_conv(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
AuthClaims {user_id, hotel_id}: AuthClaims,
|
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"))
|
Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Pool error"))
|
||||||
};
|
};
|
||||||
|
|
||||||
let is_creator = match conn
|
let creator_name: Option<String> = match conn
|
||||||
.query_row(
|
.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],
|
params![user_id, payload.conv_id],
|
||||||
|_| Ok(()),
|
|row| row.get(0),
|
||||||
)
|
)
|
||||||
.optional()
|
.optional() {
|
||||||
{
|
Ok(name) => name,
|
||||||
Ok(Some(_)) => true,
|
//Ok(None) => false,
|
||||||
Ok(None) => false,
|
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return (
|
return (
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
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 {
|
if !is_creator {
|
||||||
return (
|
return (
|
||||||
StatusCode::FORBIDDEN,
|
StatusCode::FORBIDDEN,
|
||||||
@@ -87,9 +122,8 @@ pub async fn add_user_to_conv(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//fix this
|
//fix this
|
||||||
let existing: HashSet<u32> = {
|
let existing: HashSet<u32> = {
|
||||||
let mut stmt = match conn.prepare(
|
let mut stmt = match conn.prepare(
|
||||||
"SELECT user_id FROM conversation_participants WHERE conversation_id = ?1",
|
"SELECT user_id FROM conversation_participants WHERE conversation_id = ?1",
|
||||||
) {
|
) {
|
||||||
@@ -104,14 +138,14 @@ let existing: HashSet<u32> = {
|
|||||||
|
|
||||||
match stmt.query_map(params![payload.conv_id], |row| row.get(0)) {
|
match stmt.query_map(params![payload.conv_id], |row| row.get(0)) {
|
||||||
Ok(rows) => rows.filter_map(Result::ok).collect(),
|
Ok(rows) => rows.filter_map(Result::ok).collect(),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return (
|
return (
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
"Query participants failed".to_string(),
|
"Query participants failed".to_string(),
|
||||||
)
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}; // ← stmt dropped HERE
|
||||||
}; // ← stmt dropped HERE
|
|
||||||
|
|
||||||
let payload_users: HashSet<u32> = payload.users.into_iter().collect();
|
let payload_users: HashSet<u32> = payload.users.into_iter().collect();
|
||||||
|
|
||||||
@@ -137,9 +171,9 @@ let existing: HashSet<u32> = {
|
|||||||
|
|
||||||
for user_id in to_add {
|
for user_id in to_add {
|
||||||
if let Err(err) = tx.execute(
|
if let Err(err) = tx.execute(
|
||||||
"INSERT INTO conversation_participants (conversation_id, user_id)
|
"INSERT INTO conversation_participants (conversation_id, user_id, name)
|
||||||
VALUES (?1, ?2)",
|
VALUES (?1, ?2, ?3)",
|
||||||
params![payload.conv_id, user_id],
|
params![payload.conv_id, user_id, creator_name],
|
||||||
) {
|
) {
|
||||||
return (
|
return (
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
@@ -421,6 +455,7 @@ struct Conversation {
|
|||||||
title: String,
|
title: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//FIXME: allow null conv name ? default to persons name
|
||||||
pub async fn get_convs(
|
pub async fn get_convs(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
//Path((item_name, item_amount)): Path<(String, i32)>,
|
//Path((item_name, item_amount)): Path<(String, i32)>,
|
||||||
|
|||||||
Reference in New Issue
Block a user