Templates refactoring

This commit is contained in:
Alphonse Paix
2025-09-20 04:43:55 +02:00
parent d85879a004
commit f7ebf73fbc
15 changed files with 156 additions and 146 deletions

View File

@@ -1,5 +1,7 @@
mod new_subscriber;
mod post;
mod subscriber_email;
pub use new_subscriber::NewSubscriber;
pub use post::PostEntry;
pub use subscriber_email::SubscriberEmail;

17
src/domain/post.rs Normal file
View File

@@ -0,0 +1,17 @@
use chrono::{DateTime, Utc};
use uuid::Uuid;
pub struct PostEntry {
pub post_id: Uuid,
pub author: Option<String>,
pub title: String,
pub content: String,
pub published_at: DateTime<Utc>,
}
impl PostEntry {
#[allow(dead_code)]
pub fn formatted_date(&self) -> String {
self.published_at.format("%B %d, %Y").to_string()
}
}

View File

@@ -22,7 +22,7 @@ pub use subscriptions_confirm::*;
use crate::{
authentication::AuthError,
templates::{InternalErrorTemplate, MessageTemplate},
templates::{InternalErrorTemplate, MessageTemplate, NotFoundTemplate},
};
#[derive(thiserror::Error)]
@@ -130,10 +130,6 @@ impl From<AuthError> for AppError {
}
}
#[derive(Template)]
#[template(path = "../templates/404.html")]
struct NotFoundTemplate;
pub async fn not_found() -> Response {
(
StatusCode::NOT_FOUND,

View File

@@ -1,4 +1,4 @@
use crate::authentication::AuthenticatedUser;
use crate::{authentication::AuthenticatedUser, templates::DashboardTemplate};
use askama::Template;
use axum::{
Extension,
@@ -6,14 +6,6 @@ use axum::{
};
use uuid::Uuid;
#[derive(Template)]
#[template(path = "../templates/dashboard.html")]
struct DashboardTemplate {
username: String,
idempotency_key_1: String,
idempotency_key_2: String,
}
pub async fn admin_dashboard(
Extension(AuthenticatedUser { username, .. }): Extension<AuthenticatedUser>,
) -> Response {

View File

@@ -1,9 +1,7 @@
use askama::Template;
use axum::response::Html;
#[derive(Template)]
#[template(path = "../templates/home.html")]
struct HomeTemplate;
use crate::templates::HomeTemplate;
pub async fn home() -> Html<String> {
Html(HomeTemplate.render().unwrap())

View File

@@ -3,6 +3,7 @@ use crate::{
routes::AppError,
session_state::TypedSession,
startup::AppState,
templates::LoginTemplate,
};
use anyhow::Context;
use askama::Template;
@@ -15,10 +16,6 @@ use axum::{
use axum::{http::StatusCode, response::Redirect};
use secrecy::SecretString;
#[derive(Template)]
#[template(path = "../templates/login.html")]
struct LoginTemplate;
#[derive(serde::Deserialize)]
pub struct LoginFormData {
username: String,

View File

@@ -1,41 +1,18 @@
use crate::{routes::AppError, startup::AppState};
use crate::{
domain::PostEntry,
routes::AppError,
startup::AppState,
templates::{PostTemplate, PostsTemplate},
};
use anyhow::Context;
use askama::Template;
use axum::{
extract::{Path, State},
response::{Html, IntoResponse, Response},
};
use chrono::{DateTime, Utc};
use sqlx::PgPool;
use uuid::Uuid;
struct PostEntry {
post_id: Uuid,
author: Option<String>,
title: String,
content: String,
published_at: DateTime<Utc>,
}
impl PostEntry {
#[allow(dead_code)]
fn formatted_date(&self) -> String {
self.published_at.format("%B %d, %Y").to_string()
}
}
#[derive(Template)]
#[template(path = "../templates/posts.html")]
struct PostsTemplate {
posts: Vec<PostEntry>,
}
#[derive(Template)]
#[template(path = "../templates/post.html")]
struct PostTemplate {
post: PostEntry,
}
pub async fn list_posts(
State(AppState {
connection_pool, ..

View File

@@ -1,4 +1,4 @@
use crate::startup::AppState;
use crate::{startup::AppState, templates::ConfirmTemplate};
use askama::Template;
use axum::{
extract::{Query, State},
@@ -9,10 +9,6 @@ use serde::Deserialize;
use sqlx::PgPool;
use uuid::Uuid;
#[derive(Template)]
#[template(path = "../templates/confirm.html")]
struct ConfirmTemplate;
#[tracing::instrument(name = "Confirming new subscriber", skip(params))]
pub async fn confirm(
State(AppState {

View File

@@ -1,5 +1,7 @@
use askama::Template;
use crate::domain::PostEntry;
#[derive(Template)]
pub enum MessageTemplate {
#[template(path = "../templates/success.html")]
@@ -11,3 +13,39 @@ pub enum MessageTemplate {
#[derive(Template)]
#[template(path = "../templates/500.html")]
pub struct InternalErrorTemplate;
#[derive(Template)]
#[template(path = "../templates/login.html")]
pub struct LoginTemplate;
#[derive(Template)]
#[template(path = "../templates/dashboard.html")]
pub struct DashboardTemplate {
pub username: String,
pub idempotency_key_1: String,
pub idempotency_key_2: String,
}
#[derive(Template)]
#[template(path = "../templates/home.html")]
pub struct HomeTemplate;
#[derive(Template)]
#[template(path = "../templates/posts.html")]
pub struct PostsTemplate {
pub posts: Vec<PostEntry>,
}
#[derive(Template)]
#[template(path = "../templates/post.html")]
pub struct PostTemplate {
pub post: PostEntry,
}
#[derive(Template)]
#[template(path = "../templates/confirm.html")]
pub struct ConfirmTemplate;
#[derive(Template)]
#[template(path = "../templates/404.html")]
pub struct NotFoundTemplate;