get -> post all_conv because need payload & response structure
This commit is contained in:
@@ -299,57 +299,42 @@ pub async fn get_convs(
|
||||
|
||||
let conn = match pool.get(){
|
||||
Ok(conn) => conn,
|
||||
Err(err) => {
|
||||
let body = json!({ "error": format!("Pool error: {}", err) });
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(body));
|
||||
}
|
||||
Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Pool error: {err}")),
|
||||
};
|
||||
|
||||
|
||||
let mut stmt = match conn.prepare(
|
||||
"SELECT conversation_id, name FROM conversation_participants WHERE user_id = ?1",
|
||||
) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
let body = json!({ "error": format!("Prepare failed: {}", e) });
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(body) )
|
||||
|
||||
}
|
||||
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Prepare failed: {}", e)),
|
||||
};
|
||||
|
||||
let rows = match stmt.query_map(params![user_id], |row| {
|
||||
let conversation_id: i32 = row.get(0)?;
|
||||
let name: String = row.get(1)?;
|
||||
Ok((conversation_id, name))
|
||||
Ok(Conversation {
|
||||
id: row.get(0)?,
|
||||
title: row.get(1)?,
|
||||
})
|
||||
}) {
|
||||
Ok(rows) => rows,
|
||||
//Ok(_) => {}, IMPLEMENT NO CONV ?
|
||||
Err(e) => {
|
||||
let body = json!({ "error": format!("Query failed: {}", e) });
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(body));
|
||||
}
|
||||
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Query failed: {}", e)),
|
||||
};
|
||||
|
||||
|
||||
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
|
||||
for row_result in rows {
|
||||
match row_result {
|
||||
Ok((id, name)) => {
|
||||
map.insert(id, name);
|
||||
}
|
||||
Err(e) => {
|
||||
let body = json!({ "error": format!("Row parsing failed: {}", e) });
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(body));
|
||||
}
|
||||
}
|
||||
//.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, Json("error".to_string())));
|
||||
|
||||
match serde_json::to_string(&convs) {
|
||||
Ok(json) => (StatusCode::OK, json),
|
||||
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization failed: {}", e)),
|
||||
}
|
||||
|
||||
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("/add_users_conv", put(add_user_to_conv))
|
||||
.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("/hotel_users", get(get_hotel_users))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user