From f9d729a0cbe8ac25aeb4c7046ce07dbf7836aab9 Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 8 Oct 2026 13:15:26 -0400 Subject: [PATCH] Adding functionality --- src/callers/login.rs | 51 ++++++++++++++++++++++++++++++++++++++++ src/callers/mod.rs | 20 ++++++++++++++++ src/repo/mod.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) diff --git a/src/callers/login.rs b/src/callers/login.rs index f84a1ac..5e5525b 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -43,6 +43,7 @@ pub mod request { } } } + } pub mod response { @@ -79,6 +80,16 @@ pub mod response { pub data: Vec, } } + + pub mod get_user_profile { + use serde::{Deserialize, Serialize}; + + #[derive(Default, Deserialize, Serialize, utoipa::ToSchema)] + pub struct GetUserProfileResponse { + pub message: String, + pub data: Vec, + } + } } /// 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, + axum::extract::Path(id): axum::extract::Path, + ) -> (axum::http::StatusCode, axum::Json) { + 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)) + } + } + } + } } diff --git a/src/callers/mod.rs b/src/callers/mod.rs index 512f720..a025e1a 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -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, + axum::extract::Path(id): axum::extract::Path, +) -> (axum::http::StatusCode, axum::Json) { +} +*/ diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 3346e6c..5e10b49 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -44,6 +44,62 @@ pub mod user { } } + + pub async fn get_with_id( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + ) -> Result { + 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 { + 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,