Get user profile (#7)
soaricarus_auth Build / Check (push) Successful in 58s
soaricarus_auth Build / Rustfmt (push) Successful in 1m1s
Release Tagging / release (push) Successful in 1m2s
soaricarus_auth Build / Clippy (push) Successful in 1m7s
soaricarus_auth Build / build (push) Successful in 1m9s
soaricarus_auth Build / Test Suite (push) Successful in 1m20s
soaricarus_auth Build / Check (pull_request) Successful in 36s
soaricarus_auth Build / Test Suite (pull_request) Successful in 49s
soaricarus_auth Build / Rustfmt (pull_request) Successful in 41s
soaricarus_auth Build / Clippy (pull_request) Successful in 47s
soaricarus_auth Build / build (pull_request) Successful in 37s
soaricarus_auth Build / Check (push) Successful in 58s
soaricarus_auth Build / Rustfmt (push) Successful in 1m1s
Release Tagging / release (push) Successful in 1m2s
soaricarus_auth Build / Clippy (push) Successful in 1m7s
soaricarus_auth Build / build (push) Successful in 1m9s
soaricarus_auth Build / Test Suite (push) Successful in 1m20s
soaricarus_auth Build / Check (pull_request) Successful in 36s
soaricarus_auth Build / Test Suite (pull_request) Successful in 49s
soaricarus_auth Build / Rustfmt (pull_request) Successful in 41s
soaricarus_auth Build / Clippy (pull_request) Successful in 47s
soaricarus_auth Build / build (pull_request) Successful in 37s
Reviewed-on: http://git.kundeng.us/phoenix/soaricarus_auth/pulls/7
This commit was merged in pull request #7.
This commit is contained in:
1 parent
5ff75f66b8
commit
0c0e1bdde3
10 files changed
+290
-257
No files matched your search
@@ -79,6 +79,16 @@ pub mod response {
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
pub mod get_user_profile {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
|
||||
pub struct GetUserProfileResponse {
|
||||
pub message: String,
|
||||
pub data: Vec<simodels::user::UserProfile>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Module for login endpoints
|
||||
@@ -388,4 +398,51 @@ pub mod endpoint {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Endpoint to get user profile
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = super::super::endpoints::GET_USER_PROFILE,
|
||||
params(("id" = uuid::Uuid, Path, description = "Get user profile")),
|
||||
responses(
|
||||
(status = 200, description = "User profile found", body = super::response::get_user_profile::GetUserProfileResponse),
|
||||
(status = 404, description = "Song not found", body = super::response::get_user_profile::GetUserProfileResponse),
|
||||
(status = 400, description = "Error downloading song", body = super::response::get_user_profile::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<super::response::get_user_profile::GetUserProfileResponse>,
|
||||
) {
|
||||
let mut response = super::response::get_user_profile::GetUserProfileResponse::default();
|
||||
if id.is_nil() {
|
||||
response.message = "Invalid request".to_string();
|
||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||
} else {
|
||||
match repo::user::get_with_id(&pool, &id).await {
|
||||
Ok(user) => {
|
||||
let user_profile = simodels::user::UserProfile {
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
};
|
||||
|
||||
response.message = "SUCCESSFUL".to_string();
|
||||
response.data.push(user_profile);
|
||||
(axum::http::StatusCode::OK, axum::Json(response))
|
||||
}
|
||||
Err(err) => {
|
||||
response.message = err.to_string();
|
||||
(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(response),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,5 @@ pub mod endpoints {
|
||||
pub const UPDATE_PASSWORD: &str = "/api/v2/user/password";
|
||||
pub const SERVICE_LOGIN: &str = "/api/v2/service/login";
|
||||
pub const REFRESH_TOKEN: &str = "/api/v2/token/refresh";
|
||||
pub const GET_USER_PROFILE: &str = "/api/v2/user/profile/{id}";
|
||||
}
|
||||
+6
-2
@@ -38,11 +38,11 @@ mod init {
|
||||
paths(
|
||||
common_callers::endpoint::db_ping, common_callers::endpoint::root,
|
||||
register_caller::register_user,
|
||||
login_endpoints::login, login_endpoints::update_password, login_endpoints::service_login, login_endpoints::refresh_token
|
||||
login_endpoints::login, login_endpoints::update_password, login_endpoints::service_login, login_endpoints::refresh_token, login_endpoints::get_user_profile,
|
||||
),
|
||||
components(schemas(common_callers::response::TestResult,
|
||||
register_responses::Response,
|
||||
login_responses::Response, login_responses::service_login::Response, login_responses::update_password::Response, login_responses::refresh_token::Response)),
|
||||
login_responses::Response, login_responses::service_login::Response, login_responses::update_password::Response, login_responses::refresh_token::Response, login_responses::get_user_profile::GetUserProfileResponse)),
|
||||
tags(
|
||||
(name = "soaricarus Auth API", description = "Auth API for soaricarus API")
|
||||
)
|
||||
@@ -128,6 +128,10 @@ mod init {
|
||||
callers::endpoints::REFRESH_TOKEN,
|
||||
post(callers::login::endpoint::refresh_token),
|
||||
)
|
||||
.route(
|
||||
callers::endpoints::GET_USER_PROFILE,
|
||||
get(callers::login::endpoint::get_user_profile),
|
||||
)
|
||||
.layer(cors::configure_cors().await)
|
||||
}
|
||||
|
||||
|
||||
+76
-14
@@ -24,26 +24,88 @@ pub mod user {
|
||||
|
||||
match result {
|
||||
Ok(r) => match r {
|
||||
Some(r) => Ok(simodels::user::User {
|
||||
id: r.try_get("id")?,
|
||||
username: r.try_get("username")?,
|
||||
password: r.try_get("password")?,
|
||||
email: r.try_get("email")?,
|
||||
email_verified: r.try_get("email_verified")?,
|
||||
phone: r.try_get("phone")?,
|
||||
salt_id: r.try_get("salt_id")?,
|
||||
firstname: r.try_get("firstname")?,
|
||||
lastname: r.try_get("lastname")?,
|
||||
date_created: r.try_get("date_created")?,
|
||||
last_login: r.try_get("last_login")?,
|
||||
status: r.try_get("status")?,
|
||||
}),
|
||||
Some(r) => parse_row(&r).await,
|
||||
None => Err(sqlx::Error::RowNotFound),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_with_id(
|
||||
pool: &sqlx::PgPool,
|
||||
id: &uuid::Uuid,
|
||||
) -> Result<simodels::user::User, sqlx::Error> {
|
||||
let result = sqlx::query(
|
||||
r#"
|
||||
SELECT * FROM "user" WHERE id = $1
|
||||
"#,
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_optional(pool)
|
||||
.await;
|
||||
|
||||
match result {
|
||||
Ok(r) => match r {
|
||||
Some(roww) => parse_row(&roww).await,
|
||||
None => Err(sqlx::Error::RowNotFound),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
async fn parse_row(row: &sqlx::postgres::PgRow) -> Result<simodels::user::User, sqlx::Error> {
|
||||
Ok(simodels::user::User {
|
||||
id: row
|
||||
.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
username: row
|
||||
.try_get("username")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
password: row
|
||||
.try_get("password")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
email: row
|
||||
.try_get("email")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
phone: row
|
||||
.try_get("phone")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
firstname: row
|
||||
.try_get("firstname")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
lastname: row
|
||||
.try_get("lastname")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
email_verified: row
|
||||
.try_get("email_verified")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
date_created: row
|
||||
.try_get("date_created")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
status: row
|
||||
.try_get("status")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
last_login: row
|
||||
.try_get("last_login")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
salt_id: row
|
||||
.try_get("salt_id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||
.unwrap(),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn update_password(
|
||||
pool: &sqlx::PgPool,
|
||||
user: &simodels::user::User,
|
||||
|
||||
Reference in new issue
Block a user