Can update profile information

This commit is contained in:
2023-06-05 18:52:00 +02:00
parent 0050d99797
commit 0ed24d078d
3 changed files with 46 additions and 1 deletions
geneit_backend/src

@ -2,10 +2,12 @@
//!
//! The actions of the user on his account when he is authenticated.
use crate::constants::StaticConstraints;
use crate::controllers::HttpResult;
use crate::models::User;
use crate::services::login_token_service::LoginToken;
use crate::services::users_service;
use actix_web::web::Json;
use actix_web::HttpResponse;
#[derive(serde::Serialize)]
@ -28,3 +30,24 @@ pub async fn auth_info(token: LoginToken) -> HttpResult {
.unwrap_or_default(),
}))
}
#[derive(serde::Deserialize)]
pub struct ProfileUpdate {
name: String,
}
/// Update profile information
pub async fn update_profile(token: LoginToken, profile: Json<ProfileUpdate>) -> HttpResult {
if !StaticConstraints::default()
.user_name_len
.validate(&profile.name)
{
return Ok(HttpResponse::BadRequest().json("Nom invalide!"));
}
let mut user = users_service::get_by_id(token.user_id).await?;
user.name = profile.0.name;
users_service::update_account(user).await?;
Ok(HttpResponse::Accepted().finish())
}