Using database for all blog references

This commit is contained in:
Awstin 2024-05-11 09:32:29 -04:00
parent ba8f641a74
commit 3657e66612
5 changed files with 47 additions and 36 deletions

View file

@ -19,12 +19,12 @@ pub trait PsqlData {
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Article { pub struct Article {
id: i32, id: i32,
reference: String, pub reference: String,
title: String, pub title: String,
pub content: String, pub content: String,
pub previous: Option<String>, pub previous: Option<String>,
pub next: Option<String>, pub next: Option<String>,
date: Option<Date>, pub date: Option<Date>,
} }
impl Article { impl Article {

View file

@ -1,4 +1,4 @@
use crate::database::data::Article; use crate::database::data::{Article, PsqlData};
use askama::Template; use askama::Template;
use axum::{ use axum::{
extract::{Extension, Path}, extract::{Extension, Path},
@ -6,7 +6,8 @@ use axum::{
routing::{get, Router}, routing::{get, Router},
}; };
use core::panic; use core::panic;
use std::collections::HashMap; use sqlx::PgPool;
use std::{collections::HashMap, error::Error};
use super::{root::AppState, HtmlTemplate, NavBar}; use super::{root::AppState, HtmlTemplate, NavBar};
@ -16,9 +17,14 @@ pub fn get_router() -> Router {
.route("/:article", get(article)) .route("/:article", get(article))
} }
pub async fn blog() -> impl IntoResponse { async fn blog(state: Extension<AppState>) -> impl IntoResponse {
let db_pool = &state.db;
let list: Vec<String> = get_articles_as_links_list(db_pool)
.await
.expect("couldn't get articles");
let template = BlogTemplate { let template = BlogTemplate {
active_navbar: NavBar::BLOG, active_navbar: NavBar::BLOG,
article_list: list.join("\n"),
}; };
HtmlTemplate(template) HtmlTemplate(template)
} }
@ -27,6 +33,7 @@ pub async fn blog() -> impl IntoResponse {
#[template(path = "blog.html")] #[template(path = "blog.html")]
struct BlogTemplate { struct BlogTemplate {
active_navbar: &'static str, active_navbar: &'static str,
article_list: String,
} }
#[derive(Template)] #[derive(Template)]
@ -62,3 +69,23 @@ async fn article(
}; };
HtmlTemplate(template) HtmlTemplate(template)
} }
pub async fn get_articles_as_links_list(pool: &PgPool) -> Result<Vec<String>, Box<dyn Error>> {
let mut articles: Vec<Article> = 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<String> = articles
.iter()
.map(|article| {
format!(
"<li><a href=\"/blog/{}\">{}</a></li>",
article.reference, article.title
)
})
.collect();
Ok(list)
}

View file

@ -8,6 +8,8 @@ use axum::{
use sqlx::PgPool; use sqlx::PgPool;
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use super::blog::get_articles_as_links_list;
#[derive(Clone)] #[derive(Clone)]
pub struct AppState { pub struct AppState {
pub db: PgPool, pub db: PgPool,
@ -35,9 +37,17 @@ pub fn get_router(pool: PgPool) -> Router {
.layer(Extension(state)) .layer(Extension(state))
} }
async fn home() -> impl IntoResponse { async fn home(state: Extension<AppState>) -> impl IntoResponse {
let db_pool = &state.db;
let list: Vec<String> = get_articles_as_links_list(db_pool)
.await
.expect("couldn't get articles");
let (head, tail) = list.split_at(5);
let template = HomeTemplate { let template = HomeTemplate {
active_navbar: NavBar::HOME, active_navbar: NavBar::HOME,
recent_blogs: head.join("\n"),
}; };
HtmlTemplate(template) HtmlTemplate(template)
} }
@ -46,6 +56,7 @@ async fn home() -> impl IntoResponse {
#[template(path = "home.html")] #[template(path = "home.html")]
struct HomeTemplate { struct HomeTemplate {
active_navbar: &'static str, active_navbar: &'static str,
recent_blogs: String,
} }
pub async fn projects() -> impl IntoResponse { pub async fn projects() -> impl IntoResponse {

View file

@ -5,30 +5,7 @@
<div id="content"> <div id="content">
<ul class="no-bul"> <ul class="no-bul">
<li><a href="/blog/fsdi">First Steps to Digital Independence</a></li> {{article_list|safe}}
<li><a href="/blog/independence">Independence</a></li>
<li><a href="/blog/pwi">Pets, Worry, and Information</a></li>
<li><a href="/blog/gs">Graduate School</a></li>
<li><a href="/blog/srw">Speed Reading Workbook</a></li>
<li><a href="/blog/tw">This Website</a></li>
<li><a href="/blog/lt">Leveling Tool</a></li>
<li><a href="/blog/writing">Writing</a></li>
<li><a href="/blog/lim">Less is More</a></li>
<li><a href="/blog/ile">Immersive learning and Experimentation</a></li>
<li><a href="/blog/dmwi">Doing more with my Ideas</a></li>
<li><a href="/blog/wgl">Why and Getting Lost</a></li>
<li><a href="/blog/dm">Discipline and Motivation</a></li>
<li><a href="/blog/llf">Looking like a Fool</a></li>
<li><a href="/blog/habits">Habits</a></li>
<li><a href="/blog/deepwork">Deep Work</a></li>
<li><a href="/blog/ewt">Exercising with little time</a></li>
<li><a href="/blog/afh">Asking for Help</a></li>
<li><a href="/blog/mfn">A Mind for Numbers</a></li>
<li><a href="/blog/sdl">Self Directed Learning</a></li>
<li><a href="/blog/foundation">The Foundation</a></li>
<li><a href="/blog/mop">The Myth of Perfection (in scheduling)</a></li>
<li><a href="/blog/onreading">On Reading</a></li>
<li><a href="/blog/thestart">The Start</a></li>
</ul> </ul>
</div> </div>

View file

@ -33,11 +33,7 @@
<h2>Blog</h2> <h2>Blog</h2>
<h3>Most Recent</h3> <h3>Most Recent</h3>
<ul class="no-bul"> <ul class="no-bul">
<li><a href="/blog/fsdi">First Steps to Digital Independence</a></li> {{recent_blogs|safe}}
<li><a href="/blog/independence">Independence</a></li>
<li><a href="/blog/pwi">Pets, Worry, and Information</a></li>
<li><a href="/blog/gs">Graduate School</a></li>
<li><a href="/blog/srw">Speed Reading Workbook</a></li>
</ul> </ul>
<p> <p>