feat: implement create captcha for sqlx postgres

This commit is contained in:
realaravinth 2022-05-12 11:52:53 +05:30
parent 00dca4a069
commit d64b05c84f
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
3 changed files with 30 additions and 1 deletions

View file

@ -33,6 +33,8 @@ pub fn map_register_err(e: Error) -> DBError {
DBError::EmailTaken
} else if msg.contains("mcaptcha_users_secret_key") {
DBError::SecretTaken
} else if msg.contains("mcaptcha_config_key_key") {
DBError::CaptchaKeyTaken
} else {
DBError::DBError(Box::new(Error::Database(err)))
}

View file

@ -278,6 +278,23 @@ impl MCDatabase for Database {
.map_err(map_register_err)?;
Ok(())
}
/// create new captcha
async fn create_captcha(&self, username: &str, p: &CreateCaptcha) -> DBResult<()> {
sqlx::query!(
"INSERT INTO mcaptcha_config
(key, user_id, duration, name)
VALUES ($1, (SELECT ID FROM mcaptcha_users WHERE name = $2), $3, $4)",
p.key,
username,
p.duration as i32,
p.description,
)
.execute(&self.pool)
.await
.map_err(map_register_err)?;
Ok(())
}
}
fn now_unix_time_stamp() -> i64 {

View file

@ -29,6 +29,10 @@ async fn everyting_works() {
const NAME: &str = "postgresuser";
const PASSWORD: &str = "pasdfasdfasdfadf";
const SECRET1: &str = "postgressecret1";
// captcha config
const CAPTCHA_SECRET: &str = "postgrescaptchasecret";
const CAPTCHA_DESCRIPTION: &str = "postgrescaptchadescription";
const CAPTCHA_DURATION: i32 = 30;
let url = env::var("POSTGRES_DATABASE_URL").unwrap();
let pool_options = PgPoolOptions::new().max_connections(2);
@ -42,5 +46,11 @@ async fn everyting_works() {
hash: PASSWORD,
secret: SECRET1,
};
database_works(&db, &p).await;
let c = CreateCaptcha {
duration: CAPTCHA_DURATION,
key: CAPTCHA_SECRET,
description: CAPTCHA_DESCRIPTION,
};
database_works(&db, &p, &c).await;
}