From f21e06fa26880166ec4df467ee4723503dfbad55 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 11 Nov 2018 01:38:00 +0100 Subject: main(): Insert purchaser into datatabase (WIP) This currently errors on a borrow problem with the `cx` in the closure. Here we get the purchaser name and email from the POST params and insert them as a record in the database. If all goes well, we respond with a 200. Otherwise we log errors and respond with 500. --- license-generator/src/main.rs | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/license-generator/src/main.rs b/license-generator/src/main.rs index 3fe03ed..d18b880 100644 --- a/license-generator/src/main.rs +++ b/license-generator/src/main.rs @@ -64,12 +64,6 @@ fn main() -> Result<()> { }, }; - let test_purchaser = Purchaser::new("Shiki", "shiki@example.com"); - match test_purchaser.insert(&mut cx) { - Ok(_) => (), - Err(e) => error!("{}", e), - } - fastcgi::run(|mut req| { let mut params = String::new(); match req.stdin().read_to_string(&mut params) { @@ -120,9 +114,42 @@ fn main() -> Result<()> { }; if is_verified { + let name = ps.get("name"); + let email = ps.get("email"); + + if name.is_some() && email.is_some() { + let purchaser = Purchaser::new(name.unwrap(), email.unwrap()); + match purchaser.insert(&mut cx) { + Ok(_) => { + // TODO: Print message to be appended to user email + + write!(&mut req.stdout(), "Content-Type: text/plain + +200 OK") + .unwrap_or(()); + + return; + }, + Err(e) => { + error!("{}", e); + + response::set_500(&mut req.stdout()).unwrap_or(()); + write!(&mut req.stdout(), "Content-Type: text/plain + +500 Internal Server Error") + .unwrap_or(()); + + return; + }, + } + } + + error!("Purchaser name or email not set"); + + response::set_500(&mut req.stdout()).unwrap_or(()); write!(&mut req.stdout(), "Content-Type: text/plain - 200 OK") +500 Internal Server Error") .unwrap_or(()); return; -- cgit v1.2.3