-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding DB conncetion to subscription
Signed-off-by: Anthony M. Bonafide <AnthonyMBonafide@gmail.com>
- Loading branch information
1 parent
ae2b978
commit 6b4e97a
Showing
6 changed files
with
53 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
use std::net::TcpListener; | ||
|
||
use sqlx::{Connection, PgConnection}; | ||
use zero2prod::{configuration::get_configuration, run}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), std::io::Error> { | ||
let config = get_configuration().expect("Failed to read configuration"); | ||
let address = format!("127.0.0.1:{}", config.application_port); | ||
let config = get_configuration().expect("Failed to get configuration"); | ||
let connection = PgConnection::connect(&config.database.connection_string()) | ||
.await | ||
.expect("Failed to connect to database"); | ||
|
||
let listener = TcpListener::bind(address)?; | ||
run(listener)?.await | ||
run(listener, connection)?.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
use actix_web::{web, HttpResponse}; | ||
use chrono::Utc; | ||
use sqlx::PgConnection; | ||
use uuid::Uuid; | ||
|
||
#[derive(serde::Deserialize, serde::Serialize)] | ||
pub struct FormData { | ||
name: String, | ||
email: String, | ||
} | ||
|
||
pub async fn subscribe(_form: web::Form<FormData>) -> HttpResponse { | ||
pub async fn subscribe( | ||
form: web::Form<FormData>, | ||
connection: web::Data<PgConnection>, | ||
) -> 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(connection.get_ref()) | ||
.await; | ||
HttpResponse::Ok().finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters