Basic post renderer and resolver
This commit is contained in:
parent
bcc18a83f9
commit
3967fe0253
5 changed files with 94 additions and 6 deletions
|
@ -42,7 +42,7 @@ async fn main() {
|
|||
|
||||
let app = Router::new()
|
||||
.route("/", get(routes::root))
|
||||
.route("/post/:slug", get(not_implemented))
|
||||
.route("/post/:slug", get(routes::post_resolver::resolver))
|
||||
.route("/:slug", get(routes::page_resolver::resolver))
|
||||
.with_state(app_state);
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
pub(crate) mod page_resolver;
|
||||
|
||||
pub(crate) mod post_resolver;
|
||||
|
||||
use askama::Template;
|
||||
use crate::{block_types::BlockTypes, AppState};
|
||||
|
|
88
src/routes/post_resolver.rs
Normal file
88
src/routes/post_resolver.rs
Normal file
|
@ -0,0 +1,88 @@
|
|||
use askama_axum::IntoResponse;
|
||||
use axum::extract::{State, Path};
|
||||
use axum::http::StatusCode;
|
||||
use log::{warn, error};
|
||||
|
||||
use crate::block_types;
|
||||
use crate::{block_types::BlockTypes, AppState};
|
||||
use crate::entities::{prelude::*, *};
|
||||
use sea_orm::*;
|
||||
|
||||
use super::{PostTemplate, NotFoundTemplate};
|
||||
|
||||
pub(crate) async fn resolver(
|
||||
Path(slug): Path<String>,
|
||||
state: State<AppState>
|
||||
) -> impl IntoResponse {
|
||||
|
||||
let menu: Vec<menu_entry::Model> = Menu::find()
|
||||
.filter(menu::Column::Name.eq("primary"))
|
||||
.one(&state.db_conn)
|
||||
.await
|
||||
.expect("Failed to get primary menu! HALTING")
|
||||
.unwrap()
|
||||
.find_related(MenuEntry)
|
||||
.order_by_asc(menu_entry::Column::Order)
|
||||
.all(&state.db_conn)
|
||||
.await
|
||||
.expect("Failed to get primary menu items!");
|
||||
|
||||
let post_search: Result<Option<post::Model>, DbErr> = Post::find()
|
||||
.filter(post::Column::Slug.eq(&slug))
|
||||
.one(&state.db_conn)
|
||||
.await;
|
||||
|
||||
let post_meta: post::Model = match post_search {
|
||||
Ok(post_result) => {
|
||||
match post_result {
|
||||
Some(post_result) => post_result,
|
||||
None => return (StatusCode::NOT_FOUND, NotFoundTemplate { slug, year: "2023".to_owned(), menu }).into_response(),
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
error!("{}", e);
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, ()).into_response();
|
||||
},
|
||||
};
|
||||
|
||||
let blocks: Vec<post_block::Model> = post_meta.find_related(PostBlock)
|
||||
.order_by_asc(post_block::Column::Order)
|
||||
.all(&state.db_conn)
|
||||
.await
|
||||
.expect("Failed to get post blocks! HALTING");
|
||||
|
||||
let content_blocks: Vec<BlockTypes> = blocks.into_iter().map(|f|
|
||||
match f.r#type.as_str() {
|
||||
"HR" => BlockTypes::HR,
|
||||
"PARAGRAPH" => BlockTypes::PARAGRAPH { text: f.content },
|
||||
"MARKDOWN" => BlockTypes::MARKDOWN { content: f.content },
|
||||
"HEADER" => {
|
||||
let deserde: block_types::Header = serde_json::from_str(&f.content.as_str()).expect("Incorrect HEADER formatting");
|
||||
|
||||
BlockTypes::HEADER { text: deserde.text, size: deserde.size
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
warn!("Unsupported block type! ({})", f.r#type.as_str());
|
||||
BlockTypes::UNSUPPORTED
|
||||
}
|
||||
}
|
||||
|
||||
).collect();
|
||||
|
||||
let last_updated = match post_meta.clone().updated {
|
||||
Some(updated) => Some(updated.to_string()),
|
||||
None => None,
|
||||
};
|
||||
|
||||
(StatusCode::FOUND, PostTemplate {
|
||||
draft: post_meta.clone().draft,
|
||||
title: post_meta.clone().title,
|
||||
summary: post_meta.clone().summary,
|
||||
publish_date: post_meta.clone().published.to_string(),
|
||||
last_updated,
|
||||
content_blocks,
|
||||
year: "2023".to_string(),
|
||||
menu,
|
||||
}).into_response()
|
||||
}
|
|
@ -5,9 +5,9 @@
|
|||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
||||
<title>Evie / eviee / uwueviee - {% block title %}{{ title }}{% endblock %}</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="static/css/sakura-pink.css" media="screen" type="text/css">
|
||||
<link rel="stylesheet" href="static/css/sakura-vader.css" media="screen and (prefers-color-scheme: dark)" type="text/css">
|
||||
<link rel="stylesheet" href="static/css/meow.css" type="text/css">
|
||||
<link rel="stylesheet" href="/static/css/sakura-pink.css" media="screen" type="text/css">
|
||||
<link rel="stylesheet" href="/static/css/sakura-vader.css" media="screen and (prefers-color-scheme: dark)" type="text/css">
|
||||
<link rel="stylesheet" href="/static/css/meow.css" type="text/css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>🐈 Evie / eviee / uwueviee</h1>
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
<h1>{{ title }}</h1>
|
||||
{% match summary %}
|
||||
{% when Some with (summary_content) %}
|
||||
<h2>{{ summary_content }}</h2>
|
||||
<b>{{ summary_content }}</b>
|
||||
{% when None %}
|
||||
{% endmatch %}
|
||||
{% match last_updated %}
|
||||
|
|
Reference in a new issue