diff --git a/src/callers/login.rs b/src/callers/login.rs index 348c85e..8a9f8f5 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -43,6 +43,23 @@ pub mod request { } } } + + pub mod update_user_profile { + use serde::{Deserialize, Serialize}; + + #[derive(Debug, Deserialize, Serialize, utoipa::ToSchema)] + pub struct UpdateUserProfileRequest { + pub user_id: uuid::Uuid, + pub firstname: String, + pub lastname: String + } + + impl UpdateUserProfileRequest { + pub fn is_valid(&self) -> bool { + !self.user_id.is_nil() && !self.firstname.is_empty() && !self.lastname.is_empty() + } + } + } } pub mod response { @@ -89,6 +106,17 @@ pub mod response { pub data: Vec, } } + + pub mod update_user_profile { + use serde::{Deserialize, Serialize}; + + #[derive(Default, Deserialize, Serialize, utoipa::ToSchema)] + pub struct UpdateUserProfileResponse { + pub message: String, + pub data: Vec, + } + + } } /// Module for login endpoints @@ -446,4 +474,59 @@ pub mod endpoint { } } } + + #[utoipa::path( + put, + path = super::super::endpoints::UPDATE_USER_PROFILE, + request_body( + content = super::request::update_user_profile::UpdateUserProfileRequest, + description = "Update user profile", + content_type = "application/json" + ), + responses( + (status = 200, description = "User profile updated successfully", body = super::response::update_user_profile::UpdateUserProfileResponse), + (status = 400, description = "Invalid", body = super::response::update_user_profile::UpdateUserProfileResponse), + (status = 500, description = "Failure", body = super::response::update_user_profile::UpdateUserProfileResponse) + ) + )] + pub async fn update_user_profile(axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json, + ) -> (axum::http::StatusCode, axum::Json,) { + let mut response = super::response::update_user_profile::UpdateUserProfileResponse::default(); + if !payload.is_valid() { + response.message = "Invalid".to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } else { + let user_id = payload.user_id; + match repo::user::get_with_id(&pool, &user_id).await { + Ok(user) => { + let firstname = payload.firstname; + let lastname = payload.lastname; + match repo::user::update_user_profile(&pool, &user_id, &firstname, &lastname).await { + Ok(_) => { + let user_profile = simodels::user::UserProfile { + firstname, + 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) => { + eprintln!("Error: {err:?}"); + response.message = "ERROR - Updating user profile".to_string(); + (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) + } + } + }, + Err(err) => { + eprintln!("Error: {err:?}"); + response.message = "ERROR".to_string(); + (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) + } + } + } + } } diff --git a/src/callers/mod.rs b/src/callers/mod.rs index 27b8657..e358339 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -11,4 +11,5 @@ pub mod endpoints { 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}"; + pub const UPDATE_USER_PROFILE: &str = "/api/v2/user/profile/update"; } diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 4200934..384b9d6 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -132,6 +132,34 @@ pub mod user { } } + pub async fn update_user_profile( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + firstname: &str, + lastname: &str, + ) -> Result { + match sqlx::query( + r#" + UPDATE "user" SET firstname = $1, lastname = $2 WHERE id = $3 + "#, + ) + .bind(firstname) + .bind(lastname) + .bind(id) + .execute(pool) + .await + { + Ok(r) => { + if r.rows_affected() > 0 { + Ok(true) + } else { + Ok(false) + } + } + Err(err) => Err(err), + } + } + pub async fn update_last_login( pool: &sqlx::PgPool, user: &simodels::user::User,