diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/src/main.rs b/src/main.rs index c08c2e1..40f546d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod scrobbles; mod skweets; use std::borrow::Cow; +use std::collections::HashSet; use rocket::fs::{relative, FileServer}; use rocket::http::Status; @@ -47,24 +48,6 @@ async fn home(clients: &State<Clients>) -> Template { ) } -#[get("/blog")] -async fn blog() -> Template { - let mut blogposts = posts::get_blogposts().await.unwrap_or_default(); - let tags = posts::get_tags(&blogposts); - for blogpost in &mut blogposts { - blogpost.render().await; - } - let reverse = "reverse".to_owned(); - Template::render( - "blog", - context! { - reverse, - blogposts, - tags, - }, - ) -} - #[get("/blog/<blogpost>")] async fn blogpost(blogpost: &str) -> Result<Template> { let mut blogpost = posts::get_blogpost(blogpost).await?; @@ -77,6 +60,33 @@ async fn blogpost(blogpost: &str) -> Result<Template> { )) } +#[get("/blog?<filter>")] +async fn blog(filter: Vec<String>) -> Result<Template> { + let mut blogposts = posts::get_blogposts().await?; + let tags: Vec<String> = posts::get_tags(&blogposts) + .into_iter() + .map(|tag| tag.to_owned()) + .collect(); + let mut filter_hashset: HashSet<String> = HashSet::new(); + if !filter.is_empty() { + filter_hashset.extend(filter.into_iter()); + blogposts = posts::filter_by_tags(blogposts, &filter_hashset); + } + for blogpost in &mut blogposts { + blogpost.render().await?; + } + let reverse = true; + Ok(Template::render( + "blog", + context! { + reverse, + tags, + filter_hashset, + blogposts, + }, + )) +} + #[get("/contact")] async fn contact() -> Template { Template::render("contact", context! {}) |