- Tests will use new database every run - Added chrono and uuid dependencies. - Updated documentation
36 lines
792 B
Rust
36 lines
792 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_route(
|
|
form: web::Form<FormData>,
|
|
db_conn_pool: web::Data<PgPool>,
|
|
) -> HttpResponse {
|
|
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(db_conn_pool.get_ref())
|
|
.await
|
|
{
|
|
Ok(_) => HttpResponse::Ok().finish(),
|
|
Err(e) => {
|
|
println!("Failed to execute query: {}", e);
|
|
HttpResponse::InternalServerError().finish()
|
|
}
|
|
}
|
|
}
|