From 70ae15f655558d24894fc04d76e48a5eacc4b9de Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Thu, 30 Mar 2023 09:32:24 +0200 Subject: [PATCH] Fixes the ugly Register User post handler. --- backend-actix/Cargo.lock | 53 +++++++++---------- backend-actix/src/main.rs | 2 +- .../src/request/gamenight_handlers.rs | 12 +++-- backend-actix/src/request/mod.rs | 1 - backend-actix/src/request/requests.rs | 7 ++- backend-actix/src/request/user_handlers.rs | 23 +++----- backend-actix/src/request/util.rs | 14 ----- backend-actix/src/schema/user.rs | 15 ++++-- backend-actix/src/util/mod.rs | 11 ++++ 9 files changed, 70 insertions(+), 68 deletions(-) delete mode 100644 backend-actix/src/request/util.rs create mode 100644 backend-actix/src/util/mod.rs diff --git a/backend-actix/Cargo.lock b/backend-actix/Cargo.lock index aed21e9..e4189b1 100644 --- a/backend-actix/Cargo.lock +++ b/backend-actix/Cargo.lock @@ -462,9 +462,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9c00419335c41018365ddf7e4d5f1c12ee3659ddcf3e01974650ba1de73d038" +checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93" dependencies = [ "cc", "cxxbridge-flags", @@ -474,9 +474,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb8307ad413a98fff033c8545ecf133e3257747b3bae935e7602aab8aa92d4ca" +checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b" dependencies = [ "cc", "codespan-reporting", @@ -484,24 +484,24 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.10", + "syn 2.0.11", ] [[package]] name = "cxxbridge-flags" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edc52e2eb08915cb12596d29d55f0b5384f00d697a646dbd269b6ecb0fbd9d31" +checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb" [[package]] name = "cxxbridge-macro" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "631569015d0d8d54e6c241733f944042623ab6df7bc3be7466874b05fcdb1c5f" +checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.10", + "syn 2.0.11", ] [[package]] @@ -634,9 +634,9 @@ dependencies = [ [[package]] name = "generic-array" -version = "0.14.6" +version = "0.14.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", @@ -1078,9 +1078,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.53" +version = "1.0.54" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba466839c78239c09faf015484e5cc04860f88242cff4d03eb038f04b4699b73" +checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534" dependencies = [ "unicode-ident", ] @@ -1220,29 +1220,29 @@ checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" [[package]] name = "serde" -version = "1.0.158" +version = "1.0.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "771d4d9c4163ee138805e12c710dd365e4f44be8be0503cb1bb9eb989425d9c9" +checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.158" +version = "1.0.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e801c1712f48475582b7696ac71e0ca34ebb30e09338425384269d9717c62cad" +checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585" dependencies = [ "proc-macro2", "quote", - "syn 2.0.10", + "syn 2.0.11", ] [[package]] name = "serde_json" -version = "1.0.94" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea" +checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" dependencies = [ "itoa", "ryu", @@ -1343,9 +1343,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.10" +version = "2.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aad1363ed6d37b84299588d62d3a7d95b5a5c2d9aad5c85609fda12afaa1f40" +checksum = "21e3787bb71465627110e7d87ed4faaa36c1f61042ee67badb9e2ef173accc40" dependencies = [ "proc-macro2", "quote", @@ -1378,7 +1378,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.10", + "syn 2.0.11", ] [[package]] @@ -1436,14 +1436,13 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.26.0" +version = "1.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03201d01c3c27a29c8a5cee5b55a93ddae1ccf6f08f65365c2c918f8c1b76f64" +checksum = "d0de47a4eecbe11f498978a9b29d792f0d2692d1dd003650c24c76510e3bc001" dependencies = [ "autocfg", "bytes", "libc", - "memchr", "mio", "parking_lot", "pin-project-lite", diff --git a/backend-actix/src/main.rs b/backend-actix/src/main.rs index cf5d071..96bc3ad 100644 --- a/backend-actix/src/main.rs +++ b/backend-actix/src/main.rs @@ -1,5 +1,6 @@ pub mod request; pub mod schema; +pub mod util; use actix_web::HttpServer; use actix_web::App; @@ -13,7 +14,6 @@ pub(crate) type DbPool = Pool>; #[actix_web::main] async fn main() -> std::io::Result<()> { - let url = "postgres://root:root@localhost/gamenight"; let manager = ConnectionManager::::new(url); // Refer to the `r2d2` documentation for more methods to use diff --git a/backend-actix/src/request/gamenight_handlers.rs b/backend-actix/src/request/gamenight_handlers.rs index 1ce10a7..dfc9600 100644 --- a/backend-actix/src/request/gamenight_handlers.rs +++ b/backend-actix/src/request/gamenight_handlers.rs @@ -1,10 +1,16 @@ 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}, util::GetPgConnection}, schema::{self, user::User}}; - use uuid::Uuid; +use crate::schema::{self}; +use crate::schema::user::User; +use crate::request::requests::GamenightGet; +use crate::request::requests::GamenightPost; +use crate::request::responses::GameNightResponse; +use crate::request::error::ApiError; +use crate::DbPool; +use crate::util::GetConnection; + impl GamenightPost { pub fn into_with_user(&self, user: User) -> Result { return Ok(schema::gamenight::Gamenight { diff --git a/backend-actix/src/request/mod.rs b/backend-actix/src/request/mod.rs index 34329bb..dd975f3 100644 --- a/backend-actix/src/request/mod.rs +++ b/backend-actix/src/request/mod.rs @@ -5,7 +5,6 @@ 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/requests.rs b/backend-actix/src/request/requests.rs index 06911cf..0143bbc 100644 --- a/backend-actix/src/request/requests.rs +++ b/backend-actix/src/request/requests.rs @@ -1,5 +1,8 @@ use serde::{Serialize, Deserialize}; use validator::Validate; +use diesel::PgConnection; +use diesel::r2d2::ConnectionManager; +use diesel::r2d2::Pool; use crate::schema::user::{unique_email, unique_username}; @@ -13,12 +16,12 @@ pub struct Login { pub struct Register { #[validate( length(min = 1), - custom(function = "unique_username", arg = "&'v_a mut diesel::PgConnection") + custom(function = "unique_username", arg = "&'v_a Pool>") )] pub username: String, #[validate( email, - custom(function = "unique_email", arg = "&'v_a mut diesel::PgConnection") + custom(function = "unique_email", arg = "&'v_a Pool>") )] #[validate(email)] pub email: String, diff --git a/backend-actix/src/request/user_handlers.rs b/backend-actix/src/request/user_handlers.rs index fd9b0d8..50c00e0 100644 --- a/backend-actix/src/request/user_handlers.rs +++ b/backend-actix/src/request/user_handlers.rs @@ -7,7 +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::util::GetConnection; use crate::schema::{self}; use serde_json; @@ -30,8 +30,6 @@ impl Into for Register { } } - - #[get("/token")] pub async fn login(pool: web::Data, login_data: web::Json) -> Result { let data = login_data.into_inner(); @@ -57,19 +55,12 @@ pub async fn login(pool: web::Data, login_data: web::Json) -> Res #[post("/user")] 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 = 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_conn(); - let _register_result = web::block(move || { - schema::register(&mut conn3, register_request) + web::block(move || -> Result<(), ApiError> { + register_data.validate_args((&pool, &pool))?; + let register_request = register_data.into_inner().into(); + let mut conn = pool.get_conn(); + schema::register(&mut conn, register_request)?; + Ok(()) }).await??; return Ok(HttpResponse::Ok()) diff --git a/backend-actix/src/request/util.rs b/backend-actix/src/request/util.rs deleted file mode 100644 index 0f998fe..0000000 --- a/backend-actix/src/request/util.rs +++ /dev/null @@ -1,14 +0,0 @@ -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 diff --git a/backend-actix/src/schema/user.rs b/backend-actix/src/schema/user.rs index c015ed3..0bef727 100644 --- a/backend-actix/src/schema/user.rs +++ b/backend-actix/src/schema/user.rs @@ -11,6 +11,9 @@ use argon2::{ Argon2, }; use validator::ValidationError; +use crate::DbPool; +use crate::util::GetConnection; + use super::schema::{pwd, users}; pub use super::error::DatabaseError; @@ -89,11 +92,13 @@ pub fn get_user(conn: &mut PgConnection, id: Uuid) -> Result Result<(), ValidationError> { +pub fn unique_username(username: &String, pool: &DbPool) -> Result<(), ValidationError> { + let mut conn = pool.get().expect("Couldn't get db connection from pool"); + match users::table .count() .filter(users::username.eq(username)) - .get_result(conn) + .get_result(&mut conn) { Ok(0) => Ok(()), Ok(_) => Err(ValidationError::new("User already exists")), @@ -101,11 +106,13 @@ pub fn unique_username(username: &String, conn: &mut PgConnection) -> Result<(), } } -pub fn unique_email(email: &String, conn: &mut PgConnection) -> Result<(), ValidationError> { +pub fn unique_email(email: &String, pool: &DbPool) -> Result<(), ValidationError> { + let mut conn = pool.get_conn(); + match users::table .count() .filter(users::email.eq(email)) - .get_result(conn) + .get_result(&mut conn) { Ok(0) => Ok(()), Ok(_) => Err(ValidationError::new("email already exists")), diff --git a/backend-actix/src/util/mod.rs b/backend-actix/src/util/mod.rs new file mode 100644 index 0000000..7d29e7f --- /dev/null +++ b/backend-actix/src/util/mod.rs @@ -0,0 +1,11 @@ +use diesel::{r2d2::{PooledConnection, ManageConnection, Pool}}; + +pub trait GetConnection where T: ManageConnection { + fn get_conn(&self) -> PooledConnection; +} + +impl GetConnection for Pool { + fn get_conn(&self) -> PooledConnection { + self.get().expect("Couldn't get db connection from pool") + } +} \ No newline at end of file