multi-hotel-refactor #3
@@ -299,57 +299,42 @@ pub async fn get_convs(
|
|||||||
|
|
||||||
let conn = match pool.get(){
|
let conn = match pool.get(){
|
||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
Err(err) => {
|
Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Pool error: {err}")),
|
||||||
let body = json!({ "error": format!("Pool error: {}", err) });
|
|
||||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(body));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
let mut stmt = match conn.prepare(
|
let mut stmt = match conn.prepare(
|
||||||
"SELECT conversation_id, name FROM conversation_participants WHERE user_id = ?1",
|
"SELECT conversation_id, name FROM conversation_participants WHERE user_id = ?1",
|
||||||
) {
|
) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(e) => {
|
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Prepare failed: {}", e)),
|
||||||
let body = json!({ "error": format!("Prepare failed: {}", e) });
|
|
||||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(body) )
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let rows = match stmt.query_map(params![user_id], |row| {
|
let rows = match stmt.query_map(params![user_id], |row| {
|
||||||
let conversation_id: i32 = row.get(0)?;
|
let conversation_id: i32 = row.get(0)?;
|
||||||
let name: String = row.get(1)?;
|
let name: String = row.get(1)?;
|
||||||
Ok((conversation_id, name))
|
Ok(Conversation {
|
||||||
|
id: row.get(0)?,
|
||||||
|
title: row.get(1)?,
|
||||||
|
})
|
||||||
}) {
|
}) {
|
||||||
Ok(rows) => rows,
|
Ok(rows) => rows,
|
||||||
//Ok(_) => {}, IMPLEMENT NO CONV ?
|
//Ok(_) => {}, IMPLEMENT NO CONV ?
|
||||||
Err(e) => {
|
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Query failed: {}", e)),
|
||||||
let body = json!({ "error": format!("Query failed: {}", e) });
|
|
||||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(body));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
let mut map = HashMap::new();
|
let convs: Vec<Conversation> = match rows.collect::<Result<Vec<_>, _>>() {
|
||||||
|
Ok(u) => u,
|
||||||
|
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Collect failed: {}", e)),
|
||||||
|
};
|
||||||
|
|
||||||
// ✅ Iterate through the row results
|
//.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, Json("error".to_string())));
|
||||||
for row_result in rows {
|
|
||||||
match row_result {
|
match serde_json::to_string(&convs) {
|
||||||
Ok((id, name)) => {
|
Ok(json) => (StatusCode::OK, json),
|
||||||
map.insert(id, name);
|
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization failed: {}", e)),
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let body = json!({ "error": format!("Row parsing failed: {}", e) });
|
|
||||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(body));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let convs_string = serde_json::to_string(&map)
|
|
||||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, Json("error".to_string())));
|
|
||||||
|
|
||||||
let conv_map_json = to_value(map).unwrap();
|
|
||||||
(StatusCode::OK, Json(conv_map_json))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ pub fn chat_routes() -> Router<AppState> {
|
|||||||
.route("/create_conversation", post (create_conversation))
|
.route("/create_conversation", post (create_conversation))
|
||||||
.route("/add_users_conv", put(add_user_to_conv))
|
.route("/add_users_conv", put(add_user_to_conv))
|
||||||
.route("/send_message", post(send_message))
|
.route("/send_message", post(send_message))
|
||||||
.route("/get_conv", get(get_convs))
|
.route("/get_conv", post(get_convs))
|
||||||
.route("/get_message", get(get_message))
|
.route("/get_message", get(get_message))
|
||||||
.route("/hotel_users", get(get_hotel_users))
|
.route("/hotel_users", get(get_hotel_users))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user