summaryrefslogtreecommitdiff
path: root/src/handlers/mod.rs
blob: efd99f5d669c198016e6962e925112dbc082def8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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>>,
}