summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/handlers.rs8
-rw-r--r--src/main.rs1
-rw-r--r--src/posts/abstractions/repo.rs1
-rw-r--r--src/posts/fs_post.rs2
-rw-r--r--src/posts/fs_post_repo.rs9
-rw-r--r--src/views.rs1
-rw-r--r--src/views/post.rs15
7 files changed, 35 insertions, 2 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 }
+ }
+}