From 9f341d439f7aa5fd2365024169ead2d6bdc3210c Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 14 Sep 2024 20:30:05 -0400 Subject: feat: rewrite complete --- src/posts/abstractions/post.rs | 6 ++++++ src/posts/abstractions/repo.rs | 5 +++++ 2 files changed, 11 insertions(+) create mode 100644 src/posts/abstractions/post.rs create mode 100644 src/posts/abstractions/repo.rs (limited to 'src/posts/abstractions') diff --git a/src/posts/abstractions/post.rs b/src/posts/abstractions/post.rs new file mode 100644 index 0000000..6d504db --- /dev/null +++ b/src/posts/abstractions/post.rs @@ -0,0 +1,6 @@ +use std::{borrow::Cow, fmt}; + +pub trait Post: fmt::Debug { + fn get_title(&self) -> &str; + fn get_article(&self) -> Cow; +} diff --git a/src/posts/abstractions/repo.rs b/src/posts/abstractions/repo.rs new file mode 100644 index 0000000..6fcb385 --- /dev/null +++ b/src/posts/abstractions/repo.rs @@ -0,0 +1,5 @@ +use crate::posts::abstractions::post::Post; + +pub trait PostRepo { + fn load(&self) -> impl IntoIterator; +} -- cgit v1.3 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 (limited to 'src/posts/abstractions') 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.3 From aad33fee01abe817d82c094f83ffd44ca36c8456 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 14 Sep 2024 21:11:51 -0400 Subject: feat: reverse sort posts by modify datetime --- src/posts/abstractions/post.rs | 2 +- src/posts/fs_post.rs | 25 ++++++++++++++++++++++++- src/views/posts.rs | 7 ++++--- templates/base.html | 1 + 4 files changed, 30 insertions(+), 5 deletions(-) (limited to 'src/posts/abstractions') diff --git a/src/posts/abstractions/post.rs b/src/posts/abstractions/post.rs index 6d504db..a941281 100644 --- a/src/posts/abstractions/post.rs +++ b/src/posts/abstractions/post.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, fmt}; -pub trait Post: fmt::Debug { +pub trait Post: fmt::Debug + Ord { fn get_title(&self) -> &str; fn get_article(&self) -> Cow; } diff --git a/src/posts/fs_post.rs b/src/posts/fs_post.rs index f83ff4a..8b8e725 100644 --- a/src/posts/fs_post.rs +++ b/src/posts/fs_post.rs @@ -1,7 +1,7 @@ use crate::posts::abstractions::post::Post; use std::{borrow::Cow, fs, path::PathBuf}; -#[derive(Debug)] +#[derive(Debug, Eq)] pub struct FsPost { file: PathBuf, } @@ -22,3 +22,26 @@ impl Post for FsPost { Cow::Owned(article) } } + +impl Ord for FsPost { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.file + .metadata() + .unwrap() + .modified() + .unwrap() + .cmp(&other.file.metadata().unwrap().modified().unwrap()) + } +} + +impl PartialOrd for FsPost { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for FsPost { + fn eq(&self, other: &Self) -> bool { + self.get_title() == other.get_title() + } +} diff --git a/src/views/posts.rs b/src/views/posts.rs index 2ce69f8..82b5996 100644 --- a/src/views/posts.rs +++ b/src/views/posts.rs @@ -10,8 +10,9 @@ pub struct PostsView { impl PostsView

{ pub fn with_posts(posts: impl IntoIterator) -> Self { - Self { - posts: posts.into_iter().collect(), - } + let mut posts: Vec

= posts.into_iter().collect(); + posts.sort(); + posts.reverse(); + Self { posts } } } diff --git a/templates/base.html b/templates/base.html index f146a44..79ba662 100644 --- a/templates/base.html +++ b/templates/base.html @@ -33,6 +33,7 @@ Policies Pricing Brochure + Posts Offerings logo -- cgit v1.3