Simplify handler contexts, template and style tweaks
This commit is contained in:
parent
75f50d6ed1
commit
940aadb6e5
14 changed files with 176 additions and 110 deletions
|
|
@ -24,9 +24,9 @@ pub(in crate::router::handlers) fn by_code(
|
|||
fn make_body(code: Option<u16>, message: Option<&str>) -> String {
|
||||
let mut context = tera::Context::default();
|
||||
|
||||
let graph = Graph::load();
|
||||
let out_code = code.unwrap_or(500);
|
||||
let out_message = &message.unwrap_or("Unknown error");
|
||||
let config = Graph::load().meta.config;
|
||||
|
||||
context.insert(
|
||||
"title",
|
||||
|
|
@ -35,9 +35,9 @@ fn make_body(code: Option<u16>, message: Option<&str>) -> String {
|
|||
.to_string(),
|
||||
);
|
||||
|
||||
context.insert("graph", &graph);
|
||||
context.insert("message", out_message);
|
||||
context.insert("status_code", &out_code.to_string());
|
||||
context.insert("config", &config);
|
||||
|
||||
handlers::template::render(
|
||||
"error.html",
|
||||
|
|
|
|||
|
|
@ -1,15 +1,12 @@
|
|||
use axum::response::IntoResponse as _;
|
||||
use axum::{body::Body, extract::Path, http::Response, response::Redirect};
|
||||
|
||||
use crate::graph::Edge;
|
||||
use crate::{graph::Graph, router::handlers, graph::Node};
|
||||
|
||||
pub async fn node(Path(id): Path<String>) -> Response<Body> {
|
||||
let graph = Graph::load();
|
||||
let result = graph.find_node(&id);
|
||||
let found = result.node.is_some();
|
||||
let nodes: Vec<Node> = graph.nodes.clone().into_values().collect();
|
||||
let not_found = result.node.is_none();
|
||||
let node = result
|
||||
.node
|
||||
.unwrap_or(Node::new(Some(format!("Could not find node ID {id}."))));
|
||||
|
|
@ -27,24 +24,14 @@ pub async fn node(Path(id): Path<String>) -> Response<Body> {
|
|||
}
|
||||
|
||||
let mut context = tera::Context::default();
|
||||
context.insert("graph", &graph);
|
||||
context.insert("node", &node);
|
||||
context.insert("nodes", &nodes);
|
||||
context.insert(
|
||||
"connections",
|
||||
&node
|
||||
.connections
|
||||
.clone()
|
||||
.unwrap_or_default()
|
||||
.values()
|
||||
.collect::<Vec<&Edge>>(),
|
||||
);
|
||||
context.insert("incoming", &graph.incoming.get(&id));
|
||||
context.insert("config", &graph.meta.config);
|
||||
|
||||
handlers::template::by_filename(
|
||||
"node.html",
|
||||
&context,
|
||||
if not_found { 404 } else { 500 },
|
||||
if found { 500 } else { 404 },
|
||||
Some(
|
||||
format!(
|
||||
"Failed to generate page for node {} (ID {}).\n\
|
||||
|
|
@ -53,7 +40,7 @@ pub async fn node(Path(id): Path<String>) -> Response<Body> {
|
|||
)
|
||||
.to_owned(),
|
||||
),
|
||||
not_found,
|
||||
!found,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,28 @@ pub async fn page(template: &str) -> Response<Body> {
|
|||
handlers::template::by_filename(template, &context, 500, None, false)
|
||||
}
|
||||
|
||||
pub async fn tree() -> Response<Body> {
|
||||
let mut context = tera::Context::default();
|
||||
let mut graph = Graph::load();
|
||||
|
||||
context.insert("graph", &graph);
|
||||
if let Some(root_node) = graph.get_root() {
|
||||
graph.nodes.remove(&root_node.id);
|
||||
context.insert("root_node", &root_node);
|
||||
context.insert(
|
||||
"nodes",
|
||||
&graph.nodes.values().cloned().collect::<Vec<Node>>(),
|
||||
);
|
||||
} else {
|
||||
context.insert(
|
||||
"nodes",
|
||||
&graph.nodes.values().cloned().collect::<Vec<Node>>(),
|
||||
);
|
||||
}
|
||||
|
||||
handlers::template::by_filename("tree.html", &context, 500, None, false)
|
||||
}
|
||||
|
||||
pub async fn data() -> Response<Body> {
|
||||
let mut context = tera::Context::default();
|
||||
let graph = Graph::load();
|
||||
|
|
|
|||
|
|
@ -157,10 +157,8 @@ mod tests {
|
|||
let node = crate::graph::Node::new(Some(payload.to_string()));
|
||||
let graph = Graph::load();
|
||||
context.insert("node", &node);
|
||||
context
|
||||
.insert("text", &crate::syntax::content::parse(&node.text, &graph));
|
||||
context.insert("graph", &graph);
|
||||
context.insert("incoming", &graph.incoming.get(&node.id));
|
||||
context.insert("config", &graph.meta.config);
|
||||
let (body, status) = render("node.html", &context, None);
|
||||
assert_eq!(status, 200);
|
||||
assert!(body.matches(payload).count() == 1);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue