mirror of
https://github.com/kristoferssolo/zero2prod.git
synced 2025-10-21 20:10:40 +00:00
37 lines
798 B
Rust
37 lines
798 B
Rust
use axum::{extract::State, http::StatusCode, response::IntoResponse, Form};
|
|
use chrono::Utc;
|
|
use serde::Deserialize;
|
|
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct FormData {
|
|
name: String,
|
|
email: String,
|
|
}
|
|
|
|
pub async fn subscribe(
|
|
State(conn): State<PgPool>,
|
|
Form(form): Form<FormData>,
|
|
) -> impl IntoResponse {
|
|
match 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()
|
|
)
|
|
.execute(&conn)
|
|
.await
|
|
{
|
|
Ok(_) => StatusCode::OK,
|
|
Err(e) => {
|
|
println!("Failed to execute query: {:?}", e);
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
}
|
|
}
|
|
}
|