Compare commits

...
1 Commits
Author SHA1 Message Date
phoenix 587405c995 tsk-14: Got it working
soaricarus_auth Build / Rustfmt (pull_request) Failing after 22s
soaricarus_auth Build / Check (pull_request) Successful in 27s
soaricarus_auth Build / Clippy (pull_request) Successful in 1m48s
soaricarus_auth Build / build (pull_request) Successful in 2m0s
soaricarus_auth Build / Test Suite (pull_request) Successful in 2m33s
2026-11-10 21:17:13 -05:00
3 changed files with 112 additions and 0 deletions

No files matched your search

+83
View File
@@ -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<simodels::user::UserProfile>,
}
}
pub mod update_user_profile {
use serde::{Deserialize, Serialize};
#[derive(Default, Deserialize, Serialize, utoipa::ToSchema)]
pub struct UpdateUserProfileResponse {
pub message: String,
pub data: Vec<simodels::user::UserProfile>,
}
}
}
/// 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<sqlx::PgPool>,
axum::Json(payload): axum::Json<super::request::update_user_profile::UpdateUserProfileRequest>,
) -> (axum::http::StatusCode, axum::Json<super::response::update_user_profile::UpdateUserProfileResponse>,) {
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))
}
}
}
}
}
+1
View File
@@ -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";
}
+28
View File
@@ -132,6 +132,34 @@ pub mod user {
}
}
pub async fn update_user_profile(
pool: &sqlx::PgPool,
id: &uuid::Uuid,
firstname: &str,
lastname: &str,
) -> Result<bool, 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(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,