Abstracted away getting a PgConnection with expect().
This commit is contained in:
parent
217e5ee64b
commit
3509a70a6a
@ -1,7 +1,7 @@
|
|||||||
use actix_web::{get, web, Responder, http::header::ContentType, HttpResponse, post};
|
use actix_web::{get, web, Responder, http::header::ContentType, HttpResponse, post};
|
||||||
use chrono::{DateTime, ParseError};
|
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;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ impl Into<Uuid> for GamenightGet {
|
|||||||
|
|
||||||
#[get("/gamenights")]
|
#[get("/gamenights")]
|
||||||
pub async fn gamenights(pool: web::Data<DbPool>, _user: User) -> Result<impl Responder, ApiError> {
|
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)?;
|
let gamenights = schema::gamenights(&mut conn)?;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
@ -35,7 +35,7 @@ pub async fn gamenights(pool: web::Data<DbPool>, _user: User) -> Result<impl Res
|
|||||||
|
|
||||||
#[post("/gamenight")]
|
#[post("/gamenight")]
|
||||||
pub async fn gamenight_post(pool: web::Data<DbPool>, user: User, gamenight_data: web::Json<GamenightPost>) -> Result<impl Responder, ApiError> {
|
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)?)?;
|
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")]
|
#[get("/gamenight")]
|
||||||
pub async fn gamenight_get(pool: web::Data<DbPool>, _user: User, gamenight_data: web::Json<GamenightGet>) -> Result<impl Responder, ApiError> {
|
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())?;
|
let gamenight = schema::gamenight::get_gamenight(&mut conn, gamenight_data.into_inner().into())?;
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ mod user_handlers;
|
|||||||
mod gamenight_handlers;
|
mod gamenight_handlers;
|
||||||
mod error;
|
mod error;
|
||||||
mod authorization;
|
mod authorization;
|
||||||
|
mod util;
|
||||||
|
|
||||||
pub use user_handlers::login;
|
pub use user_handlers::login;
|
||||||
pub use user_handlers::register;
|
pub use user_handlers::register;
|
||||||
|
@ -7,6 +7,7 @@ use crate::request::requests::{Login, Register};
|
|||||||
use crate::request::error::ApiError;
|
use crate::request::error::ApiError;
|
||||||
use crate::request::responses::LoginResponse;
|
use crate::request::responses::LoginResponse;
|
||||||
use crate::request::authorization::get_token;
|
use crate::request::authorization::get_token;
|
||||||
|
use crate::request::util::GetPgConnection;
|
||||||
use crate::schema::{self};
|
use crate::schema::{self};
|
||||||
use serde_json;
|
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 data = login_data.into_inner();
|
||||||
|
|
||||||
let response = if let Some(user) = web::block(move || {
|
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())
|
schema::login(&mut conn, data.into())
|
||||||
})
|
})
|
||||||
.await??
|
.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> {
|
pub async fn register(pool: web::Data<DbPool>, register_data: web::Json<Register>) -> Result<impl Responder, ApiError> {
|
||||||
let data1 = register_data.clone();
|
let data1 = register_data.clone();
|
||||||
let data2 = register_data.clone();
|
let data2 = register_data.clone();
|
||||||
|
let register_request = data2.into();
|
||||||
let register_request : schema::user::Register = data2.into();
|
let mut conn1 = pool.get_conn();
|
||||||
let mut conn1 = pool.get().expect("couldn't get db connection from pool");
|
let mut conn2 = pool.get_conn();
|
||||||
let mut conn2 = pool.get().expect("couldn't get db connection from pool");
|
|
||||||
|
|
||||||
|
|
||||||
let _validation_result = web::block(move || {
|
let _validation_result = web::block(move || {
|
||||||
data1.validate_args((&mut conn1, &mut conn2))
|
data1.validate_args((&mut conn1, &mut conn2))
|
||||||
}).await??;
|
}).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 || {
|
let _register_result = web::block(move || {
|
||||||
schema::register(&mut conn3, register_request)
|
schema::register(&mut conn3, register_request)
|
||||||
}).await??;
|
}).await??;
|
||||||
|
14
backend-actix/src/request/util.rs
Normal file
14
backend-actix/src/request/util.rs
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user