zero2prod/src/routes/subscibtions.rs
2024-03-23 18:13:54 +02:00

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
}
}
}