style: apply rustfmt to backend codebase
- Apply rustfmt to all Rust source files in backend/ - Fix trailing whitespace inconsistencies - Standardize formatting across handlers, models, and services - Improve code readability with consistent formatting These changes are purely stylistic and do not affect functionality. All CI checks now pass with proper formatting.
This commit is contained in:
parent
6b7e4d4016
commit
ee0feb77ef
41 changed files with 1266 additions and 819 deletions
|
|
@ -1,19 +1,9 @@
|
|||
use axum::{
|
||||
extract::{State},
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
Json,
|
||||
Extension,
|
||||
};
|
||||
use axum::{extract::State, http::StatusCode, response::IntoResponse, Extension, Json};
|
||||
use mongodb::bson::oid::ObjectId;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
use mongodb::bson::oid::ObjectId;
|
||||
|
||||
use crate::{
|
||||
auth::jwt::Claims,
|
||||
config::AppState,
|
||||
models::user::User,
|
||||
};
|
||||
use crate::{auth::jwt::Claims, config::AppState, models::user::User};
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct UserProfileResponse {
|
||||
|
|
@ -27,7 +17,7 @@ pub struct UserProfileResponse {
|
|||
|
||||
impl TryFrom<User> for UserProfileResponse {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
|
||||
fn try_from(user: User) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
id: user.id.map(|id| id.to_string()).unwrap_or_default(),
|
||||
|
|
@ -51,22 +41,28 @@ pub async fn get_profile(
|
|||
Extension(claims): Extension<Claims>,
|
||||
) -> impl IntoResponse {
|
||||
let user_id = ObjectId::parse_str(&claims.sub).unwrap();
|
||||
|
||||
|
||||
match state.db.find_user_by_id(&user_id).await {
|
||||
Ok(Some(user)) => {
|
||||
let response: UserProfileResponse = user.try_into().unwrap();
|
||||
(StatusCode::OK, Json(response)).into_response()
|
||||
}
|
||||
Ok(None) => {
|
||||
(StatusCode::NOT_FOUND, Json(serde_json::json!({
|
||||
Ok(None) => (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(serde_json::json!({
|
||||
"error": "user not found"
|
||||
}))).into_response()
|
||||
}
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get user profile: {}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "failed to get profile"
|
||||
}))).into_response()
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "failed to get profile"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -77,33 +73,45 @@ pub async fn update_profile(
|
|||
Json(req): Json<UpdateProfileRequest>,
|
||||
) -> impl IntoResponse {
|
||||
if let Err(errors) = req.validate() {
|
||||
return (StatusCode::BAD_REQUEST, Json(serde_json::json!({
|
||||
"error": "validation failed",
|
||||
"details": errors.to_string()
|
||||
}))).into_response();
|
||||
return (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!({
|
||||
"error": "validation failed",
|
||||
"details": errors.to_string()
|
||||
})),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
|
||||
|
||||
let user_id = ObjectId::parse_str(&claims.sub).unwrap();
|
||||
|
||||
|
||||
let mut user = match state.db.find_user_by_id(&user_id).await {
|
||||
Ok(Some(u)) => u,
|
||||
Ok(None) => {
|
||||
return (StatusCode::NOT_FOUND, Json(serde_json::json!({
|
||||
"error": "user not found"
|
||||
}))).into_response()
|
||||
return (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(serde_json::json!({
|
||||
"error": "user not found"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get user: {}", e);
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "database error"
|
||||
}))).into_response()
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "database error"
|
||||
})),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
if let Some(username) = req.username {
|
||||
user.username = username;
|
||||
}
|
||||
|
||||
|
||||
match state.db.update_user(&user).await {
|
||||
Ok(_) => {
|
||||
let response: UserProfileResponse = user.try_into().unwrap();
|
||||
|
|
@ -111,9 +119,13 @@ pub async fn update_profile(
|
|||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to update user: {}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "failed to update profile"
|
||||
}))).into_response()
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "failed to update profile"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -123,14 +135,18 @@ pub async fn delete_account(
|
|||
Extension(claims): Extension<Claims>,
|
||||
) -> impl IntoResponse {
|
||||
let user_id = ObjectId::parse_str(&claims.sub).unwrap();
|
||||
|
||||
|
||||
match state.db.delete_user(&user_id).await {
|
||||
Ok(_) => (StatusCode::NO_CONTENT, ()).into_response(),
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to delete user: {}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "failed to delete account"
|
||||
}))).into_response()
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "failed to delete account"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -149,63 +165,91 @@ pub async fn change_password(
|
|||
Json(req): Json<ChangePasswordRequest>,
|
||||
) -> impl IntoResponse {
|
||||
if let Err(errors) = req.validate() {
|
||||
return (StatusCode::BAD_REQUEST, Json(serde_json::json!({
|
||||
"error": "validation failed",
|
||||
"details": errors.to_string()
|
||||
}))).into_response();
|
||||
return (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!({
|
||||
"error": "validation failed",
|
||||
"details": errors.to_string()
|
||||
})),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
|
||||
|
||||
let user_id = ObjectId::parse_str(&claims.sub).unwrap();
|
||||
|
||||
|
||||
let mut user = match state.db.find_user_by_id(&user_id).await {
|
||||
Ok(Some(u)) => u,
|
||||
Ok(None) => {
|
||||
return (StatusCode::NOT_FOUND, Json(serde_json::json!({
|
||||
"error": "user not found"
|
||||
}))).into_response()
|
||||
return (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(serde_json::json!({
|
||||
"error": "user not found"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get user: {}", e);
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "database error"
|
||||
}))).into_response()
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "database error"
|
||||
})),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Verify current password
|
||||
match user.verify_password(&req.current_password) {
|
||||
Ok(true) => {},
|
||||
Ok(true) => {}
|
||||
Ok(false) => {
|
||||
return (StatusCode::UNAUTHORIZED, Json(serde_json::json!({
|
||||
"error": "current password is incorrect"
|
||||
}))).into_response()
|
||||
return (
|
||||
StatusCode::UNAUTHORIZED,
|
||||
Json(serde_json::json!({
|
||||
"error": "current password is incorrect"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to verify password: {}", e);
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "failed to verify password"
|
||||
}))).into_response()
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "failed to verify password"
|
||||
})),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Update password
|
||||
match user.update_password(req.new_password) {
|
||||
Ok(_) => {},
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to hash new password: {}", e);
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "failed to update password"
|
||||
}))).into_response()
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "failed to update password"
|
||||
})),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
match state.db.update_user(&user).await {
|
||||
Ok(_) => (StatusCode::NO_CONTENT, ()).into_response(),
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to update user: {}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "failed to update password"
|
||||
}))).into_response()
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "failed to update password"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -230,22 +274,28 @@ pub async fn get_settings(
|
|||
Extension(claims): Extension<Claims>,
|
||||
) -> impl IntoResponse {
|
||||
let user_id = ObjectId::parse_str(&claims.sub).unwrap();
|
||||
|
||||
|
||||
match state.db.find_user_by_id(&user_id).await {
|
||||
Ok(Some(user)) => {
|
||||
let response: UserSettingsResponse = user.into();
|
||||
(StatusCode::OK, Json(response)).into_response()
|
||||
}
|
||||
Ok(None) => {
|
||||
(StatusCode::NOT_FOUND, Json(serde_json::json!({
|
||||
Ok(None) => (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(serde_json::json!({
|
||||
"error": "user not found"
|
||||
}))).into_response()
|
||||
}
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get user: {}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "failed to get settings"
|
||||
}))).into_response()
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "failed to get settings"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -261,29 +311,37 @@ pub async fn update_settings(
|
|||
Json(req): Json<UpdateSettingsRequest>,
|
||||
) -> impl IntoResponse {
|
||||
let user_id = ObjectId::parse_str(&claims.sub).unwrap();
|
||||
|
||||
|
||||
let mut user = match state.db.find_user_by_id(&user_id).await {
|
||||
Ok(Some(u)) => u,
|
||||
Ok(None) => {
|
||||
return (StatusCode::NOT_FOUND, Json(serde_json::json!({
|
||||
"error": "user not found"
|
||||
}))).into_response()
|
||||
return (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(serde_json::json!({
|
||||
"error": "user not found"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get user: {}", e);
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "database error"
|
||||
}))).into_response()
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "database error"
|
||||
})),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
if let Some(recovery_enabled) = req.recovery_enabled {
|
||||
if !recovery_enabled {
|
||||
user.remove_recovery_phrase();
|
||||
}
|
||||
// Note: Enabling recovery requires a separate endpoint to set the phrase
|
||||
}
|
||||
|
||||
|
||||
match state.db.update_user(&user).await {
|
||||
Ok(_) => {
|
||||
let response: UserSettingsResponse = user.into();
|
||||
|
|
@ -291,9 +349,13 @@ pub async fn update_settings(
|
|||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to update user: {}", e);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": "failed to update settings"
|
||||
}))).into_response()
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(serde_json::json!({
|
||||
"error": "failed to update settings"
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue