From 4a6b5fc68c201919388055544068a44f118bbfe3 Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 12 Jun 2026 16:59:43 -0400 Subject: [PATCH] Adding code for endpoint --- src/callers/login.rs | 111 +++++++++++++++++++++++++++++++++++++++++++ src/repo/mod.rs | 28 +++++++++++ 2 files changed, 139 insertions(+) diff --git a/src/callers/login.rs b/src/callers/login.rs index 60ad921..f887f32 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -59,6 +59,24 @@ pub mod request { } } } + + #[derive(Deserialize, utoipa::ToSchema)] + pub struct UserUpdateNameRequest { + pub user_id: uuid::Uuid, + pub firstname: String, + pub lastname: String, + } + + impl UserUpdateNameRequest { + pub fn is_valid(&self) -> (bool, Option) { + if self.user_id.is_nil() || self.firstname.is_empty() || self.lastname.is_empty() { + let reason = String::from("Missing fields"); + (false, Some(reason)) + } else { + (true, None) + } + } + } } pub mod response { @@ -96,6 +114,12 @@ pub mod response { pub message: String, pub data: Vec, } + + #[derive(Deserialize, Serialize, utoipa::ToSchema)] + pub struct UserUpdateNameResponse { + pub message: String, + pub data: Vec, + } } /// Endpoint for a user login @@ -715,3 +739,90 @@ pub async fn update_password( } } } + +/// Endpoint for a updating password +#[utoipa::path( + patch, + path = super::endpoints::UPDATE_USER_NAME, + request_body( + content = request::UserUpdateNameRequest, + description = "Data required to update name of a user", + content_type = "application/json" + ), + responses( + (status = 200, description = "User login successful", body = response::UserUpdateNameResponse), + (status = 400, description = "Bad data", body = response::UserUpdateNameResponse), + (status = 500, description = "Something went wrong", body = response::UserUpdateNameResponse) + ) +)] +pub async fn update_name_of_user( + axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json, +) -> ( + axum::http::StatusCode, + axum::Json, +) { + let (valid_request, reason) = payload.is_valid(); + if !valid_request { + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response::UserUpdateNameResponse { + message: reason.unwrap_or_default(), + data: Vec::new(), + }), + ) + } else { + match repo::user::get_with_id(&pool, &payload.user_id).await { + Ok(user) => { + if user.firstname == payload.firstname || user.lastname == payload.lastname { + ( + axum::http::StatusCode::NOT_MODIFIED, + axum::Json(response::UserUpdateNameResponse { + message: String::from("No change"), + data: Vec::new(), + }), + ) + } else { + match repo::user::update_name( + &pool, + &user.id, + &payload.firstname, + &payload.lastname, + ) + .await + { + Ok(_) => ( + axum::http::StatusCode::OK, + axum::Json(response::UserUpdateNameResponse { + message: String::from(super::messages::SUCCESSFUL_MESSAGE), + data: vec![textsender_models::user::User { + id: user.id, + firstname: payload.firstname, + lastname: payload.lastname, + username: user.username, + created: user.created, + last_login: user.last_login, + ..Default::default() + }], + }), + ), + Err(err) => ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response::UserUpdateNameResponse { + message: err.to_string(), + data: Vec::new(), + }), + ), + } + } + } + Err(err) => ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response::UserUpdateNameResponse { + message: err.to_string(), + data: Vec::new(), + }), + ), + } + } +} diff --git a/src/repo/mod.rs b/src/repo/mod.rs index d0ff331..81f37a9 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -130,6 +130,34 @@ pub mod user { } } + pub async fn update_name( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + firstname: &str, + lastname: &str, + ) -> Result<(), sqlx::Error> { + match sqlx::query( + r#" + UPDATE "user" SET firstname = $1, lastname = $2 WHERE id = $3 + "#, + ) + .bind(firstname) + .bind(lastname) + .bind(id) + .execute(pool) + .await + { + Ok(row) => { + if row.rows_affected() > 0 { + Ok(()) + } else { + Err(sqlx::Error::RowNotFound) + } + } + Err(err) => Err(err), + } + } + pub async fn exists(pool: &sqlx::PgPool, username: &String) -> Result { let result = sqlx::query( r#"