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,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 {