From 8f9021d8efb8c8132171512ff53512fbc0f9af58 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 11 May 2021 17:41:03 +0200 Subject: [PATCH] Can get admin information --- src/api_data/admin/admin_info_api.rs | 26 +++++++++++++++++++ src/api_data/admin/mod.rs | 3 ++- .../admin/admin_account_controller.rs | 8 ++++++ src/helpers/admin_account_helper.rs | 6 +++++ src/helpers/database.rs | 5 ++++ src/routes.rs | 1 + 6 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/api_data/admin/admin_info_api.rs diff --git a/src/api_data/admin/admin_info_api.rs b/src/api_data/admin/admin_info_api.rs new file mode 100644 index 0000000..aaaf8f1 --- /dev/null +++ b/src/api_data/admin/admin_info_api.rs @@ -0,0 +1,26 @@ +//! # Admin information API +//! +//! @author Pierre Hubert + +use serde::Serialize; + +use crate::data::admin::Admin; + +#[derive(Serialize)] +pub struct AdminInfoAPI { + id: u64, + name: String, + email: String, + time_create: u64, +} + +impl AdminInfoAPI { + pub fn new(a: &Admin) -> Self { + Self { + id: a.id.id(), + name: a.name.clone(), + email: a.email.clone(), + time_create: a.time_create, + } + } +} \ No newline at end of file diff --git a/src/api_data/admin/mod.rs b/src/api_data/admin/mod.rs index ebfebd7..1c29937 100644 --- a/src/api_data/admin/mod.rs +++ b/src/api_data/admin/mod.rs @@ -4,4 +4,5 @@ pub mod admin_auth_options; pub mod admin_auth_success; -pub mod admin_id_api; \ No newline at end of file +pub mod admin_id_api; +pub mod admin_info_api; \ No newline at end of file diff --git a/src/controllers/admin/admin_account_controller.rs b/src/controllers/admin/admin_account_controller.rs index cc9459a..5e33cfa 100644 --- a/src/controllers/admin/admin_account_controller.rs +++ b/src/controllers/admin/admin_account_controller.rs @@ -5,6 +5,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::base_request_handler::BaseRequestHandler; use crate::data::http_request_handler::HttpRequestHandler; use crate::helpers::{admin_access_token_helper, admin_account_helper}; @@ -41,4 +42,11 @@ pub fn auth_with_reset_token(r: &mut HttpRequestHandler) -> RequestResult { /// Get current admin ID pub fn get_admin_id(r: &mut HttpRequestHandler) -> RequestResult { r.set_response(AdminIDAPI::new(r.admin_id()?)) +} + +/// Get current admin information +pub fn get_admin_info(r: &mut HttpRequestHandler) -> RequestResult { + let admin = admin_account_helper::find_admin_by_id(r.admin_id()?)?; + + r.set_response(AdminInfoAPI::new(&admin)) } \ No newline at end of file diff --git a/src/helpers/admin_account_helper.rs b/src/helpers/admin_account_helper.rs index 8a03f95..d902097 100644 --- a/src/helpers/admin_account_helper.rs +++ b/src/helpers/admin_account_helper.rs @@ -24,6 +24,12 @@ pub fn create(new_admin: &NewAdmin) -> Res { .map(|i| AdminID::new(i)) } +/// Get admin information by ID +pub fn find_admin_by_id(id: AdminID) -> Res { + database::QueryInfo::new(ADMIN_LIST_TABLE) + .cond_admin_id("id", id) + .query_row(db_to_admin) +} /// Get admin information by admin email address pub fn find_admin_by_email(email: &str) -> Res { diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 00ca049..006a58c 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -171,6 +171,11 @@ impl QueryInfo { self } + pub fn cond_admin_id(mut self, key: &str, val: AdminID) -> QueryInfo { + self.conditions.insert(key.to_string(), mysql::Value::from(val.id())); + self + } + pub fn cond_group_id(mut self, key: &str, val: &GroupID) -> QueryInfo { self.conditions.insert(key.to_string(), mysql::Value::from(val.id())); self diff --git a/src/routes.rs b/src/routes.rs index 7235271..4a7411d 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -350,5 +350,6 @@ pub fn get_routes() -> Vec { Route::limited_admin_post_without_login("/admin/accounts/auth_options", Box::new(admin_account_controller::get_auth_options), LimitPolicy::FAILURE(5)), Route::limited_admin_post_without_login("/admin/accounts/auth_with_reset_token", Box::new(admin_account_controller::auth_with_reset_token), LimitPolicy::FAILURE(5)), 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)), ] } \ No newline at end of file