From 3657e66612e8b974c07ad5d15fe908a90f535960 Mon Sep 17 00:00:00 2001 From: Awstin Date: Sat, 11 May 2024 09:32:29 -0400 Subject: [PATCH] Using database for all blog references --- src/database/data.rs | 6 +++--- src/html/blog.rs | 33 ++++++++++++++++++++++++++++++--- src/html/root.rs | 13 ++++++++++++- templates/blog.html | 25 +------------------------ templates/home.html | 6 +----- 5 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/database/data.rs b/src/database/data.rs index 0c82f2d..99c14e1 100644 --- a/src/database/data.rs +++ b/src/database/data.rs @@ -19,12 +19,12 @@ pub trait PsqlData { #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] pub struct Article { id: i32, - reference: String, - title: String, + pub reference: String, + pub title: String, pub content: String, pub previous: Option, pub next: Option, - date: Option, + pub date: Option, } impl Article { diff --git a/src/html/blog.rs b/src/html/blog.rs index b669a73..34136bd 100644 --- a/src/html/blog.rs +++ b/src/html/blog.rs @@ -1,4 +1,4 @@ -use crate::database::data::Article; +use crate::database::data::{Article, PsqlData}; use askama::Template; use axum::{ extract::{Extension, Path}, @@ -6,7 +6,8 @@ use axum::{ routing::{get, Router}, }; use core::panic; -use std::collections::HashMap; +use sqlx::PgPool; +use std::{collections::HashMap, error::Error}; use super::{root::AppState, HtmlTemplate, NavBar}; @@ -16,9 +17,14 @@ pub fn get_router() -> Router { .route("/:article", get(article)) } -pub async fn blog() -> impl IntoResponse { +async fn blog(state: Extension) -> impl IntoResponse { + let db_pool = &state.db; + let list: Vec = get_articles_as_links_list(db_pool) + .await + .expect("couldn't get articles"); let template = BlogTemplate { active_navbar: NavBar::BLOG, + article_list: list.join("\n"), }; HtmlTemplate(template) } @@ -27,6 +33,7 @@ pub async fn blog() -> impl IntoResponse { #[template(path = "blog.html")] struct BlogTemplate { active_navbar: &'static str, + article_list: String, } #[derive(Template)] @@ -62,3 +69,23 @@ async fn article( }; HtmlTemplate(template) } + +pub async fn get_articles_as_links_list(pool: &PgPool) -> Result, Box> { + let mut articles: Vec
= match Article::read_all(pool).await { + Ok(a) => a.iter().map(|x| *x.clone()).collect(), + Err(_) => panic!("Not an article at all!!!!"), + }; + + articles.sort_by(|a, b| b.date.cmp(&a.date)); + + let list: Vec = articles + .iter() + .map(|article| { + format!( + "
  • {}
  • ", + article.reference, article.title + ) + }) + .collect(); + Ok(list) +} diff --git a/src/html/root.rs b/src/html/root.rs index a67c0a0..c6c1e92 100644 --- a/src/html/root.rs +++ b/src/html/root.rs @@ -8,6 +8,8 @@ use axum::{ use sqlx::PgPool; use tower_http::services::ServeDir; +use super::blog::get_articles_as_links_list; + #[derive(Clone)] pub struct AppState { pub db: PgPool, @@ -35,9 +37,17 @@ pub fn get_router(pool: PgPool) -> Router { .layer(Extension(state)) } -async fn home() -> impl IntoResponse { +async fn home(state: Extension) -> impl IntoResponse { + let db_pool = &state.db; + let list: Vec = get_articles_as_links_list(db_pool) + .await + .expect("couldn't get articles"); + + let (head, tail) = list.split_at(5); + let template = HomeTemplate { active_navbar: NavBar::HOME, + recent_blogs: head.join("\n"), }; HtmlTemplate(template) } @@ -46,6 +56,7 @@ async fn home() -> impl IntoResponse { #[template(path = "home.html")] struct HomeTemplate { active_navbar: &'static str, + recent_blogs: String, } pub async fn projects() -> impl IntoResponse { diff --git a/templates/blog.html b/templates/blog.html index ba668d8..504b869 100644 --- a/templates/blog.html +++ b/templates/blog.html @@ -5,30 +5,7 @@ diff --git a/templates/home.html b/templates/home.html index 8ce8515..35713e2 100644 --- a/templates/home.html +++ b/templates/home.html @@ -33,11 +33,7 @@

    Blog

    Most Recent