feat: implement interface to add user's traffic pattern sqlx postgres

This commit is contained in:
realaravinth 2022-05-14 18:22:36 +05:30
parent 2132ab5791
commit dacdd2cb8e
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
2 changed files with 37 additions and 1 deletions

View file

@ -532,6 +532,35 @@ impl MCDatabase for Database {
Ok(resp.duration)
}
/// Add traffic configuration
async fn add_traffic_pattern(
&self,
username: &str,
captcha_key: &str,
pattern: &TrafficPattern,
) -> DBResult<()> {
sqlx::query!(
"INSERT INTO mcaptcha_sitekey_user_provided_avg_traffic (
config_id,
avg_traffic,
peak_sustainable_traffic,
broke_my_site_traffic
) VALUES (
(SELECT config_id FROM mcaptcha_config WHERE key = ($1)
AND user_id = (SELECT ID FROM mcaptcha_users WHERE name = $2)
), $3, $4, $5)",
//payload.avg_traffic,
captcha_key,
username,
pattern.avg_traffic as i32,
pattern.peak_sustainable_traffic as i32,
pattern.broke_my_site_traffic.as_ref().map(|v| *v as i32),
)
.execute(&self.pool)
.await
.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?;
Ok(())
}
}
fn now_unix_time_stamp() -> i64 {

View file

@ -34,6 +34,13 @@ async fn everyting_works() {
const CAPTCHA_DESCRIPTION: &str = "postgrescaptchadescription";
const CAPTCHA_DURATION: i32 = 30;
// easy traffic pattern
const TRAFFIC_PATTERN: TrafficPattern = TrafficPattern {
avg_traffic: 500,
peak_sustainable_traffic: 5_000,
broke_my_site_traffic: Some(10_000),
};
const LEVELS: [Level; 3] = [
Level {
difficulty_factor: 1,
@ -67,5 +74,5 @@ async fn everyting_works() {
key: CAPTCHA_SECRET,
description: CAPTCHA_DESCRIPTION,
};
database_works(&db, &p, &c, &LEVELS).await;
database_works(&db, &p, &c, &LEVELS, &TRAFFIC_PATTERN).await;
}