use crate::database::data::Article; use askama::Template; use axum::{ extract::{Extension, Path}, response::IntoResponse, routing::{get, Router}, }; use core::panic; use std::collections::HashMap; use super::{root::AppState, HtmlTemplate, NavBar}; pub fn get_router() -> Router { Router::new() .route("/", get(blog)) .route("/:article", get(article)) } pub async fn blog() -> impl IntoResponse { let template = BlogTemplate { active_navbar: NavBar::BLOG, }; HtmlTemplate(template) } #[derive(Template)] #[template(path = "blog.html")] struct BlogTemplate { active_navbar: &'static str, } #[derive(Template)] #[template(path = "Article.html")] struct ArticleTemplate { active_navbar: &'static str, next: String, previous: String, content: String, } async fn article( state: Extension, Path(params): Path>, ) -> impl IntoResponse { let db_pool = &state.db; let article_id: &String = params.get("article").unwrap(); let article: Article = match Article::read_by_reference(db_pool, article_id).await { Ok(a) => *a, Err(_) => panic!("Not an article at all!!!!"), }; let template = ArticleTemplate { active_navbar: NavBar::BLOG, content: article.content, previous: match article.previous { Some(a) => a, None => "".to_string(), }, next: match article.next { Some(a) => a, None => "".to_string(), }, }; HtmlTemplate(template) }