mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-05-18 16:28:08 +00:00
34 lines
741 B
Rust
34 lines
741 B
Rust
use actix_web::{web, HttpResponse};
|
|
use chrono::Utc;
|
|
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
#[derive(serde::Deserialize)]
|
|
pub struct FormData {
|
|
email: String,
|
|
name: String,
|
|
}
|
|
|
|
pub async fn subscribe(
|
|
form: web::Form<FormData>,
|
|
pool: web::Data<PgPool>,
|
|
) -> Result<HttpResponse, HttpResponse> {
|
|
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(pool.as_ref())
|
|
.await
|
|
.map_err(|e| {
|
|
println!("Failed to execute query: {}", e);
|
|
HttpResponse::InternalServerError().finish()
|
|
})?;
|
|
Ok(HttpResponse::Ok().finish())
|
|
}
|