starting to properly handle forms
This commit is contained in:
parent
657b47d20b
commit
2bf185b1af
2 changed files with 52 additions and 55 deletions
|
@ -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<i64> {
|
||||
let uniform = Uniform::new_inclusive::<i64, i64>(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<Vec<i64>, 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::<Vec<i64>>())
|
||||
}
|
||||
|
||||
pub fn id_public_to_db(id: &str) -> Option<i64> {
|
||||
let id_re = Regex::new(r"^\d{6}$").unwrap();
|
||||
|
||||
|
|
|
@ -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::<Vec<&i64>>();
|
||||
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 = "<tag>")]
|
||||
pub async fn handle_register(mut db: DollTagsDb, tag: Form<TagForm<'_>>) -> 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<TagForm<'_>>) -> 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 = "<tag>", rank = 2)]
|
||||
pub async fn handle_retry_register(
|
||||
db: DollTagsDb,
|
||||
tag: Form<Contextual<'_, TagForm<'_>>>,
|
||||
) -> 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())
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue