achubb_website/src/html/blog.rs

53 lines
1.3 KiB
Rust

use crate::database::data::Article;
use crate::html;
use askama::Template;
use axum::{
extract::{Extension, Path},
response::IntoResponse,
routing::{get, Router},
};
use core::panic;
use std::collections::HashMap;
use super::root::AppState;
pub fn get_router() -> Router {
Router::new()
.route("/", get(html::root::blog))
.route("/:article", get(article))
}
#[derive(Template)]
#[template(path = "Article.html")]
struct ArticleTemplate {
active_navbar: &'static str,
next: String,
previous: String,
content: String,
}
async fn article(
state: Extension<AppState>,
Path(params): Path<HashMap<String, String>>,
) -> 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: html::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(),
},
};
html::HtmlTemplate(template)
}