From 2ce0d2f38b52722570012380a9d08c52ab613215 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 1 Jul 2026 22:13:16 -0400 Subject: [PATCH] Adding endpoint to get user profile --- src/callers/login.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++ src/callers/mod.rs | 2 ++ src/lib.rs | 16 ++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/callers/login.rs b/src/callers/login.rs index d9216b2..db38435 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -120,6 +120,12 @@ pub mod response { pub message: String, pub data: Vec, } + + #[derive(Default, Deserialize, Serialize, utoipa::ToSchema)] + pub struct GetUserProfileResponse { + pub message: String, + pub data: Vec, + } } /// Endpoint for a user login @@ -796,3 +802,51 @@ pub async fn update_name_of_user( } } } + +/// Endpoint to get User Profile +#[utoipa::path( + delete, + path = super::endpoints::GET_USER_PROFILE, + params(("id" = uuid::Uuid, Path, description = "User Id")), + responses( + (status = 200, description = "Deleted", body = response::GetUserProfileResponse), + (status = 400, description = "Bad request", body = response::GetUserProfileResponse), + (status = 500, description = "Error", body = response::GetUserProfileResponse) + ) +)] +pub async fn get_user_profile( + axum::Extension(pool): axum::Extension, + axum::extract::Path(id): axum::extract::Path, +) -> ( + axum::http::StatusCode, + axum::Json, +) { + let mut response = response::GetUserProfileResponse::default(); + if id.is_nil() { + response.message = String::from("Invalid"); + return (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)); + } + + match repo::user::get_with_id(&pool, &id).await { + Ok(user) => { + let user_profile = textsender_models::user::UserProfile { + user_id: user.id, + phone_number: user.phone_number, + firstname: user.firstname, + lastname: user.lastname, + last_login: user.last_login, + created: user.created, + username: user.username + }; + response.message = String::from(super::messages::SUCCESSFUL_MESSAGE); + response.data.push(user_profile); + + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + eprintln!("Error: {err:?}"); + response.message = String::from("Bad request"); + (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) + } + } +} diff --git a/src/callers/mod.rs b/src/callers/mod.rs index 0dbf0a8..2e19209 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -19,4 +19,6 @@ pub mod endpoints { pub const UPDATE_PASSWORD: &str = "/api/v1/user/password/update"; /// Endpoint constant for updating user's name pub const UPDATE_USER_NAME: &str = "/api/v1/user/name/update"; + /// Endpoint constant for getting user profile + pub const GET_USER_PROFILE: &str = "/api/v1/user/profile/{id}"; } diff --git a/src/lib.rs b/src/lib.rs index d56cab5..d8e404b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,9 +24,12 @@ pub mod init { paths( common_callers::endpoint::db_ping, common_callers::endpoint::root, register_caller::register_user, login_caller::user_login, + login_caller::get_user_profile, ), components(schemas(common_callers::response::TestResult, - register_responses::Response, login_responses::LoginResponse)), + register_responses::Response, login_responses::LoginResponse, + login_responses::GetUserProfileResponse, + )), tags( (name = "TextSender Auth API", description = "Auth API for TextSender API") ) @@ -39,6 +42,7 @@ pub mod init { let cors = tower_http::cors::CorsLayer::new() .allow_methods([ axum::http::Method::GET, + axum::http::Method::PATCH, axum::http::Method::POST, axum::http::Method::PUT, axum::http::Method::DELETE, @@ -76,6 +80,12 @@ pub mod init { cors.allow_origin(vec![ "http://localhost:4200".parse().unwrap(), "http://127.0.0.1:4200".parse().unwrap(), + "http://localhost:5173".parse().unwrap(), + "http://127.0.0.1:5173".parse().unwrap(), + "http://localhost:9080".parse().unwrap(), + "http://127.0.0.1:9080".parse().unwrap(), + "http://localhost:9081".parse().unwrap(), + "http://127.0.0.1:9081".parse().unwrap(), ]) } } @@ -118,6 +128,10 @@ pub mod init { callers::endpoints::UPDATE_USER_NAME, patch(callers::login::update_name_of_user), ) + .route( + callers::endpoints::GET_USER_PROFILE, + patch(callers::login::get_user_profile), + ) .layer(cors::configure_cors().await) }