Compare commits
1
Commits
v0.8.8
..
587405c995
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
587405c995
|
No files matched your search
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user