From 4ddc96e98379e401577474f390ec5ae4af9d0298 Mon Sep 17 00:00:00 2001 From: "Florian RICHER (MrDev023)" Date: Tue, 22 Mar 2022 21:21:31 +0100 Subject: [PATCH] Refactor run_query --- src/controllers/mod.rs | 19 +------------------ src/controllers/users_controller.rs | 24 +++++++++++++++--------- src/main.rs | 2 -- src/models/mod.rs | 19 ++++++++++++++++++- src/models/user.rs | 18 ++++++++++++------ 5 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index df34291..27a47aa 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -1,25 +1,8 @@ -use actix_web::{web, Error}; -use diesel::r2d2; - -use crate::{DbPool}; +use actix_web::web; mod hello_controller; mod users_controller; -type DbError = Box; - -pub(self) async fn run_query(query: F, pool: &web::Data) -> Result -where - F: Fn(&diesel::SqliteConnection) -> diesel::QueryResult -{ - web::block(move || { - let conn = pool.get()?; - query(&conn) - }) - .await? - .map_err(actix_web::error::ErrorInternalServerError) -} - pub fn init_routes(cfg: &mut web::ServiceConfig) { cfg.configure(hello_controller::init_routes); cfg.configure(users_controller::init_routes); diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs index 66157b9..111e2d6 100644 --- a/src/controllers/users_controller.rs +++ b/src/controllers/users_controller.rs @@ -2,12 +2,18 @@ use actix_web::{get, web, Error, HttpResponse}; use yew::ServerRenderer; use crate::{ - models::user::{NewUser, User}, + models::{ + user::{ + NewUser, + User, + }, + run_query, + DbPool + }, views::components::{ user::{UserComponent, UserComponentProps}, users::{UsersComponent, UsersComponentProps}, }, - DbPool, controllers::run_query, }; #[get("")] @@ -22,15 +28,15 @@ async fn index(pool: web::Data) -> Result { #[get("create")] async fn create(pool: web::Data) -> Result { - let user = User::new(NewUser { - name: "John".to_string(), - }); - - let u = user.clone(); - run_query(|conn| u.insert(conn), &pool).await?; + let user = run_query(|conn| { + let user = User::new(NewUser { + name: "John".to_string(), + }); + user.insert(conn) + }, &pool).await?; let renderer = - ServerRenderer::::with_props(UserComponentProps { user: user.clone() }); + ServerRenderer::::with_props(UserComponentProps { user: user }); Ok(HttpResponse::Ok().body(renderer.render().await)) } diff --git a/src/main.rs b/src/main.rs index e30be12..4bad5e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,6 @@ pub(self) mod models; pub(self) mod schema; pub(self) mod views; -pub(self) type DbPool = r2d2::Pool>; - use actix_web::{middleware, web, App, HttpServer}; use diesel::{ r2d2::{self, ConnectionManager}, diff --git a/src/models/mod.rs b/src/models/mod.rs index 018ff2e..0319b98 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1 +1,18 @@ -pub mod user; \ No newline at end of file +use actix_web::{web, Error}; +use diesel::{r2d2::{self, ConnectionManager}, SqliteConnection}; + +pub mod user; + +pub(crate) type DbPool = r2d2::Pool>; + +pub(crate) async fn run_query(query: F, pool: &web::Data) -> Result +where + F: Fn(&diesel::SqliteConnection) -> diesel::QueryResult + Send + 'static, + T: Send + 'static +{ + let conn = pool.get() + .map_err(actix_web::error::ErrorInternalServerError)?; + web::block(move || query(&conn) ) + .await? + .map_err(actix_web::error::ErrorInternalServerError) +} \ No newline at end of file diff --git a/src/models/user.rs b/src/models/user.rs index b9bcd12..f10fd83 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -33,23 +33,29 @@ impl User { .first(conn) } - pub fn insert(&self, conn: &SqliteConnection) -> QueryResult { + pub fn insert(&self, conn: &SqliteConnection) -> QueryResult { use crate::schema::users::dsl::*; - diesel::insert_into(users).values(self).execute(conn) + diesel::insert_into(users).values(self).execute(conn)?; + + Ok(self.clone()) } - pub fn update(&self, conn: &SqliteConnection) -> QueryResult { + pub fn update(&self, conn: &SqliteConnection) -> QueryResult { use crate::schema::users::dsl::*; diesel::update(users.find(self.id.clone())) .set(self) - .execute(conn) + .execute(conn)?; + + Ok(self.clone()) } - pub fn delete(&self, conn: &SqliteConnection) -> QueryResult { + pub fn delete(&self, conn: &SqliteConnection) -> QueryResult { use crate::schema::users::dsl::*; - diesel::delete(users.filter(id.eq(self.id.clone()))).execute(conn) + diesel::delete(users.filter(id.eq(self.id.clone()))).execute(conn)?; + + Ok(self.clone()) } }