diff --git a/geneit_backend/src/controllers/user_controller.rs b/geneit_backend/src/controllers/user_controller.rs index 129ff57..9319f20 100644 --- a/geneit_backend/src/controllers/user_controller.rs +++ b/geneit_backend/src/controllers/user_controller.rs @@ -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) -> 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()) +} diff --git a/geneit_backend/src/main.rs b/geneit_backend/src/main.rs index dc83c0d..b262fe6 100644 --- a/geneit_backend/src/main.rs +++ b/geneit_backend/src/main.rs @@ -54,6 +54,10 @@ async fn main() -> std::io::Result<()> { .route("/auth/logout", web::get().to(auth_controller::logout)) // User controller .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())? .run() diff --git a/geneit_backend/src/services/users_service.rs b/geneit_backend/src/services/users_service.rs index c5c6e06..3c89572 100644 --- a/geneit_backend/src/services/users_service.rs +++ b/geneit_backend/src/services/users_service.rs @@ -138,6 +138,14 @@ pub async fn validate_account(user: &User) -> anyhow::Result<()> { 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( &user.email, "Activation de votre compte GeneIT", @@ -148,10 +156,20 @@ pub async fn validate_account(user: &User) -> anyhow::Result<()> { ) .await?; + Ok(()) +} + +/// Update account information +pub async fn update_account(user: User) -> anyhow::Result<()> { 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),)) + .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)?, ) })?;