Adding functionality
This commit is contained in:
3 files changed
+127
No files matched your search
@@ -43,6 +43,7 @@ pub mod request {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
@@ -79,6 +80,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 +399,44 @@ 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() {
|
||||
(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,24 @@ 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}";
|
||||
}
|
||||
|
||||
/*
|
||||
/// 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 profileRequestRequest found", body = super::response::GetUserProfileRequest)),
|
||||
(status = 404, description = "Song not found", body = super::response::GetUserProfileRequest)),
|
||||
(status = 400, description = "Error downloading song", super::response::GetUserProfileRequest)),
|
||||
)
|
||||
)]
|
||||
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<axum::response::GetUserProfileResponse>) {
|
||||
}
|
||||
*/
|
||||
@@ -44,6 +44,62 @@ pub mod user {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||
password: row.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||
email: row.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||
phone: row.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||
firstname: row.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||
lastname: row.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||
email_verified: row.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||
date_created: row.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||
status: row.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||
last_login: row.try_get("id")
|
||||
.map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||
salt_id: row.try_get("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