From f6030577a02a042547e513d3f0e630f061a38205 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Thu, 26 Jan 2023 18:25:01 +0530 Subject: [PATCH] fix: upload difficulty via web form --- src/errors.rs | 4 +++ src/pages/panel/campaigns/bench.rs | 2 +- src/pages/panel/campaigns/new.rs | 43 ++++++++++++++++++++++--- templates/panel/campaigns/new/form.html | 14 +++++++- templates/panel/nav/index.html | 2 +- 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index a2cc5fc..620fce4 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -79,6 +79,9 @@ pub enum ServiceError { #[display(fmt = "Campaign doesn't exist")] CampaignDoesntExist, + + #[display(fmt = "Not a number: only numeral data is accepted")] + NotANumber, } #[derive(Serialize, Deserialize)] @@ -123,6 +126,7 @@ impl ResponseError for ServiceError { ServiceError::PasswordsDontMatch => StatusCode::BAD_REQUEST, ServiceError::CampaignDoesntExist => StatusCode::NOT_FOUND, + ServiceError::NotANumber => StatusCode::BAD_REQUEST, } } } diff --git a/src/pages/panel/campaigns/bench.rs b/src/pages/panel/campaigns/bench.rs index 37cb9c6..c9aa88a 100644 --- a/src/pages/panel/campaigns/bench.rs +++ b/src/pages/panel/campaigns/bench.rs @@ -30,7 +30,7 @@ pub struct Bench { ctx: RefCell, } -pub const BENCH: TemplateFile = TemplateFile::new("new_campaign", "bench/index.html"); +pub const BENCH: TemplateFile = TemplateFile::new("new_bench", "bench/index.html"); impl CtxError for Bench { fn with_error(&self, e: &ReadableError) -> String { diff --git a/src/pages/panel/campaigns/new.rs b/src/pages/panel/campaigns/new.rs index 40f1204..b225164 100644 --- a/src/pages/panel/campaigns/new.rs +++ b/src/pages/panel/campaigns/new.rs @@ -23,6 +23,7 @@ use actix_web::{web, HttpResponse, Responder}; use tera::Context; use crate::api::v1::admin::campaigns::{runners, AddCapmaign}; +use crate::errors::*; use crate::AppData; pub use super::*; @@ -60,23 +61,46 @@ impl NewCampaign { path = "PAGES.panel.campaigns.new", wrap = "crate::pages::get_page_check_login()" )] +#[tracing::instrument(name = "New campaign form", skip(data))] pub async fn new_campaign(data: AppData) -> PageResult { let new_campaign = NewCampaign::new(&data.settings).render(); let html = ContentType::html(); Ok(HttpResponse::Ok().content_type(html).body(new_campaign)) } +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct FormAddCampaign { + pub name: String, + pub difficulties: String, +} + +impl FormAddCampaign { + fn parse(self) -> ServiceResult { + let name = self.name; + let mut difficulties = Vec::new(); + for d in self.difficulties.split(',') { + let d = d.parse::().map_err(|_| ServiceError::NotANumber)?; + difficulties.push(d); + } + Ok(AddCapmaign { name, difficulties }) + } +} + #[actix_web_codegen_const_routes::post( path = "PAGES.panel.campaigns.new", wrap = "crate::pages::get_page_check_login()" )] +#[tracing::instrument(name = "New campaign form submit", skip(data, id))] pub async fn new_campaign_submit( id: Identity, - payload: web::Json, + payload: web::Form, data: AppData, ) -> PageResult { let username = id.identity().unwrap(); - let mut payload = payload.into_inner(); + let mut payload = payload + .into_inner() + .parse() + .map_err(|e| PageError::new(NewCampaign::new(&data.settings), e))?; runners::add_runner(&username, &mut payload, &data) .await @@ -117,14 +141,23 @@ mod tests { let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; let cookies = get_cookie!(signin_resp); - let new = AddCapmaign { + let mut difficulties = String::new(); + for d in DIFFICULTIES.iter() { + if difficulties.is_empty() { + difficulties = format!("{d}"); + } else { + difficulties = format!("{difficulties},{d}"); + } + } + println!("{difficulties}"); + let new = super::FormAddCampaign { name: CAMPAIGN_NAME.into(), - difficulties: DIFFICULTIES.into(), + difficulties, }; let new_resp = test::call_service( &app, - post_request!(&new, crate::PAGES.panel.campaigns.new) + post_request!(&new, crate::PAGES.panel.campaigns.new, FORM) .cookie(cookies) .to_request(), ) diff --git a/templates/panel/campaigns/new/form.html b/templates/panel/campaigns/new/form.html index 39f2204..327b0d1 100644 --- a/templates/panel/campaigns/new/form.html +++ b/templates/panel/campaigns/new/form.html @@ -1,6 +1,6 @@

Create new campaigns

+ +
diff --git a/templates/panel/nav/index.html b/templates/panel/nav/index.html index 421030b..1a178a1 100644 --- a/templates/panel/nav/index.html +++ b/templates/panel/nav/index.html @@ -4,7 +4,7 @@