48 lines
1.2 KiB
Rust
48 lines
1.2 KiB
Rust
use rocket::State;
|
|
use rocket_contrib::Template;
|
|
use std::collections::HashMap;
|
|
use super::super::ListStore;
|
|
|
|
#[derive(Serialize)]
|
|
struct IndexTemplateContext<'a> {
|
|
app_version: &'a str,
|
|
lists: Vec<HashMap<&'a str, &'a str>>
|
|
}
|
|
|
|
#[get("/")]
|
|
fn index(lists: State<ListStore>) -> Template {
|
|
let lists = lists.read().unwrap();
|
|
let mut list_kvs = vec![];
|
|
for list in lists.iter() {
|
|
let mut list_kv = HashMap::new();
|
|
list_kv.insert("id", list.id.as_ref());
|
|
list_kv.insert("name", list.name.as_ref());
|
|
list_kvs.push(list_kv);
|
|
}
|
|
let context = IndexTemplateContext {
|
|
app_version: env!("CARGO_PKG_VERSION"),
|
|
lists: list_kvs
|
|
};
|
|
Template::render("index", &context)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use rocket;
|
|
use rocket::http::{Accept, Status};
|
|
use rocket::local::Client;
|
|
|
|
#[test]
|
|
fn index() {
|
|
let client = Client::new(rocket(Some("test"))).unwrap();
|
|
|
|
// Try to get the index and verify the body
|
|
let mut res = client.get("/").header(Accept::HTML).dispatch();
|
|
assert_eq!(res.status(), Status::Ok);
|
|
|
|
let body = res.body_string().unwrap();
|
|
println!("body: {}", body);
|
|
assert!(body.contains("<span id=\"list-name\">Test</span>"));
|
|
}
|
|
}
|