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 actix_web::web;
|
||||||
use diesel::r2d2;
|
|
||||||
|
|
||||||
use crate::{DbPool};
|
|
||||||
|
|
||||||
mod hello_controller;
|
mod hello_controller;
|
||||||
mod users_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) {
|
pub fn init_routes(cfg: &mut web::ServiceConfig) {
|
||||||
cfg.configure(hello_controller::init_routes);
|
cfg.configure(hello_controller::init_routes);
|
||||||
cfg.configure(users_controller::init_routes);
|
cfg.configure(users_controller::init_routes);
|
||||||
|
|
|
@ -2,12 +2,18 @@ use actix_web::{get, web, Error, HttpResponse};
|
||||||
use yew::ServerRenderer;
|
use yew::ServerRenderer;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
models::user::{NewUser, User},
|
models::{
|
||||||
|
user::{
|
||||||
|
NewUser,
|
||||||
|
User,
|
||||||
|
},
|
||||||
|
run_query,
|
||||||
|
DbPool
|
||||||
|
},
|
||||||
views::components::{
|
views::components::{
|
||||||
user::{UserComponent, UserComponentProps},
|
user::{UserComponent, UserComponentProps},
|
||||||
users::{UsersComponent, UsersComponentProps},
|
users::{UsersComponent, UsersComponentProps},
|
||||||
},
|
},
|
||||||
DbPool, controllers::run_query,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[get("")]
|
#[get("")]
|
||||||
|
@ -22,15 +28,15 @@ async fn index(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||||
|
|
||||||
#[get("create")]
|
#[get("create")]
|
||||||
async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||||
let user = User::new(NewUser {
|
let user = run_query(|conn| {
|
||||||
name: "John".to_string(),
|
let user = User::new(NewUser {
|
||||||
});
|
name: "John".to_string(),
|
||||||
|
});
|
||||||
let u = user.clone();
|
user.insert(conn)
|
||||||
run_query(|conn| u.insert(conn), &pool).await?;
|
}, &pool).await?;
|
||||||
|
|
||||||
let renderer =
|
let renderer =
|
||||||
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user.clone() });
|
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user });
|
||||||
Ok(HttpResponse::Ok().body(renderer.render().await))
|
Ok(HttpResponse::Ok().body(renderer.render().await))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,6 @@ pub(self) mod models;
|
||||||
pub(self) mod schema;
|
pub(self) mod schema;
|
||||||
pub(self) mod views;
|
pub(self) mod views;
|
||||||
|
|
||||||
pub(self) type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
|
||||||
|
|
||||||
use actix_web::{middleware, web, App, HttpServer};
|
use actix_web::{middleware, web, App, HttpServer};
|
||||||
use diesel::{
|
use diesel::{
|
||||||
r2d2::{self, ConnectionManager},
|
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)
|
.first(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<usize> {
|
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<Self> {
|
||||||
use crate::schema::users::dsl::*;
|
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::*;
|
use crate::schema::users::dsl::*;
|
||||||
|
|
||||||
diesel::update(users.find(self.id.clone()))
|
diesel::update(users.find(self.id.clone()))
|
||||||
.set(self)
|
.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::*;
|
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