mirror of https://github.com/buster-so/buster.git
Refactor user routes to include new endpoint for retrieving user by ID
- Removed the public modifier from `get_user` and `update_user` modules to encapsulate them within the module. - Added a new route to the user router for fetching a user by their ID, enhancing the API's functionality. - This change improves the user management capabilities by allowing retrieval of specific user details based on their unique identifier.
This commit is contained in:
parent
1f1df4a7bb
commit
d74214a910
|
@ -0,0 +1,8 @@
|
|||
use axum::{routing::get, Router};
|
||||
|
||||
mod users;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route("/:id/users", get(users::list_organization_users))
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
use anyhow::Result;
|
||||
use axum::{extract::Path, Extension, http::StatusCode};
|
||||
use diesel::{ExpressionMethods, QueryDsl, JoinOnDsl};
|
||||
use diesel_async::RunQueryDsl;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
database::{
|
||||
enums::{UserOrganizationRole, UserOrganizationStatus},
|
||||
lib::get_pg_pool,
|
||||
models::User,
|
||||
schema::{users, users_to_organizations},
|
||||
},
|
||||
routes::rest::ApiResponse,
|
||||
utils::clients::sentry_utils::send_sentry_error,
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct UserResponse {
|
||||
pub id: Uuid,
|
||||
pub name: Option<String>,
|
||||
pub email: String,
|
||||
pub role: UserOrganizationRole,
|
||||
pub status: UserOrganizationStatus,
|
||||
}
|
||||
|
||||
pub async fn list_organization_users(
|
||||
Extension(user): Extension<User>,
|
||||
Path(organization_id): Path<Uuid>,
|
||||
) -> Result<ApiResponse<Vec<UserResponse>>, (StatusCode, &'static str)> {
|
||||
let users = match list_organization_users_handler(organization_id).await {
|
||||
Ok(users) => users,
|
||||
Err(e) => {
|
||||
tracing::error!("Error listing organization users: {:?}", e);
|
||||
send_sentry_error(&e.to_string(), Some(&user.id));
|
||||
return Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Error listing organization users",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(ApiResponse::JsonData(users))
|
||||
}
|
||||
|
||||
async fn list_organization_users_handler(organization_id: Uuid) -> Result<Vec<UserResponse>> {
|
||||
let mut conn = get_pg_pool().get().await?;
|
||||
|
||||
let users = users::table
|
||||
.inner_join(users_to_organizations::table.on(users::id.eq(users_to_organizations::user_id)))
|
||||
.select((
|
||||
users::id,
|
||||
users::email,
|
||||
users::name.nullable(),
|
||||
users_to_organizations::role,
|
||||
users_to_organizations::status,
|
||||
))
|
||||
.filter(users_to_organizations::organization_id.eq(organization_id))
|
||||
.filter(users_to_organizations::deleted_at.is_null())
|
||||
.load::<(Uuid, String, Option<String>, UserOrganizationRole, UserOrganizationStatus)>(&mut conn)
|
||||
.await?;
|
||||
|
||||
Ok(users
|
||||
.into_iter()
|
||||
.map(|(id, email, name, role, status)| UserResponse {
|
||||
id,
|
||||
name,
|
||||
email,
|
||||
role,
|
||||
status,
|
||||
})
|
||||
.collect())
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
use anyhow::Result;
|
||||
use axum::{extract::Path, Extension};
|
||||
use diesel_async::RunQueryDsl;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
database::{
|
||||
enums::{UserOrganizationRole, UserOrganizationStatus},
|
||||
lib::get_pg_pool,
|
||||
models::User,
|
||||
schema::{users, users_to_organizations},
|
||||
},
|
||||
routes::rest::ApiResponse,
|
||||
utils::clients::sentry_utils::send_sentry_error,
|
||||
};
|
||||
use axum::http::StatusCode;
|
||||
use diesel::{ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct UserResponse {
|
||||
pub id: Uuid,
|
||||
pub name: Option<String>,
|
||||
pub email: String,
|
||||
pub role: UserOrganizationRole,
|
||||
pub status: UserOrganizationStatus,
|
||||
}
|
||||
|
||||
pub async fn get_user_by_id(
|
||||
Extension(user): Extension<User>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<ApiResponse<UserResponse>, (StatusCode, &'static str)> {
|
||||
let user_info = match get_user_information(&id).await {
|
||||
Ok(user_info) => user_info,
|
||||
Err(e) => {
|
||||
tracing::error!("Error getting user information: {:?}", e);
|
||||
send_sentry_error(&e.to_string(), Some(&user.id));
|
||||
return Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Error getting user information",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(ApiResponse::JsonData(user_info))
|
||||
}
|
||||
|
||||
pub async fn get_user_information(user_id: &Uuid) -> Result<UserResponse> {
|
||||
let pg_pool = get_pg_pool();
|
||||
let mut conn = pg_pool.get().await?;
|
||||
|
||||
let (user, (role, status)) = users::table
|
||||
.inner_join(users_to_organizations::table.on(users::id.eq(users_to_organizations::user_id)))
|
||||
.select((
|
||||
(users::id, users::email, users::name.nullable()),
|
||||
(users_to_organizations::role, users_to_organizations::status),
|
||||
))
|
||||
.filter(users::id.eq(user_id))
|
||||
.first::<(
|
||||
(Uuid, String, Option<String>),
|
||||
(UserOrganizationRole, UserOrganizationStatus),
|
||||
)>(&mut conn)
|
||||
.await?;
|
||||
|
||||
let (id, email, name) = user;
|
||||
|
||||
Ok(UserResponse {
|
||||
id,
|
||||
name,
|
||||
email,
|
||||
role,
|
||||
status,
|
||||
})
|
||||
}
|
|
@ -3,11 +3,13 @@ use axum::{
|
|||
Router,
|
||||
};
|
||||
|
||||
pub mod get_user;
|
||||
pub mod update_user;
|
||||
mod get_user;
|
||||
mod get_user_by_id;
|
||||
mod update_user;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route("/", get(get_user::get_user))
|
||||
.route("/", put(update_user::update_user))
|
||||
.route("/:id", get(get_user_by_id::get_user_by_id))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue