Refactor run_query
This commit is contained in:
parent
65e20c2fef
commit
4ddc96e983
5 changed files with 46 additions and 36 deletions
|
@ -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<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
pub(self) async fn run_query<T, F>(query: F, pool: &web::Data<DbPool>) -> Result<T, Error>
|
||||
where
|
||||
F: Fn(&diesel::SqliteConnection) -> diesel::QueryResult<T>
|
||||
{
|
||||
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);
|
||||
|
|
|
@ -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<DbPool>) -> Result<HttpResponse, Error> {
|
|||
|
||||
#[get("create")]
|
||||
async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||
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::<UserComponent>::with_props(UserComponentProps { user: user.clone() });
|
||||
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user });
|
||||
Ok(HttpResponse::Ok().body(renderer.render().await))
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,6 @@ pub(self) mod models;
|
|||
pub(self) mod schema;
|
||||
pub(self) mod views;
|
||||
|
||||
pub(self) type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||
|
||||
use actix_web::{middleware, web, App, HttpServer};
|
||||
use diesel::{
|
||||
r2d2::{self, ConnectionManager},
|
||||
|
|
|
@ -1 +1,18 @@
|
|||
pub mod user;
|
||||
use actix_web::{web, Error};
|
||||
use diesel::{r2d2::{self, ConnectionManager}, SqliteConnection};
|
||||
|
||||
pub mod user;
|
||||
|
||||
pub(crate) type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||
|
||||
pub(crate) async fn run_query<T, F>(query: F, pool: &web::Data<DbPool>) -> Result<T, Error>
|
||||
where
|
||||
F: Fn(&diesel::SqliteConnection) -> diesel::QueryResult<T> + 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)
|
||||
}
|
|
@ -33,23 +33,29 @@ impl User {
|
|||
.first(conn)
|
||||
}
|
||||
|
||||
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<usize> {
|
||||
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<Self> {
|
||||
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<usize> {
|
||||
pub fn update(&self, conn: &SqliteConnection) -> QueryResult<Self> {
|
||||
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<usize> {
|
||||
pub fn delete(&self, conn: &SqliteConnection) -> QueryResult<Self> {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue