From 3509a70a6abbe6baff649e11324f4a216d1e8772 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sun, 26 Mar 2023 11:14:05 +0200 Subject: [PATCH] Abstracted away getting a PgConnection with expect(). --- backend-actix/src/request/gamenight_handlers.rs | 8 ++++---- backend-actix/src/request/mod.rs | 1 + backend-actix/src/request/user_handlers.rs | 13 ++++++------- backend-actix/src/request/util.rs | 14 ++++++++++++++ 4 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 backend-actix/src/request/util.rs diff --git a/backend-actix/src/request/gamenight_handlers.rs b/backend-actix/src/request/gamenight_handlers.rs index f283487..1ce10a7 100644 --- a/backend-actix/src/request/gamenight_handlers.rs +++ b/backend-actix/src/request/gamenight_handlers.rs @@ -1,7 +1,7 @@ use actix_web::{get, web, Responder, http::header::ContentType, HttpResponse, post}; use chrono::{DateTime, ParseError}; -use crate::{DbPool, request::{error::ApiError, responses::GameNightResponse, requests::{GamenightPost, GamenightGet}}, schema::{self, user::User}}; +use crate::{DbPool, request::{error::ApiError, responses::GameNightResponse, requests::{GamenightPost, GamenightGet}, util::GetPgConnection}, schema::{self, user::User}}; use uuid::Uuid; @@ -24,7 +24,7 @@ impl Into for GamenightGet { #[get("/gamenights")] pub async fn gamenights(pool: web::Data, _user: User) -> Result { - let mut conn = pool.get().expect("couldn't get db connection from pool"); + let mut conn = pool.get_conn(); let gamenights = schema::gamenights(&mut conn)?; Ok(HttpResponse::Ok() @@ -35,7 +35,7 @@ pub async fn gamenights(pool: web::Data, _user: User) -> Result, user: User, gamenight_data: web::Json) -> Result { - let mut conn = pool.get().expect("couldn't get db connection from pool"); + let mut conn = pool.get_conn(); schema::gamenight::add_gamenight(&mut conn, gamenight_data.into_with_user(user)?)?; @@ -44,7 +44,7 @@ pub async fn gamenight_post(pool: web::Data, user: User, gamenight_data: #[get("/gamenight")] pub async fn gamenight_get(pool: web::Data, _user: User, gamenight_data: web::Json) -> Result { - let mut conn = pool.get().expect("couldn't get db connection from pool"); + let mut conn = pool.get_conn(); let gamenight = schema::gamenight::get_gamenight(&mut conn, gamenight_data.into_inner().into())?; diff --git a/backend-actix/src/request/mod.rs b/backend-actix/src/request/mod.rs index dd975f3..34329bb 100644 --- a/backend-actix/src/request/mod.rs +++ b/backend-actix/src/request/mod.rs @@ -5,6 +5,7 @@ mod user_handlers; mod gamenight_handlers; mod error; mod authorization; +mod util; pub use user_handlers::login; pub use user_handlers::register; diff --git a/backend-actix/src/request/user_handlers.rs b/backend-actix/src/request/user_handlers.rs index faede52..fd9b0d8 100644 --- a/backend-actix/src/request/user_handlers.rs +++ b/backend-actix/src/request/user_handlers.rs @@ -7,6 +7,7 @@ use crate::request::requests::{Login, Register}; use crate::request::error::ApiError; use crate::request::responses::LoginResponse; use crate::request::authorization::get_token; +use crate::request::util::GetPgConnection; use crate::schema::{self}; use serde_json; @@ -36,7 +37,7 @@ pub async fn login(pool: web::Data, login_data: web::Json) -> Res let data = login_data.into_inner(); let response = if let Some(user) = web::block(move || { - let mut conn = pool.get().expect("couldn't get db connection from pool"); + let mut conn = pool.get_conn(); schema::login(&mut conn, data.into()) }) .await?? @@ -58,17 +59,15 @@ pub async fn login(pool: web::Data, login_data: web::Json) -> Res pub async fn register(pool: web::Data, register_data: web::Json) -> Result { let data1 = register_data.clone(); let data2 = register_data.clone(); - - let register_request : schema::user::Register = data2.into(); - let mut conn1 = pool.get().expect("couldn't get db connection from pool"); - let mut conn2 = pool.get().expect("couldn't get db connection from pool"); - + let register_request = data2.into(); + let mut conn1 = pool.get_conn(); + let mut conn2 = pool.get_conn(); let _validation_result = web::block(move || { data1.validate_args((&mut conn1, &mut conn2)) }).await??; - let mut conn3 = pool.get().expect("couldn't get db connection from pool"); + let mut conn3 = pool.get_conn(); let _register_result = web::block(move || { schema::register(&mut conn3, register_request) }).await??; diff --git a/backend-actix/src/request/util.rs b/backend-actix/src/request/util.rs new file mode 100644 index 0000000..0f998fe --- /dev/null +++ b/backend-actix/src/request/util.rs @@ -0,0 +1,14 @@ +use actix_web::web; +use diesel::{r2d2::{PooledConnection, ConnectionManager}, PgConnection}; + +use crate::DbPool; + +pub trait GetPgConnection { + fn get_conn(&self) -> PooledConnection>; +} + +impl GetPgConnection for web::Data { + fn get_conn(&self) -> PooledConnection> { + self.get().expect("couldn't get db connection from pool") + } +} \ No newline at end of file