Can update profile information

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

View File

@ -2,10 +2,12 @@
//! //!
//! The actions of the user on his account when he is authenticated. //! The actions of the user on his account when he is authenticated.
use crate::constants::StaticConstraints;
use crate::controllers::HttpResult; use crate::controllers::HttpResult;
use crate::models::User; use crate::models::User;
use crate::services::login_token_service::LoginToken; use crate::services::login_token_service::LoginToken;
use crate::services::users_service; use crate::services::users_service;
use actix_web::web::Json;
use actix_web::HttpResponse; use actix_web::HttpResponse;
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
@ -28,3 +30,24 @@ pub async fn auth_info(token: LoginToken) -> HttpResult {
.unwrap_or_default(), .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())
}

View File

@ -54,6 +54,10 @@ async fn main() -> std::io::Result<()> {
.route("/auth/logout", web::get().to(auth_controller::logout)) .route("/auth/logout", web::get().to(auth_controller::logout))
// User controller // User controller
.route("/user/info", web::get().to(user_controller::auth_info)) .route("/user/info", web::get().to(user_controller::auth_info))
.route(
"/user/update_profile",
web::post().to(user_controller::update_profile),
)
}) })
.bind(AppConfig::get().listen_address.as_str())? .bind(AppConfig::get().listen_address.as_str())?
.run() .run()

View File

@ -138,6 +138,14 @@ pub async fn validate_account(user: &User) -> anyhow::Result<()> {
return Ok(()); return Ok(());
} }
db_connection::execute(|conn| {
Ok(
diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id)))
.set((users::dsl::time_activate.eq(time() as i64),))
.execute(conn)?,
)
})?;
mail_service::send_mail( mail_service::send_mail(
&user.email, &user.email,
"Activation de votre compte GeneIT", "Activation de votre compte GeneIT",
@ -148,10 +156,20 @@ pub async fn validate_account(user: &User) -> anyhow::Result<()> {
) )
.await?; .await?;
Ok(())
}
/// Update account information
pub async fn update_account(user: User) -> anyhow::Result<()> {
db_connection::execute(|conn| { db_connection::execute(|conn| {
Ok( Ok(
diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id))) diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id)))
.set((users::dsl::time_activate.eq(time() as i64),)) .set((
users::dsl::name.eq(user.name),
users::dsl::admin.eq(user.admin),
users::dsl::active.eq(user.active),
users::dsl::email.eq(user.email),
))
.execute(conn)?, .execute(conn)?,
) )
})?; })?;