Skip to content

Commit

Permalink
feat: move insert_access_token fn to admin_access_token_provider.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
zhixiong.zhu committed Aug 13, 2020
1 parent d03d0e3 commit 36509d1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/controller/backend/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<admin_login>")]
pub fn login(admin_login: Json<AdminLogin>, connection: MysqlDatabase) -> Result<Json<BaseResponse<AdminToken>>, Error> {
let admin_record = admin_provider::get_by_name(&admin_login.name, &connection);
Expand All @@ -68,7 +57,8 @@ pub fn login(admin_login: Json<AdminLogin>, 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,
};
Expand Down
11 changes: 11 additions & 0 deletions src/provider/admin_access_token_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ pub fn get(id: i32, connection: MysqlDatabase) -> QueryResult<AdminAccessToken>
pub fn get_by_access_token(access_token: &str, connection: MysqlDatabase) -> QueryResult<AdminAccessToken> {
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
}

0 comments on commit 36509d1

Please sign in to comment.