feat: migrate getting traffic pattern to use db_* interface

This commit is contained in:
realaravinth 2022-05-14 18:45:59 +05:30
parent b983f08884
commit 2dd18897b0
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
3 changed files with 14 additions and 74 deletions

View file

@ -241,39 +241,6 @@
},
"query": "SELECT config_id, duration, name from mcaptcha_config WHERE\n key = $1 AND\n user_id = (SELECT ID FROM mcaptcha_users WHERE name = $2) "
},
"c2e167e56242de7e0a835e25004b15ca8340545fa0ca7ac8f3293157d2d03d98": {
"describe": {
"columns": [
{
"name": "avg_traffic",
"ordinal": 0,
"type_info": "Int4"
},
{
"name": "peak_sustainable_traffic",
"ordinal": 1,
"type_info": "Int4"
},
{
"name": "broke_my_site_traffic",
"ordinal": 2,
"type_info": "Int4"
}
],
"nullable": [
false,
false,
true
],
"parameters": {
"Left": [
"Text",
"Text"
]
}
},
"query": "SELECT \n avg_traffic, \n peak_sustainable_traffic, \n broke_my_site_traffic \n FROM \n mcaptcha_sitekey_user_provided_avg_traffic \n WHERE \n config_id = (\n SELECT \n config_id \n FROM \n mcaptcha_config \n WHERE \n KEY = $1 \n AND user_id = (\n SELECT \n id \n FROM \n mcaptcha_users \n WHERE \n NAME = $2\n )\n )\n "
},
"c399efd5db1284dcb470c40f9b076851f77498c75a63a3b151d4a111bd3e2957": {
"describe": {
"columns": [

View file

@ -119,6 +119,10 @@ pub enum ServiceError {
/// captcha not found
#[display(fmt = "Captcha not found.")]
CaptchaNotFound,
/// Traffic pattern not found
#[display(fmt = "Traffic pattern not found")]
TrafficPatternNotFound,
}
#[derive(Serialize, Deserialize)]
@ -179,6 +183,7 @@ impl ResponseError for ServiceError {
ServiceError::DBError(_) => StatusCode::INTERNAL_SERVER_ERROR,
ServiceError::CaptchaNotFound => StatusCode::NOT_FOUND,
ServiceError::TrafficPatternNotFound => StatusCode::NOT_FOUND,
}
}
}
@ -208,6 +213,7 @@ impl From<DBError> for ServiceError {
DBError::EmailTaken => ServiceError::EmailTaken,
DBError::AccountNotFound => ServiceError::AccountNotFound,
DBError::CaptchaNotFound => ServiceError::CaptchaNotFound,
DBError::TrafficPatternNotFound => ServiceError::TrafficPatternNotFound,
_ => ServiceError::DBError(DBErrorWrapper(e)),
}
}

View file

@ -17,7 +17,8 @@
use actix_identity::Identity;
use actix_web::{http, web, HttpResponse, Responder};
use sailfish::TemplateOnce;
use sqlx::Error::RowNotFound;
use db_core::errors::DBError;
use crate::api::v1::mcaptcha::easy::TrafficPatternRequest;
use crate::errors::*;
@ -133,44 +134,7 @@ pub async fn easy(
let username = id.identity().unwrap();
let key = path.into_inner();
struct Traffic {
peak_sustainable_traffic: i32,
avg_traffic: i32,
broke_my_site_traffic: Option<i32>,
}
match sqlx::query_as!(
Traffic,
"SELECT
avg_traffic,
peak_sustainable_traffic,
broke_my_site_traffic
FROM
mcaptcha_sitekey_user_provided_avg_traffic
WHERE
config_id = (
SELECT
config_id
FROM
mcaptcha_config
WHERE
KEY = $1
AND user_id = (
SELECT
id
FROM
mcaptcha_users
WHERE
NAME = $2
)
)
",
&key,
&username
)
.fetch_one(&data.db)
.await
{
match data.dblib.get_traffic_pattern(&username, &key).await {
Ok(c) => {
struct Description {
name: String,
@ -199,7 +163,7 @@ pub async fn easy(
.content_type("text/html; charset=utf-8")
.body(page));
}
Err(RowNotFound) => {
Err(DBError::TrafficPatternNotFound) => {
return Ok(HttpResponse::Found()
.insert_header((
http::header::LOCATION,
@ -207,7 +171,10 @@ pub async fn easy(
))
.finish());
}
Err(e) => Err(e.into()),
Err(e) => {
let e: ServiceError = e.into();
Err(e.into())
}
}
}