diff options
-rw-r--r-- | src/handlers.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/posts/abstractions/repo.rs | 1 | ||||
-rw-r--r-- | src/posts/fs_post.rs | 2 | ||||
-rw-r--r-- | src/posts/fs_post_repo.rs | 9 | ||||
-rw-r--r-- | src/views.rs | 1 | ||||
-rw-r--r-- | src/views/post.rs | 15 | ||||
-rw-r--r-- | templates/post.html | 19 | ||||
-rw-r--r-- | templates/posts.html | 12 |
9 files changed, 54 insertions, 14 deletions
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<Arc<impl TutorRepo>>) -> Html<String> { let view = AboutView::with_tutors(repo.load()); @@ -32,3 +33,8 @@ pub async fn posts_handler(State(repo): State<Arc<impl PostRepo>>) -> Html<Strin Html(view.render().unwrap()) } +pub async fn post_handler(Path(post_id): Path<String>, State(repo): State<Arc<impl PostRepo>>) -> Html<String> { + 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<Item = impl Post>; + 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<str> { 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<P: Post> { + post: P, +} + +impl<P: Post> PostView<P> { + 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 @@ -<!DOCTYPE html> -<html> - <body> - <article> +{% extends "base.html" %} + +{% block main %} +<section class="banner"> + <h1>{{ post.get_title() }}</h1> +</section> + +<section class="quiet"> + <article> {{ post.get_article()|markdown }} - </article> - </body> -</html> + </article> +</section> +{% 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 %} +<section class="banner"> + <h1>Journal Posts</h1> +</section> + <section class="quiet"> <p> - <ul> - {% for post in posts %} - <li>{{ post.get_title() }}</li> - {% endfor %} - </ul> + {% for post in posts %} + <h3><a href="/posts/{{ post.get_title()|e }}">{{ post.get_title() }}</a></h3> + {% endfor %} </p> <section> {% endblock %} |