2024-01-28 22:22:29 +01:00
|
|
|
use axum::extract::State;
|
2024-01-01 21:02:31 +01:00
|
|
|
use axum::routing::post;
|
2024-01-28 22:22:29 +01:00
|
|
|
use axum::Form;
|
2023-12-30 22:21:57 +01:00
|
|
|
use axum::Router;
|
2024-01-28 22:22:29 +01:00
|
|
|
use chrono::Utc;
|
2024-01-02 14:39:30 +01:00
|
|
|
use serde::Deserialize;
|
2024-01-28 22:22:29 +01:00
|
|
|
use sqlx::PgPool;
|
|
|
|
|
use uuid::Uuid;
|
2024-01-02 14:39:30 +01:00
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
struct FormData {
|
|
|
|
|
email: String,
|
|
|
|
|
name: String,
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-28 22:22:29 +01:00
|
|
|
async fn subscribe(State(pool): State<PgPool>, Form(form): Form<FormData>) {
|
|
|
|
|
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;
|
2024-01-02 14:39:30 +01:00
|
|
|
}
|
2023-12-30 22:21:57 +01:00
|
|
|
|
2024-01-28 22:22:29 +01:00
|
|
|
pub fn routes_subscriptions(pool: PgPool) -> Router {
|
|
|
|
|
Router::new()
|
|
|
|
|
.route("/subscriptions", post(subscribe))
|
|
|
|
|
.with_state(pool)
|
2023-12-30 22:21:57 +01:00
|
|
|
}
|