Database connection and user registration
This commit is contained in:
37
src/routes/subscriptions.rs
Normal file
37
src/routes/subscriptions.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use axum::{Form, extract::State, http::StatusCode, response::IntoResponse};
|
||||
use chrono::Utc;
|
||||
use serde::Deserialize;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn subscribe(
|
||||
State(connection): State<PgPool>,
|
||||
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(&connection)
|
||||
.await
|
||||
{
|
||||
Ok(_) => StatusCode::OK,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to execute query: {}", e);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[allow(dead_code)]
|
||||
pub struct FormData {
|
||||
name: String,
|
||||
email: String,
|
||||
}
|
||||
Reference in New Issue
Block a user