diff --git a/src/controllers/admin/admin_account_controller.rs b/src/controllers/admin/admin_account_controller.rs index d72a51d..4ddc678 100644 --- a/src/controllers/admin/admin_account_controller.rs +++ b/src/controllers/admin/admin_account_controller.rs @@ -6,6 +6,7 @@ use crate::api_data::admin::admin_auth_options::AdminAuthOptions; use crate::api_data::admin::admin_auth_success::AdminAuthSuccess; use crate::api_data::admin::admin_id_api::AdminIDAPI; use crate::api_data::admin::admin_info_api::AdminInfoAPI; +use crate::data::admin::NewAdminGeneralSettings; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::http_request_handler::HttpRequestHandler; use crate::helpers::{admin_access_token_helper, admin_account_helper}; @@ -61,10 +62,31 @@ pub fn get_admin_info(r: &mut HttpRequestHandler) -> RequestResult { if admin_id == r.admin_id()? { admin_account_helper::find_admin_by_id(admin_id)? } else { + // TODO unimplemented!(); } } }; r.set_response(AdminInfoAPI::new(&admin)) +} + +/// Update general settings +pub fn update_general_settings(r: &mut HttpRequestHandler) -> RequestResult { + let admin_id = r.post_admin_id("id")?; + let new_name = r.post_string("name")?; + let new_email = r.post_email("email")?; + + if admin_id != r.admin_id()? { + // TODO + unimplemented!(); + } + + admin_account_helper::set_general_settings(NewAdminGeneralSettings { + id: admin_id, + name: new_name, + email: new_email, + })?; + + r.ok() } \ No newline at end of file diff --git a/src/data/admin.rs b/src/data/admin.rs index cd2e8b7..a3e90da 100644 --- a/src/data/admin.rs +++ b/src/data/admin.rs @@ -48,4 +48,11 @@ pub struct AdminAccessToken { pub token: String, pub id: AdminID, pub last_refresh: u64 +} + +/// New admin general settings +pub struct NewAdminGeneralSettings { + pub id: AdminID, + pub name: String, + pub email: String } \ No newline at end of file diff --git a/src/helpers/admin_account_helper.rs b/src/helpers/admin_account_helper.rs index 82eada8..9cc870b 100644 --- a/src/helpers/admin_account_helper.rs +++ b/src/helpers/admin_account_helper.rs @@ -4,7 +4,7 @@ use crate::constants::{ADMIN_RESET_TOKEN_LENGTH, ADMIN_RESET_TOKEN_LIFETIME}; use crate::constants::database_tables_names::ADMIN_LIST_TABLE; -use crate::data::admin::{Admin, AdminID, AdminResetToken, NewAdmin}; +use crate::data::admin::{Admin, AdminID, AdminResetToken, NewAdmin, NewAdminGeneralSettings}; use crate::data::error::{ExecError, Res}; use crate::helpers::database; use crate::utils::crypt_utils::rand_str; @@ -62,6 +62,15 @@ pub fn create_new_reset_token(id: AdminID) -> Res { Ok(token) } +/// Update admin general settings +pub fn set_general_settings(settings: NewAdminGeneralSettings) -> Res { + database::UpdateInfo::new(ADMIN_LIST_TABLE) + .cond_admin_id("id", settings.id) + .set_str("name", &settings.name) + .set_str("email", &settings.email) + .exec() +} + /// Turn a database entry into an admin structure fn db_to_admin(row: &database::RowResult) -> Res { let reset_token_expire = row.get_optional_u64("reset_token_expire")? diff --git a/src/routes.rs b/src/routes.rs index 9b5ccb4..927b35d 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -352,5 +352,6 @@ pub fn get_routes() -> Vec { Route::admin_post("/admin/accounts/sign_out", Box::new(admin_account_controller::sign_out)), Route::admin_post("/admin/accounts/id", Box::new(admin_account_controller::get_admin_id)), Route::admin_post("/admin/accounts/info", Box::new(admin_account_controller::get_admin_info)), + Route::admin_post("/admin/accounts/update_general_settings", Box::new(admin_account_controller::update_general_settings)), ] } \ No newline at end of file