Abstracted away getting a PgConnection with expect().

This commit is contained in:
Dennis Brentjes 2023-03-26 11:14:05 +02:00
parent 217e5ee64b
commit 3509a70a6a
4 changed files with 25 additions and 11 deletions

View File

@ -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<Uuid> for GamenightGet {
#[get("/gamenights")]
pub async fn gamenights(pool: web::Data<DbPool>, _user: User) -> Result<impl Responder, ApiError> {
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<DbPool>, _user: User) -> Result<impl Res
#[post("/gamenight")]
pub async fn gamenight_post(pool: web::Data<DbPool>, user: User, gamenight_data: web::Json<GamenightPost>) -> Result<impl Responder, ApiError> {
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<DbPool>, user: User, gamenight_data:
#[get("/gamenight")]
pub async fn gamenight_get(pool: web::Data<DbPool>, _user: User, gamenight_data: web::Json<GamenightGet>) -> Result<impl Responder, ApiError> {
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())?;

View File

@ -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;

View File

@ -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<DbPool>, login_data: web::Json<Login>) -> 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<DbPool>, login_data: web::Json<Login>) -> Res
pub async fn register(pool: web::Data<DbPool>, register_data: web::Json<Register>) -> Result<impl Responder, ApiError> {
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??;

View File

@ -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<ConnectionManager<PgConnection>>;
}
impl GetPgConnection for web::Data<DbPool> {
fn get_conn(&self) -> PooledConnection<ConnectionManager<PgConnection>> {
self.get().expect("couldn't get db connection from pool")
}
}