home: wip news

This commit is contained in:
Agahnim 2026-03-23 15:27:52 +01:00
parent f360250b57
commit a753fd3497
9 changed files with 507 additions and 19 deletions

View file

@ -1,8 +1,10 @@
use chrono::NaiveDate;
use serde::Deserialize;
pub struct AppState {
pub mp_tracks: Vec<Track>,
pub p_tracks: Vec<Track>,
pub website_news: Vec<WebsiteArticle>,
}
impl AppState {
@ -18,13 +20,25 @@ impl AppState {
.expect("p-tracks.json non trouvé"),
)
.expect("JSON invalide"),
website_news: serde_json::from_str(
&std::fs::read_to_string("content/web-articles.json")
.expect("web-articles.json non trouvé"),
)
.expect("JSON invalide"),
}
}
}
#[derive(Deserialize, Clone)]
#[derive(Deserialize)]
pub struct Track {
pub title: String,
pub artist: String,
pub src: String,
pub album: Option<String>,
}
#[derive(Deserialize)]
pub struct WebsiteArticle {
pub date: NaiveDate,
pub body: String,
}

View file

@ -6,25 +6,35 @@ use axum::{
};
use std::sync::Arc;
use crate::domain::AppState;
use crate::domain::{AppState, WebsiteArticle};
#[derive(Template)]
#[template(path = "home.html")]
struct HomeTemplate {
tracks: Vec<crate::domain::Track>,
struct HomeTemplate<'a> {
tracks: &'a Vec<crate::domain::Track>,
news: &'a Vec<WebsiteArticle>,
}
#[derive(Template)]
#[template(path = "partials/home.html")]
struct HomePartialTemplate;
struct HomePartialTemplate<'a> {
news: &'a Vec<WebsiteArticle>,
}
pub async fn home(headers: HeaderMap, state: State<Arc<AppState>>) -> impl IntoResponse {
if headers.contains_key("hx-request") {
Html(HomePartialTemplate.render().unwrap())
Html(
HomePartialTemplate {
news: &state.website_news,
}
.render()
.unwrap(),
)
} else {
Html(
HomeTemplate {
tracks: state.mp_tracks.clone(),
tracks: &state.mp_tracks,
news: &state.website_news,
}
.render()
.unwrap(),

View file

@ -10,8 +10,8 @@ use crate::domain::AppState;
#[derive(Template)]
#[template(path = "music.html")]
struct MusicTemplate {
tracks: Vec<crate::domain::Track>,
struct MusicTemplate<'a> {
tracks: &'a Vec<crate::domain::Track>,
}
#[derive(Template)]
@ -24,7 +24,7 @@ pub async fn music(headers: HeaderMap, state: State<Arc<AppState>>) -> impl Into
} else {
Html(
MusicTemplate {
tracks: state.p_tracks.clone(),
tracks: &state.p_tracks,
}
.render()
.unwrap(),

View file

@ -11,8 +11,8 @@ use crate::domain::AppState;
#[derive(Template)]
#[template(path = "notfound.html")]
struct NotfoundTemplate {
tracks: Vec<crate::domain::Track>,
struct NotfoundTemplate<'a> {
tracks: &'a Vec<crate::domain::Track>,
}
#[derive(Template)]
@ -25,7 +25,7 @@ pub async fn notfound(headers: HeaderMap, state: State<Arc<AppState>>) -> impl I
} else {
Html(
NotfoundTemplate {
tracks: state.mp_tracks.clone(),
tracks: &state.mp_tracks,
}
.render()
.unwrap(),