Implement changes to allow database connection for insertions
Also updated tests to reflect the new PgPool format being used. Tests now create a mock database on creation to determine if it works before removing it. Up to Chapter 4.
This commit is contained in:
@@ -31,4 +31,11 @@ impl DatabaseSettings {
|
||||
self.username, self.password, self.host, self.port, self.database_name
|
||||
)
|
||||
}
|
||||
|
||||
pub fn connection_string_without_db(&self) -> String {
|
||||
format!(
|
||||
"postgres://{}:{}@{}:{}",
|
||||
self.username, self.password, self.host, self.port
|
||||
)
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -1,14 +1,20 @@
|
||||
use mail_app::startup::run;
|
||||
use mail_app::configuration::get_configuration;
|
||||
use std::net::TcpListener;
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
// Attempt to read from config
|
||||
let configuration = get_configuration().expect("Failed to read configuration data.");
|
||||
|
||||
// Configure connection to database for our startup
|
||||
let connection_pool = PgPool::connect(&configuration.database.connection_string())
|
||||
.await
|
||||
.expect("Failed to connect to Postgres.");
|
||||
|
||||
// Take port from settings file
|
||||
let address = format!("127.0.0.1:{}", configuration.application_port);
|
||||
let listener = TcpListener::bind(address)?;
|
||||
run(listener)?.await
|
||||
run(listener, connection_pool)?.await
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
use actix_web::{web, HttpResponse};
|
||||
use sqlx::PgPool;
|
||||
use chrono::Utc;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct FormData {
|
||||
@@ -6,6 +9,26 @@ pub struct FormData {
|
||||
name: String
|
||||
}
|
||||
|
||||
pub async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
pub async fn subscribe(form: web::Form<FormData>, pool: web::Data<PgPool>,) -> HttpResponse {
|
||||
// _connect uses PgPool from the application state as defined in startup.rs
|
||||
// Use a match statement for the query as it only returns two outcomes
|
||||
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(pool.as_ref())
|
||||
.await
|
||||
{
|
||||
Ok(_) => HttpResponse::Ok().finish(),
|
||||
Err(e) => {
|
||||
println!("Failed to execute query: {}", e);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -1,14 +1,17 @@
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use actix_web::dev::Server;
|
||||
use std::net::TcpListener;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::routes;
|
||||
|
||||
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
|
||||
let server = HttpServer::new(|| {
|
||||
pub fn run(listener: TcpListener, db_pool: PgPool) -> Result<Server, std::io::Error> {
|
||||
let db_pool = web::Data::new(db_pool); // Wrap connection in a smart pointer
|
||||
let server = HttpServer::new(move || {
|
||||
App::new()
|
||||
.route("/health_check", web::get().to(routes::health_check))
|
||||
.route("/subscriptions", web::post().to(routes::subscribe))
|
||||
.app_data(db_pool.clone()) // Get pointer copy and attach to application state
|
||||
})
|
||||
.listen(listener)?
|
||||
.run();
|
||||
|
||||
Reference in New Issue
Block a user