From 18339f611fd17e1300593edd65adf7604a39ad72 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 14 Sep 2024 20:54:44 -0400 Subject: feat: working rudimentary blog presentation --- src/handlers.rs | 8 +++++++- src/main.rs | 1 + src/posts/abstractions/repo.rs | 1 + src/posts/fs_post.rs | 2 +- src/posts/fs_post_repo.rs | 9 +++++++++ src/views.rs | 1 + src/views/post.rs | 15 +++++++++++++++ templates/post.html | 19 ++++++++++++------- templates/posts.html | 12 +++++++----- 9 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 src/views/post.rs diff --git a/src/handlers.rs b/src/handlers.rs index db5cf7c..800d8f8 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,4 +1,5 @@ use askama::Template; +use crate::views::post::PostView; use crate::views::posts::PostsView; use crate::posts::abstractions::repo::PostRepo; use crate::views::policies::PoliciesTemplate; @@ -8,7 +9,7 @@ use crate::views::about::AboutView; use crate::tutors::abstractions::tutor_repo::TutorRepo; use std::sync::Arc; use axum::response::Html; -use axum::extract::State; +use axum::extract::{State, Path}; pub async fn about_handler(State(repo): State>) -> Html { let view = AboutView::with_tutors(repo.load()); @@ -32,3 +33,8 @@ pub async fn posts_handler(State(repo): State>) -> Html, State(repo): State>) -> Html { + let view = PostView::with_post(repo.by_id(&post_id)); + Html(view.render().unwrap()) +} + diff --git a/src/main.rs b/src/main.rs index fb15e51..4314292 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ async fn main() { let app = Router::new() .route("/", get(handlers::index_handler)) .route("/posts", get(handlers::posts_handler)) + .route("/posts/:post_id", get(handlers::post_handler)) .with_state(posts) .route("/policies", get(handlers::policies_handler)) .route("/brochure", get(handlers::brochure_handler)) diff --git a/src/posts/abstractions/repo.rs b/src/posts/abstractions/repo.rs index 6fcb385..6fd5d08 100644 --- a/src/posts/abstractions/repo.rs +++ b/src/posts/abstractions/repo.rs @@ -2,4 +2,5 @@ use crate::posts::abstractions::post::Post; pub trait PostRepo { fn load(&self) -> impl IntoIterator; + fn by_id(&self, post_id: &str) -> impl Post; } diff --git a/src/posts/fs_post.rs b/src/posts/fs_post.rs index e767803..f83ff4a 100644 --- a/src/posts/fs_post.rs +++ b/src/posts/fs_post.rs @@ -14,7 +14,7 @@ impl FsPost { impl Post for FsPost { fn get_title(&self) -> &str { - self.file.file_name().unwrap().to_str().unwrap() + self.file.file_stem().unwrap().to_str().unwrap() } fn get_article(&self) -> Cow { diff --git a/src/posts/fs_post_repo.rs b/src/posts/fs_post_repo.rs index eb37a6a..13f797b 100644 --- a/src/posts/fs_post_repo.rs +++ b/src/posts/fs_post_repo.rs @@ -1,3 +1,4 @@ +use crate::posts::abstractions::post::Post; use crate::posts::abstractions::repo::PostRepo; use crate::posts::fs_post::FsPost; use std::{fs, path::PathBuf}; @@ -20,4 +21,12 @@ impl PostRepo for FsPostRepo { .filter(|d| !d.file_name().to_string_lossy().starts_with('.')) .map(|d| FsPost::with_path(d.path())) } + + fn by_id(&self, post_id: &str) -> FsPost { + let posts = self.load(); + posts + .into_iter() + .find(|p| p.get_title() == post_id) + .unwrap() + } } diff --git a/src/views.rs b/src/views.rs index b986f56..cb58813 100644 --- a/src/views.rs +++ b/src/views.rs @@ -2,4 +2,5 @@ pub mod about; pub mod brochure; pub mod index; pub mod policies; +pub mod post; pub mod posts; diff --git a/src/views/post.rs b/src/views/post.rs new file mode 100644 index 0000000..4f0554b --- /dev/null +++ b/src/views/post.rs @@ -0,0 +1,15 @@ +use crate::helpers::*; +use crate::posts::abstractions::post::Post; +use askama::Template; + +#[derive(Template)] +#[template(path = "post.html")] +pub struct PostView { + post: P, +} + +impl PostView

{ + pub fn with_post(post: P) -> Self { + Self { post } + } +} diff --git a/templates/post.html b/templates/post.html index 8346f16..3155c7a 100644 --- a/templates/post.html +++ b/templates/post.html @@ -1,8 +1,13 @@ - - - -

+{% extends "base.html" %} + +{% block main %} + + +
+
{{ post.get_article()|markdown }} -
- - +
+ +{% endblock %} diff --git a/templates/posts.html b/templates/posts.html index 57d2480..0cb4cf1 100644 --- a/templates/posts.html +++ b/templates/posts.html @@ -1,13 +1,15 @@ {% extends "base.html" %} {% block main %} + +

-

    - {% for post in posts %} -
  • {{ post.get_title() }}
  • - {% endfor %} -
+ {% for post in posts %} +

{{ post.get_title() }}

+ {% endfor %}

{% endblock %} -- cgit v1.2.3