From a135acf943ba0fc442dca69f43339921728b1ed4 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Thu, 22 Jun 2023 15:35:18 +0100 Subject: implement blog tag filtering --- src/posts/mod.rs | 55 +++++++++++++++++++++++++++++++++++++++---------------- src/posts/note.rs | 8 +++++++- 2 files changed, 46 insertions(+), 17 deletions(-) (limited to 'src/posts') diff --git a/src/posts/mod.rs b/src/posts/mod.rs index be1e1a9..2dc5c4b 100644 --- a/src/posts/mod.rs +++ b/src/posts/mod.rs @@ -1,3 +1,6 @@ +mod article; +mod note; + use std::collections::HashSet; use async_trait::async_trait; @@ -16,12 +19,6 @@ enum PostType { Note, } -enum TextFormat { - Markdown, - Plaintext, - Html, -} - #[derive(Debug)] pub struct Post { // id: i64, @@ -34,7 +31,7 @@ pub struct Post { data: D, } -impl Post { +impl Post { pub async fn render(&mut self) -> Result<()> { self.render = Some(self.data.render().await?); Ok(()) @@ -74,6 +71,8 @@ pub async fn get_blogposts() -> Result>> { let blogpost = Post::try_from(blogpost).await.unwrap(); blogposts.push(blogpost); } + blogposts.sort_by_key(|post| post.created_at); + blogposts.reverse(); Ok(blogposts) } @@ -87,21 +86,45 @@ pub async fn get_blogpost(post_name: &str) -> Result> { path: file.path().to_str().unwrap_or_default().to_owned(), name: name.to_owned(), }; - let blogpost = Post::try_from(blogpost).await.unwrap(); + let blogpost = Post::try_from(blogpost).await?; return Ok(blogpost); } } Err(BlossomError::NotFound(Status::new(404))) } -pub fn get_tags(posts: &Vec>) -> HashSet { - posts.into_iter().fold(HashSet::new(), |mut acc, post| { - let tags = &post.tags; - for tag in tags { - acc.insert(tag.to_owned()); - } - acc - }) +pub fn get_tags(posts: &Vec>) -> Vec<&String> { + let mut tags = posts + .into_iter() + .fold(HashSet::new(), |mut acc, post| { + let tags = &post.tags; + for tag in tags { + acc.insert(tag); + } + acc + }) + .into_iter() + .collect::>(); + tags.sort(); + tags +} + +pub fn filter_by_tags<'p, D: Content>( + posts: Vec>, + filter_tags: &HashSet, +) -> Vec> { + posts + .into_iter() + .filter(|post| { + for tag in &post.tags { + match filter_tags.contains(tag) { + true => return true, + false => continue, + } + } + false + }) + .collect() } #[async_trait] diff --git a/src/posts/note.rs b/src/posts/note.rs index 95655d4..d7fa6c9 100644 --- a/src/posts/note.rs +++ b/src/posts/note.rs @@ -1 +1,7 @@ -struct note +enum TextFormat { + Markdown, + Plaintext, + Html, +} + +struct Note {} -- cgit