From 36509d1f8801d191752996add41062dbd0e2fca8 Mon Sep 17 00:00:00 2001 From: "zhixiong.zhu" Date: Fri, 14 Aug 2020 00:27:49 +0800 Subject: [PATCH] feat: move insert_access_token fn to admin_access_token_provider.rs --- src/controller/backend/index.rs | 14 ++------------ src/provider/admin_access_token_provider.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/controller/backend/index.rs b/src/controller/backend/index.rs index 35e4583..8a8885b 100644 --- a/src/controller/backend/index.rs +++ b/src/controller/backend/index.rs @@ -46,17 +46,6 @@ fn passwd_hash(p: &str, s: &str) -> String { format!("{:x}", md5::compute((without_salt_hash + s).as_str())) } -fn insert_access_token(access_token: &str, admin_id: i32, connection: &MysqlDatabase) -> bool { - let record = InsertableAdminAccessToken { - admin_id: admin_id, - access_token: access_token, - }; - let result = diesel::insert_into(admin_access_token::table) - .values(&record) - .execute(&connection.0); - true -} - #[post("/admin/login", format = "application/json", data = "")] pub fn login(admin_login: Json, connection: MysqlDatabase) -> Result>, Error> { let admin_record = admin_provider::get_by_name(&admin_login.name, &connection); @@ -68,7 +57,8 @@ pub fn login(admin_login: Json, connection: MysqlDatabase) -> Result .sample_iter(&Alphanumeric) .take(32) .collect(); - insert_access_token(&rand_string, admin_record.id, &connection); + admin_access_token_provider::insert_access_token(&rand_string, admin_record.id, &connection); + let last_id = last_insert_id(&connection); let admin_token = AdminToken { token: rand_string, }; diff --git a/src/provider/admin_access_token_provider.rs b/src/provider/admin_access_token_provider.rs index f84aa3d..25844da 100644 --- a/src/provider/admin_access_token_provider.rs +++ b/src/provider/admin_access_token_provider.rs @@ -12,3 +12,14 @@ pub fn get(id: i32, connection: MysqlDatabase) -> QueryResult pub fn get_by_access_token(access_token: &str, connection: MysqlDatabase) -> QueryResult { admin_access_token::table.filter(admin_access_token::dsl::access_token.eq(access_token)).first(&(connection.0)) } + +pub fn insert_access_token(access_token: &str, admin_id: i32, connection: &MysqlDatabase) -> bool { + let record = InsertableAdminAccessToken { + admin_id: admin_id, + access_token: access_token, + }; + let result = diesel::insert_into(admin_access_token::table) + .values(&record) + .execute(&connection.0); + true +}