Initial commit
This commit is contained in:
99
src/form.rs
Normal file
99
src/form.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
/* hrtime - transgender survey website
|
||||
* Copyright (C) 2025 Olive <hello@grasswren.net>
|
||||
* see LICENCE file for licensing information */
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use diesel::insert_into;
|
||||
use diesel::RunQueryDsl;
|
||||
|
||||
use rocket::form::{Form, FromForm};
|
||||
use rocket::fs::NamedFile;
|
||||
|
||||
use crate::Database;
|
||||
use crate::models;
|
||||
use crate::schema::*;
|
||||
|
||||
#[derive(FromForm, Debug)]
|
||||
pub struct Primary {
|
||||
hrt: String,
|
||||
sex: bool,
|
||||
|
||||
entry: models::Entry,
|
||||
gender: models::Gender,
|
||||
ethnicity: models::Ethnicity,
|
||||
meds: Vec<models::Medication>,
|
||||
masc: models::Masculine,
|
||||
mascsex: models::MasculineSex,
|
||||
fem: models::Feminine,
|
||||
femsex: models::FeminineSex,
|
||||
}
|
||||
|
||||
#[rocket::post("/", data="<form>")]
|
||||
pub async fn form(
|
||||
mut form: Form<Primary>,
|
||||
conn: Database,
|
||||
) -> Option<NamedFile> {
|
||||
let entry = form.entry.clone();
|
||||
let id = conn.run(|conn| insert_into(entry::table)
|
||||
.values(entry)
|
||||
.returning(entry::dsl::id)
|
||||
.get_result::<Option<i32>>(conn)
|
||||
.unwrap()).await.unwrap();
|
||||
|
||||
form.gender.entry = id;
|
||||
form.ethnicity.entry = id;
|
||||
for med in form.meds.iter_mut() {
|
||||
med.entry = id;
|
||||
}
|
||||
|
||||
let gender = form.gender.clone();
|
||||
conn.run(|conn| insert_into(gender::table)
|
||||
.values(gender)
|
||||
.execute(conn)
|
||||
.unwrap()).await;
|
||||
let ethnicity = form.ethnicity.clone();
|
||||
conn.run(|conn| insert_into(ethnicity::table)
|
||||
.values(ethnicity)
|
||||
.execute(conn)
|
||||
.unwrap()).await;
|
||||
let meds = form.meds.clone();
|
||||
conn.run(|conn| insert_into(medication::table)
|
||||
.values(meds)
|
||||
.execute(conn)
|
||||
.unwrap()).await;
|
||||
|
||||
if form.hrt == "Masculinizing" {
|
||||
form.masc.entry = id;
|
||||
let masc = form.masc.clone();
|
||||
conn.run(|conn| insert_into(masculine::table)
|
||||
.values(masc)
|
||||
.execute(conn)
|
||||
.unwrap()).await;
|
||||
if form.sex {
|
||||
form.mascsex.entry = id;
|
||||
let mascsex = form.mascsex.clone();
|
||||
conn.run(|conn| insert_into(masculine_sex::table)
|
||||
.values(mascsex)
|
||||
.execute(conn)
|
||||
.unwrap()).await;
|
||||
}
|
||||
} else {
|
||||
form.fem.entry = id;
|
||||
let fem = form.fem.clone();
|
||||
conn.run(|conn| insert_into(feminine::table)
|
||||
.values(fem)
|
||||
.execute(conn)
|
||||
.unwrap()).await;
|
||||
if form.sex {
|
||||
form.femsex.entry = id;
|
||||
let femsex = form.femsex.clone();
|
||||
conn.run(|conn| insert_into(feminine_sex::table)
|
||||
.values(femsex)
|
||||
.execute(conn)
|
||||
.unwrap()).await;
|
||||
}
|
||||
}
|
||||
|
||||
return NamedFile::open(Path::new("/var/www/thanks.html")).await.ok();
|
||||
}
|
||||
Reference in New Issue
Block a user