refactor modules
This commit is contained in:
@ -2,33 +2,33 @@ use askama::Template;
|
||||
use axum::{extract::Path, http::StatusCode};
|
||||
|
||||
use crate::{
|
||||
blog_posts::blog_post_model::{BlogPostMetadata, BLOG_POST_PATH},
|
||||
components::{
|
||||
site_footer::{render_site_footer, SiteFooter},
|
||||
site_header::{HeaderProps, Link},
|
||||
},
|
||||
filters,
|
||||
post_list::get_post_list,
|
||||
post_parser::ParseResult,
|
||||
post_utils::{post_listing::get_post_list, post_parser::ParseResult},
|
||||
};
|
||||
|
||||
use super::post::{PostMetadata, BLOG_POST_PATH};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "post_list.html")]
|
||||
pub struct PostListTemplate {
|
||||
pub title: String,
|
||||
pub posts: Vec<ParseResult<PostMetadata>>,
|
||||
pub posts: Vec<ParseResult<BlogPostMetadata>>,
|
||||
pub tag: Option<String>,
|
||||
pub site_footer: SiteFooter,
|
||||
pub header_props: HeaderProps,
|
||||
}
|
||||
|
||||
pub async fn render_post_list(tag: Option<Path<String>>) -> Result<PostListTemplate, StatusCode> {
|
||||
pub async fn render_blog_post_list(
|
||||
tag: Option<Path<String>>,
|
||||
) -> Result<PostListTemplate, StatusCode> {
|
||||
// I will forget what happens here in a week. But essentially it's pattern matching and shadowing
|
||||
let tag = tag.map(|Path(tag)| tag);
|
||||
|
||||
let site_footer = render_site_footer().await?;
|
||||
let mut post_list = get_post_list::<PostMetadata>(BLOG_POST_PATH).await?;
|
||||
let mut post_list = get_post_list::<BlogPostMetadata>(BLOG_POST_PATH).await?;
|
||||
post_list.sort_by_key(|post| post.metadata.date);
|
||||
post_list.reverse();
|
||||
|
43
axum_server/src/pages/blog_post_page.rs
Normal file
43
axum_server/src/pages/blog_post_page.rs
Normal file
@ -0,0 +1,43 @@
|
||||
use askama::Template;
|
||||
use axum::{extract::Path, http::StatusCode};
|
||||
use chrono::{DateTime, Utc};
|
||||
use tokio::try_join;
|
||||
|
||||
use crate::{
|
||||
blog_posts::blog_post_model::BlogPostMetadata, components::site_header::Link, filters,
|
||||
post_utils::post_parser::parse_post,
|
||||
};
|
||||
|
||||
use crate::components::{
|
||||
site_footer::{render_site_footer, SiteFooter},
|
||||
site_header::HeaderProps,
|
||||
};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "blog_post.html")]
|
||||
pub struct BlogPostTemplate {
|
||||
pub title: String,
|
||||
pub body: String,
|
||||
pub date: DateTime<Utc>,
|
||||
pub tags: Vec<String>,
|
||||
pub site_footer: SiteFooter,
|
||||
pub header_props: HeaderProps,
|
||||
}
|
||||
|
||||
pub async fn render_blog_post(Path(post_id): Path<String>) -> Result<BlogPostTemplate, StatusCode> {
|
||||
let path = format!("../_posts/blog/{}.md", post_id);
|
||||
let parse_post = parse_post::<BlogPostMetadata>(&path);
|
||||
let (site_footer, parsed) = try_join!(render_site_footer(), parse_post)?;
|
||||
|
||||
Ok(BlogPostTemplate {
|
||||
title: parsed.metadata.title,
|
||||
date: parsed.metadata.date,
|
||||
tags: parsed.metadata.tags,
|
||||
body: parsed.body,
|
||||
site_footer,
|
||||
header_props: HeaderProps::with_back_link(Link {
|
||||
href: "/blog".to_string(),
|
||||
label: "All posts".to_string(),
|
||||
}),
|
||||
})
|
||||
}
|
@ -2,36 +2,35 @@ use askama::Template;
|
||||
use axum::http::StatusCode;
|
||||
use tokio::try_join;
|
||||
|
||||
use crate::filters;
|
||||
use crate::{
|
||||
blog_posts::{
|
||||
blog_post_model::BlogPostMetadata, featured_blog_posts::get_featured_blog_posts,
|
||||
tag_list::get_popular_blog_tags,
|
||||
},
|
||||
components::{
|
||||
site_footer::{render_site_footer, SiteFooter},
|
||||
site_header::HeaderProps,
|
||||
},
|
||||
featured_posts::get_featured_posts,
|
||||
featured_projects::get_featured_projects,
|
||||
post_parser::ParseResult,
|
||||
tag_list::get_popular_blog_tags,
|
||||
filters,
|
||||
post_utils::post_parser::ParseResult,
|
||||
projects::{featured_projects::get_featured_projects, project_model::ProjectMetadata},
|
||||
};
|
||||
|
||||
use super::post::PostMetadata;
|
||||
use super::project::ProjectMetadata;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
pub struct IndexTemplate {
|
||||
site_footer: SiteFooter,
|
||||
header_props: HeaderProps,
|
||||
blog_tags: Vec<String>,
|
||||
featured_posts: Vec<ParseResult<PostMetadata>>,
|
||||
featured_blog_posts: Vec<ParseResult<BlogPostMetadata>>,
|
||||
featured_projects: Vec<ParseResult<ProjectMetadata>>,
|
||||
}
|
||||
|
||||
pub async fn render_index() -> Result<IndexTemplate, StatusCode> {
|
||||
let (site_footer, blog_tags, featured_posts, featured_projects) = try_join!(
|
||||
let (site_footer, blog_tags, featured_blog_posts, featured_projects) = try_join!(
|
||||
render_site_footer(),
|
||||
get_popular_blog_tags(),
|
||||
get_featured_posts(),
|
||||
get_featured_blog_posts(),
|
||||
get_featured_projects()
|
||||
)?;
|
||||
|
||||
@ -39,7 +38,7 @@ pub async fn render_index() -> Result<IndexTemplate, StatusCode> {
|
||||
site_footer,
|
||||
header_props: HeaderProps::default(),
|
||||
blog_tags,
|
||||
featured_posts,
|
||||
featured_blog_posts,
|
||||
featured_projects,
|
||||
})
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
pub mod admin;
|
||||
pub mod blog_post_list;
|
||||
pub mod blog_post_page;
|
||||
pub mod contact;
|
||||
pub mod index;
|
||||
pub mod post;
|
||||
pub mod post_list;
|
||||
pub mod project;
|
||||
|
@ -1,57 +0,0 @@
|
||||
use crate::{components::site_header::Link, filters};
|
||||
use askama::Template;
|
||||
use axum::{extract::Path, http::StatusCode};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Deserialize;
|
||||
use tokio::try_join;
|
||||
|
||||
use crate::{
|
||||
components::{
|
||||
site_footer::{render_site_footer, SiteFooter},
|
||||
site_header::HeaderProps,
|
||||
},
|
||||
post_parser::{deserialize_date, parse_post},
|
||||
};
|
||||
|
||||
pub const BLOG_POST_PATH: &str = "../_posts/blog";
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct PostMetadata {
|
||||
pub layout: String,
|
||||
pub title: String,
|
||||
pub segments: Vec<String>,
|
||||
pub published: bool,
|
||||
#[serde(deserialize_with = "deserialize_date")]
|
||||
pub date: DateTime<Utc>,
|
||||
pub thumbnail: Option<String>,
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "post.html")]
|
||||
pub struct PostTemplate {
|
||||
pub title: String,
|
||||
pub body: String,
|
||||
pub date: DateTime<Utc>,
|
||||
pub tags: Vec<String>,
|
||||
pub site_footer: SiteFooter,
|
||||
pub header_props: HeaderProps,
|
||||
}
|
||||
|
||||
pub async fn render_post(Path(post_id): Path<String>) -> Result<PostTemplate, StatusCode> {
|
||||
let path = format!("../_posts/blog/{}.md", post_id);
|
||||
let parse_post = parse_post::<PostMetadata>(&path);
|
||||
let (site_footer, parsed) = try_join!(render_site_footer(), parse_post)?;
|
||||
|
||||
Ok(PostTemplate {
|
||||
title: parsed.metadata.title,
|
||||
date: parsed.metadata.date,
|
||||
tags: parsed.metadata.tags,
|
||||
body: parsed.body,
|
||||
site_footer,
|
||||
header_props: HeaderProps::with_back_link(Link {
|
||||
href: "/blog".to_string(),
|
||||
label: "All posts".to_string(),
|
||||
}),
|
||||
})
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct ProjectMetadata {
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub classification: String,
|
||||
pub displayed: bool,
|
||||
pub cover_image: Option<String>,
|
||||
pub tags: Vec<String>,
|
||||
pub featured: bool,
|
||||
pub link: Option<String>,
|
||||
}
|
||||
|
||||
pub fn translate_classification(classification: &str) -> &str {
|
||||
match classification {
|
||||
"webapp" => "Web application",
|
||||
"website" => "Web site",
|
||||
"presentation" => "Presentation",
|
||||
"videogame" => "Video game",
|
||||
any => any,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user