feat: add sqlx transaction extractor to router

This commit is contained in:
Alexander Navarro 2025-02-22 18:54:44 -03:00
parent cfaf7ecc1f
commit a662b94b21
8 changed files with 79 additions and 14 deletions

View file

@ -1,12 +1,13 @@
mod error;
pub mod router;
use axum::extract::FromRef;
pub use error::{Error, Result, ResultTemplate};
use minijinja::{Environment, Template};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
pub type Tx = axum_sqlx_tx::Tx<sqlx::Postgres>;
pub type TxState = axum_sqlx_tx::State<sqlx::Postgres>;
#[derive(Serialize, Deserialize, Debug)]
pub struct Link {
@ -15,13 +16,15 @@ pub struct Link {
pub subpages: Vec<Self>,
}
#[derive(Clone, FromRef)]
pub struct AppState {
tmpl_env: Environment<'static>,
tx: TxState,
}
impl AppState {
pub fn new(tmpl_env: Environment<'static>) -> Arc<Self> {
Arc::new(AppState { tmpl_env })
pub fn new(tmpl_env: Environment<'static>, tx: TxState) -> Self {
AppState { tmpl_env, tx }
}
pub fn get_template(&self, name: &str) -> Result<Template> {