2024-04-28 20:23:19 -04:00
|
|
|
use crate::database::data::Article;
|
2024-03-24 10:45:16 -04:00
|
|
|
use crate::html;
|
|
|
|
|
use askama::Template;
|
2024-04-28 20:23:19 -04:00
|
|
|
use axum::{
|
|
|
|
|
extract::{Extension, Path},
|
|
|
|
|
response::IntoResponse,
|
|
|
|
|
routing::{get, Router},
|
|
|
|
|
};
|
|
|
|
|
use core::panic;
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
use super::root::AppState;
|
2024-03-24 10:45:16 -04:00
|
|
|
|
|
|
|
|
pub fn get_router() -> Router {
|
|
|
|
|
Router::new()
|
|
|
|
|
.route("/", get(html::root::blog))
|
2024-04-28 20:23:19 -04:00
|
|
|
.route("/:article", get(article))
|
2024-03-24 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
2024-04-28 20:23:19 -04:00
|
|
|
#[template(path = "Article.html")]
|
|
|
|
|
struct ArticleTemplate {
|
2024-03-24 15:12:36 -04:00
|
|
|
active_navbar: &'static str,
|
2024-04-28 20:23:19 -04:00
|
|
|
next: String,
|
|
|
|
|
previous: String,
|
|
|
|
|
content: String,
|
2024-03-24 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
2024-04-28 20:23:19 -04:00
|
|
|
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(),
|
|
|
|
|
},
|
2024-03-24 10:45:16 -04:00
|
|
|
};
|
|
|
|
|
html::HtmlTemplate(template)
|
|
|
|
|
}
|