fetch bench configuration

This commit is contained in:
Aravinth Manivannan 2021-10-11 18:28:45 +05:30
parent af4f1b208e
commit 2c4d2e5358
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
4 changed files with 52 additions and 13 deletions

View File

@ -203,6 +203,26 @@
]
}
},
"9cdade613ce724631cc3f187510758ee0929e93ff3f8ce81fe35594756644246": {
"query": "SELECT difficulties FROM survey_campaigns WHERE id = $1;",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "difficulties",
"type_info": "Int4Array"
}
],
"parameters": {
"Left": [
"Uuid"
]
},
"nullable": [
false
]
}
},
"a721cfa249acf328c2f29c4cf8c2aeba1a635bcf49d18ced5474caa10b7cae4f": {
"query": "INSERT INTO survey_benches \n (resp_id, difficulty, duration) \n VALUES ($1, $2, $3);",
"describe": {

View File

@ -27,6 +27,7 @@ pub use super::{get_random, get_uuid};
pub fn services(cfg: &mut ServiceConfig) {
auth::services(cfg);
account::services(cfg);
campaigns::services(cfg);
}
pub fn get_admin_check_login() -> crate::CheckLogin {

View File

@ -32,29 +32,36 @@ pub mod routes {
pub struct Benches {
pub submit: &'static str,
pub register: &'static str,
pub fetch: &'static str,
pub scope: &'static str,
}
impl Benches {
pub const fn new() -> Benches {
let submit = "/api/v1/benches/{campaign_id}/submit";
let fetch = "/api/v1/benches/{campaign_id}/fetch";
let register = "/api/v1/benches/register";
let scope = "/api/v1/benches/";
Benches {
submit,
register,
fetch,
scope,
}
}
pub fn submit_route(&self, campaign_id: &str) -> String {
self.submit.replace("{campaign_id}", &campaign_id)
}
pub fn fetch_routes(&self, campaign_id: &str) -> String {
self.fetch.replace("{campaign_id}", &campaign_id)
}
}
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(submit);
cfg.service(register);
cfg.service(fetch);
}
pub mod runners {
@ -228,3 +235,26 @@ async fn submit(
Ok(HttpResponse::Ok().json(resp))
}
#[derive(Serialize, Deserialize)]
pub struct BenchConfig {
pub difficulties: Vec<i32>,
}
#[my_codegen::post(
path = "crate::V1_API_ROUTES.benches.fetch",
wrap = "get_check_login()"
)]
async fn fetch(data: AppData, path: web::Path<String>) -> ServiceResult<impl Responder> {
let path = path.into_inner();
let campaign_id = Uuid::parse_str(&path).map_err(|_| ServiceError::NotAnId)?;
let config = sqlx::query_as!(
BenchConfig,
"SELECT difficulties FROM survey_campaigns WHERE id = $1;",
&campaign_id,
)
.fetch_one(&data.db)
.await?;
Ok(HttpResponse::Ok().json(config))
}

View File

@ -28,8 +28,6 @@ use derive_more::{Display, Error};
use libmcaptcha::errors::CaptchaError;
use serde::{Deserialize, Serialize};
use tokio::sync::oneshot::error::RecvError;
use url::ParseError;
use validator::ValidationErrors;
#[derive(Debug, Display, PartialEq, Error)]
#[cfg(not(tarpaulin_include))]
@ -57,11 +55,6 @@ pub enum ServiceError {
//405j
NotAnId,
/// when the a token name is already taken
/// token not found
#[display(fmt = "Token not found. Is token registered?")]
TokenNotFound,
#[display(fmt = "Wrong password")]
WrongPassword,
#[display(fmt = "Account not found")]
@ -109,7 +102,6 @@ impl ResponseError for ServiceError {
})
.unwrap(),
)
.into()
}
#[cfg(not(tarpaulin_include))]
@ -133,7 +125,6 @@ impl ResponseError for ServiceError {
ServiceError::PasswordTooLong => StatusCode::BAD_REQUEST,
ServiceError::PasswordsDontMatch => StatusCode::BAD_REQUEST,
ServiceError::TokenNotFound => StatusCode::NOT_FOUND,
ServiceError::CaptchaError(e) => {
log::error!("{}", e);
match e {
@ -155,10 +146,7 @@ impl From<CaptchaError> for ServiceError {
#[cfg(not(tarpaulin_include))]
impl From<sqlx::Error> for ServiceError {
#[cfg(not(tarpaulin_include))]
fn from(e: sqlx::Error) -> Self {
// use sqlx::error::Error;
// use std::borrow::Cow;
fn from(_: sqlx::Error) -> Self {
ServiceError::InternalServerError
}
}