diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index 27a47aa..df34291 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -1,8 +1,25 @@ -use actix_web::web; +use actix_web::{web, Error}; +use diesel::r2d2; + +use crate::{DbPool}; 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 87760d9..66157b9 100644 --- a/src/controllers/users_controller.rs +++ b/src/controllers/users_controller.rs @@ -7,17 +7,12 @@ use crate::{ user::{UserComponent, UserComponentProps}, users::{UsersComponent, UsersComponentProps}, }, - DbPool, + DbPool, controllers::run_query, }; #[get("")] async fn index(pool: web::Data) -> Result { - let users = web::block(move || { - let conn = pool.get()?; - User::all(&conn) - }) - .await? - .map_err(actix_web::error::ErrorInternalServerError)?; + let users = run_query(|conn| User::all(conn), &pool).await?; let renderer = ServerRenderer::::with_props(UsersComponentProps { users: users.clone(), @@ -32,12 +27,7 @@ async fn create(pool: web::Data) -> Result { }); let u = user.clone(); - web::block(move || { - let conn = pool.get()?; - u.insert(&conn) - }) - .await? - .map_err(actix_web::error::ErrorInternalServerError)?; + run_query(|conn| u.insert(conn), &pool).await?; let renderer = ServerRenderer::::with_props(UserComponentProps { user: user.clone() }); diff --git a/src/models/mod.rs b/src/models/mod.rs index 53afe73..018ff2e 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,3 +1 @@ -pub(self) type DbError = Box; - -pub mod user; +pub mod user; \ No newline at end of file diff --git a/src/models/user.rs b/src/models/user.rs index 42dcd6e..b9bcd12 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -1,11 +1,9 @@ -use diesel::prelude::*; +use diesel::{prelude::*, result::QueryResult}; use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::schema::users; -use super::DbError; - #[derive(Debug, Clone, Serialize, Deserialize, Queryable, Insertable, AsChangeset, PartialEq)] pub struct User { pub id: String, @@ -25,40 +23,33 @@ impl User { } } - pub fn all(conn: &SqliteConnection) -> Result, DbError> { - users::table.load::(conn).map_err(Into::into) + pub fn all(conn: &SqliteConnection) -> QueryResult> { + users::table.load(conn) } - pub fn find_by_id(conn: &SqliteConnection, id: String) -> Result { + pub fn find_by_id(conn: &SqliteConnection, id: String) -> QueryResult { users::table .filter(users::id.eq(id)) - .first::(conn) - .map_err(Into::into) + .first(conn) } - pub fn insert(&self, conn: &SqliteConnection) -> Result<(), DbError> { + pub fn insert(&self, conn: &SqliteConnection) -> QueryResult { use crate::schema::users::dsl::*; - diesel::insert_into(users).values(self).execute(conn)?; - - Ok(()) + diesel::insert_into(users).values(self).execute(conn) } - pub fn update(&self, conn: &SqliteConnection) -> Result<(), DbError> { + pub fn update(&self, conn: &SqliteConnection) -> QueryResult { use crate::schema::users::dsl::*; diesel::update(users.find(self.id.clone())) .set(self) - .execute(conn)?; - - Ok(()) + .execute(conn) } - pub fn delete(&self, conn: &SqliteConnection) -> Result<(), DbError> { + pub fn delete(&self, conn: &SqliteConnection) -> QueryResult { use crate::schema::users::dsl::*; - diesel::delete(users.filter(id.eq(self.id.clone()))).execute(conn)?; - - Ok(()) + diesel::delete(users.filter(id.eq(self.id.clone()))).execute(conn) } }