feat: migrate update_key to use db_*

This commit is contained in:
realaravinth 2022-05-12 20:19:24 +05:30
parent b2d32c6113
commit 2dff139ae2
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88

View file

@ -22,6 +22,7 @@ use libmcaptcha::defense::Level;
use libmcaptcha::master::messages::RenameBuilder;
use serde::{Deserialize, Serialize};
use db_core::errors::DBError;
use db_core::CreateCaptcha;
use super::create::MCaptchaDetails;
@ -43,16 +44,21 @@ pub async fn update_key(
loop {
key = get_random(32);
let res = runner::update_key(&key, &payload.key, &username, &data).await;
if res.is_ok() {
break;
} else if let Err(sqlx::Error::Database(err)) = res {
if err.code() == Some(Cow::from("23505")) {
continue;
} else {
return Err(sqlx::Error::Database(err).into());
let mut key;
loop {
key = get_random(32);
match data
.dblib
.update_captcha_key(&username, &payload.key, &key)
.await
{
Ok(_) => break,
Err(DBError::SecretTaken) => continue,
Err(e) => return Err(e.into()),
}
};
}
}
let payload = payload.into_inner();
@ -99,24 +105,6 @@ pub mod runner {
use super::*;
pub async fn update_key(
key: &str,
old_key: &str,
username: &str,
data: &AppData,
) -> Result<(), sqlx::Error> {
sqlx::query!(
"UPDATE mcaptcha_config SET key = $1
WHERE key = $2 AND user_id = (SELECT ID FROM mcaptcha_users WHERE name = $3)",
&key,
&old_key,
&username,
)
.execute(&data.db)
.await?;
Ok(())
}
pub async fn update_captcha(
payload: &UpdateCaptcha,
data: &AppData,