mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-09-27 13:50:03 +00:00
56 lines
1.2 KiB
Rust
56 lines
1.2 KiB
Rust
|
use actix_web::{web, HttpResponse};
|
||
|
use chrono::Utc;
|
||
|
use sqlx::PgPool;
|
||
|
use uuid::Uuid;
|
||
|
|
||
|
#[derive(serde::Deserialize)]
|
||
|
pub struct SubscribeRequest {
|
||
|
email: String,
|
||
|
name: String,
|
||
|
}
|
||
|
|
||
|
#[tracing::instrument(
|
||
|
name = "Adding a new subscriber",
|
||
|
skip(payload, pool),
|
||
|
fields(
|
||
|
email = %payload.email,
|
||
|
name = %payload.name
|
||
|
)
|
||
|
)]
|
||
|
pub async fn subscribe(
|
||
|
payload: web::Form<SubscribeRequest>,
|
||
|
pool: web::Data<PgPool>,
|
||
|
) -> Result<HttpResponse, HttpResponse> {
|
||
|
insert_subscriber(&pool, &payload)
|
||
|
.await
|
||
|
.map_err(|_| HttpResponse::InternalServerError().finish())?;
|
||
|
Ok(HttpResponse::Ok().finish())
|
||
|
}
|
||
|
|
||
|
#[tracing::instrument(
|
||
|
name = "Saving new subscriber details in the database",
|
||
|
skip(payload, pool)
|
||
|
)]
|
||
|
pub async fn insert_subscriber(
|
||
|
pool: &PgPool,
|
||
|
payload: &SubscribeRequest,
|
||
|
) -> Result<(), sqlx::Error> {
|
||
|
sqlx::query!(
|
||
|
r#"
|
||
|
INSERT INTO subscriptions (id, email, name, subscribed_at)
|
||
|
VALUES ($1, $2, $3, $4)
|
||
|
"#,
|
||
|
Uuid::new_v4(),
|
||
|
payload.email,
|
||
|
payload.name,
|
||
|
Utc::now()
|
||
|
)
|
||
|
.execute(pool)
|
||
|
.await
|
||
|
.map_err(|e| {
|
||
|
tracing::error!("Failed to execute query: {:?}", e);
|
||
|
e
|
||
|
})?;
|
||
|
Ok(())
|
||
|
}
|