use axum::extract::State; use axum::routing::post; use axum::Form; use axum::Router; use chrono::Utc; use serde::Deserialize; use sqlx::PgPool; use uuid::Uuid; #[derive(Debug, Deserialize)] struct FormData { email: String, name: String, } async fn subscribe(State(pool): State, Form(form): Form) { let _ = sqlx::query!( r#" INSERT INTO subscriptions (id, email, name, subscribed_at) VALUES ($1, $2, $3, $4) "#, Uuid::new_v4(), form.email, form.name, Utc::now() ) // We use `get_ref` to get an immutable reference to the `PgConnection` // wrapped by `web::Data`. .execute(&pool) .await; } pub fn routes_subscriptions(pool: PgPool) -> Router { Router::new() .route("/subscriptions", post(subscribe)) .with_state(pool) }