diff --git a/src/ids.rs b/src/ids.rs index 59fdc80..7dc195c 100644 --- a/src/ids.rs +++ b/src/ids.rs @@ -1,12 +1,21 @@ use rand::{distributions::Uniform, prelude::Distribution, thread_rng}; use regex::Regex; +use crate::db::{doll, schema::DollTagsDb}; + pub fn generate_ids() -> Vec { let uniform = Uniform::new_inclusive::(100_000, 999_999); let mut rng = thread_rng(); (1..=10).map(|_| uniform.sample(&mut rng)).collect() } +pub async fn pick_ids(db: DollTagsDb) -> Result, sqlx::Error> { + let mut ids_bundle = generate_ids(); + let occupied_ids = doll::check_ids(db, &ids_bundle).await?; + ids_bundle.retain(|&id| !occupied_ids.contains(&id)); + Ok(ids_bundle.iter().take(5).map(|v| *v).collect::>()) +} + pub fn id_public_to_db(id: &str) -> Option { let id_re = Regex::new(r"^\d{6}$").unwrap(); diff --git a/src/routes/form/register_tag.rs b/src/routes/form/register_tag.rs index 2dae59a..c2b4494 100644 --- a/src/routes/form/register_tag.rs +++ b/src/routes/form/register_tag.rs @@ -1,5 +1,5 @@ use rocket::{ - form::{self, Form}, + form::{self, Contextual, Form}, response::Redirect, }; use rocket_dyn_templates::{context, Template}; @@ -9,16 +9,13 @@ use crate::{ doll, schema::{CreateDollProfile, DollTagsDb}, }, - ids::{generate_ids, id_public_to_db}, - routes::error_handlers::PageResult, + ids::{id_public_to_db, pick_ids}, + routes::{error_handlers::PageResult, public}, }; #[get("/register")] pub async fn show_register(db: DollTagsDb) -> PageResult { - let mut ids_bundle = generate_ids(); - let occupied_ids = doll::check_ids(db, &ids_bundle).await?; - ids_bundle.retain(|&id| !occupied_ids.contains(&id)); - let ids = ids_bundle.iter().take(5).collect::>(); + let ids = pick_ids(db).await?; Ok(Template::render( "register", @@ -97,14 +94,23 @@ fn validate_chassis<'v>(a: &str, b: &str, c: &str, field: &str) -> form::Result< #[post("/register", data = "")] pub async fn handle_register(mut db: DollTagsDb, tag: Form>) -> PageResult { println!("register: {:?}", tag); + fn normalize_opt(opt: &str) -> Option<&str> { + if opt.len() != 0 { + Some(opt) + } else { + None + } + } + let id = id_public_to_db(&tag.ident).expect("id format was wrong but is now right??"); let pronouns = format!( "{}/{}/{}", tag.pronoun_subject, tag.pronoun_object, tag.pronoun_possessive ); - let microchip_id = tag.microchip_id.to_lowercase(); + let normalized_microchip_id = tag.microchip_id.to_lowercase(); + let microchip_id = normalize_opt(&normalized_microchip_id); - if doll::id_exists(&mut *db, id, µchip_id).await? { + if doll::id_exists(&mut *db, id, microchip_id.unwrap_or("")).await? { return Ok(Redirect::found(uri!("/register")).into()); } @@ -112,57 +118,39 @@ pub async fn handle_register(mut db: DollTagsDb, tag: Form>) -> Page db, CreateDollProfile { id, - microchip_id: if microchip_id.len() != 0 { - Some(µchip_id) - } else { - None - }, + microchip_id, name: tag.name, pronouns: pronouns.as_str(), handler_name: tag.handler_name, - handler_url: if tag.handler_link.len() != 0 { - Some(tag.handler_link) - } else { - None - }, - kind: if tag.kind.len() != 0 { - Some(tag.kind) - } else { - None - }, - breed: if tag.breed.len() != 0 { - Some(tag.breed) - } else { - None - }, - behaviour: if tag.behaviour.len() != 0 { - Some(tag.behaviour) - } else { - None - }, - description: if tag.description.len() != 0 { - Some(tag.description) - } else { - None - }, - chassis_type: if tag.chassis_type.len() != 0 { - Some(tag.chassis_type) - } else { - None - }, - chassis_id: if tag.chassis_id.len() != 0 { - Some(tag.chassis_id) - } else { - None - }, - chassis_color: if tag.chassis_color.len() != 0 { - Some(tag.chassis_color) - } else { - None - }, + handler_url: normalize_opt(tag.handler_link), + kind: normalize_opt(tag.kind), + breed: normalize_opt(tag.breed), + behaviour: normalize_opt(tag.behaviour), + description: normalize_opt(tag.description), + chassis_type: normalize_opt(tag.chassis_type), + chassis_id: normalize_opt(tag.chassis_id), + chassis_color: normalize_opt(tag.chassis_color), }, ) .await?; - Ok(Template::render("register", context! {ids: vec![123456]}).into()) + Ok(Redirect::to(uri!(public::show_profile(Some(tag.ident), microchip_id))).into()) +} + +#[post("/register", data = "", rank = 2)] +pub async fn handle_retry_register( + db: DollTagsDb, + tag: Form>>, +) -> PageResult { + // in case the form validation fails, this will be tasked with rendering the page again with submitted values and display errors + let ids = pick_ids(db).await?; + + Ok(Template::render( + "register", + context! { + ids, + previous: &tag.context, + }, + ) + .into()) }