Adding endpoint to get user profile
This commit is contained in:
@@ -120,6 +120,12 @@ pub mod response {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::user::User>,
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||
pub struct GetUserProfileResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<textsender_models::user::UserProfile>,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<sqlx::PgPool>,
|
||||
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<response::GetUserProfileResponse>,
|
||||
) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}";
|
||||
}
|
||||
|
||||
+15
-1
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user