summaryrefslogtreecommitdiff
path: root/src/handlers
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2021-03-05 16:48:48 -0500
committerAdam T. Carpenter <atc@53hor.net>2021-03-05 16:48:48 -0500
commit18290b45af404af505936cd850762057a5a4eea0 (patch)
tree429ade36310e9e7367ef7377138353673a34b3a5 /src/handlers
parent0c63ae51d1221b8ecd9742c7d5580bd06551ecbb (diff)
downloadtwinh-18290b45af404af505936cd850762057a5a4eea0.tar.xz
twinh-18290b45af404af505936cd850762057a5a4eea0.zip
broke out handlers, added basic models
Diffstat (limited to 'src/handlers')
-rw-r--r--src/handlers/mod.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs
new file mode 100644
index 0000000..efd99f5
--- /dev/null
+++ b/src/handlers/mod.rs
@@ -0,0 +1,70 @@
+use crate::templates;
+use handlebars::Handlebars;
+use hyper::Method;
+use hyper::StatusCode;
+use hyper::{Body, Request, Response};
+use serde::{Deserialize, Serialize};
+use std::convert::Infallible; // TODO:
+
+pub async fn router(
+ req: Request<Body>,
+ _client: mongodb::Client,
+) -> Result<Response<Body>, Infallible> {
+ match (req.method(), req.uri().path()) {
+ (&Method::GET, "/") | (&Method::GET, "/index.html") => Ok(Response::new("Welcome!".into())),
+ (&Method::GET, "/parts") | (&Method::GET, "/parts/index.html") => get_parts(req).await,
+ _ => Ok(Response::builder()
+ .status(StatusCode::NOT_FOUND)
+ .body("Not found.".into())
+ .unwrap()),
+ }
+}
+
+async fn get_parts(req: Request<Body>) -> Result<Response<Body>, Infallible> {
+ let query = req.uri().query().unwrap_or_default();
+ let filter = serde_urlencoded::de::from_str::<PartsQuery>(query).unwrap();
+ let mut reg = Handlebars::new();
+ reg.register_template_string("index", templates::INDEX_T)
+ .unwrap();
+ let mut data = PartsData::default();
+ data.makes = Some(vec!["Hudson".into(), "Essex".into(), "Terraplane".into()]);
+
+ if let Some(make) = filter.make {
+ if make.eq("Hudson") {
+ data.models = Some(vec!["Hornet".into(), "Wasp".into(), "Jet".into()]);
+ } else if make.eq("Essex") {
+ data.models = Some(vec!["Super Six".into()]);
+ }
+ data.selected_make = Some(make);
+ }
+
+ data.parts = Some(Vec::new());
+ if let Some(model) = filter.model {
+ data.parts = Some(vec!["1".into(), "2".into(), "3".into()]);
+ data.selected_model = Some(model);
+ }
+
+ let result = reg.render("index", &data).unwrap();
+ Ok(Response::new(result.into()))
+}
+
+#[derive(Debug, Deserialize)]
+struct PartsQuery {
+ make: Option<String>,
+ year: Option<String>,
+ model: Option<String>,
+ engine: Option<String>,
+}
+
+#[derive(Debug, Serialize, Default)]
+struct PartsData {
+ makes: Option<Vec<String>>,
+ years: Option<Vec<String>>,
+ models: Option<Vec<String>>,
+ engines: Option<Vec<String>>,
+ selected_make: Option<String>,
+ selected_year: Option<String>,
+ selected_model: Option<String>,
+ selected_engine: Option<String>,
+ parts: Option<Vec<String>>,
+}