get all room endpoint + clean fil structure

This commit is contained in:
2025-09-26 05:40:13 +02:00
parent 91ae1e4e75
commit 32fe134743
7 changed files with 102 additions and 24 deletions

View File

@@ -160,13 +160,6 @@ pub async fn send_message(
Ok(_) => (StatusCode::NOT_FOUND, "Conversation not found".to_string()),
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error from DB: {err}")),
}
/*
match result {
Ok(rows) if rows > 0 => (StatusCode::OK, "added message succesfully".to_string()),
Ok(_) => (StatusCode::NOT_FOUND, "not able to add the message, conversation may not exist".to_string() ),
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error when adding the message : {err}")),
}
*/
}
@@ -222,4 +215,51 @@ pub async fn get_message(
Ok((StatusCode::OK, serde_json::to_string(&messages).unwrap()))
}
#[derive(Serialize)]
struct User {
id: i32,
username: String,
display_name: String,
}
pub async fn get_hotel_users(
State(state): State<AppState>,
AuthClaims { hotel_id, .. }: AuthClaims,
) -> impl IntoResponse {
let conn = match state.logs_pool.get() {
Ok(c) => c,
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "DB connection error".to_string()),
};
let mut stmt = match conn.prepare(
"SELECT id, username, displayname FROM users WHERE hotel_id = ?1",
) {
Ok(s) => s,
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Prepare failed: {}", e)),
};
let users_iter = match stmt.query_map(params![hotel_id], |row| {
Ok(User {
id: row.get(0)?,
username: row.get(1)?,
display_name: row.get(2)?,
})
}) {
Ok(iter) => iter,
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Query failed: {}", e)),
};
let users: Vec<User> = match users_iter.collect::<Result<Vec<_>, _>>() {
Ok(u) => u,
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("Collect failed: {}", e)),
};
match serde_json::to_string(&users) {
Ok(json) => (StatusCode::OK, json),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization failed: {}", e)),
}
}