[NOT WORK] Begin refactor run_query
This commit is contained in:
parent
6facdae336
commit
65e20c2fef
4 changed files with 33 additions and 37 deletions
|
@ -1,8 +1,25 @@
|
||||||
use actix_web::web;
|
use actix_web::{web, Error};
|
||||||
|
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);
|
||||||
|
|
|
@ -7,17 +7,12 @@ use crate::{
|
||||||
user::{UserComponent, UserComponentProps},
|
user::{UserComponent, UserComponentProps},
|
||||||
users::{UsersComponent, UsersComponentProps},
|
users::{UsersComponent, UsersComponentProps},
|
||||||
},
|
},
|
||||||
DbPool,
|
DbPool, controllers::run_query,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[get("")]
|
#[get("")]
|
||||||
async fn index(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
async fn index(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||||
let users = web::block(move || {
|
let users = run_query(|conn| User::all(conn), &pool).await?;
|
||||||
let conn = pool.get()?;
|
|
||||||
User::all(&conn)
|
|
||||||
})
|
|
||||||
.await?
|
|
||||||
.map_err(actix_web::error::ErrorInternalServerError)?;
|
|
||||||
|
|
||||||
let renderer = ServerRenderer::<UsersComponent>::with_props(UsersComponentProps {
|
let renderer = ServerRenderer::<UsersComponent>::with_props(UsersComponentProps {
|
||||||
users: users.clone(),
|
users: users.clone(),
|
||||||
|
@ -32,12 +27,7 @@ async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||||
});
|
});
|
||||||
|
|
||||||
let u = user.clone();
|
let u = user.clone();
|
||||||
web::block(move || {
|
run_query(|conn| u.insert(conn), &pool).await?;
|
||||||
let conn = pool.get()?;
|
|
||||||
u.insert(&conn)
|
|
||||||
})
|
|
||||||
.await?
|
|
||||||
.map_err(actix_web::error::ErrorInternalServerError)?;
|
|
||||||
|
|
||||||
let renderer =
|
let renderer =
|
||||||
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user.clone() });
|
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user.clone() });
|
||||||
|
|
|
@ -1,3 +1 @@
|
||||||
pub(self) type DbError = Box<dyn std::error::Error + Send + Sync>;
|
pub mod user;
|
||||||
|
|
||||||
pub mod user;
|
|
|
@ -1,11 +1,9 @@
|
||||||
use diesel::prelude::*;
|
use diesel::{prelude::*, result::QueryResult};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::schema::users;
|
use crate::schema::users;
|
||||||
|
|
||||||
use super::DbError;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Insertable, AsChangeset, PartialEq)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Insertable, AsChangeset, PartialEq)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
|
@ -25,40 +23,33 @@ impl User {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn all(conn: &SqliteConnection) -> Result<Vec<Self>, DbError> {
|
pub fn all(conn: &SqliteConnection) -> QueryResult<Vec<Self>> {
|
||||||
users::table.load::<Self>(conn).map_err(Into::into)
|
users::table.load(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_by_id(conn: &SqliteConnection, id: String) -> Result<Self, DbError> {
|
pub fn find_by_id(conn: &SqliteConnection, id: String) -> QueryResult<Self> {
|
||||||
users::table
|
users::table
|
||||||
.filter(users::id.eq(id))
|
.filter(users::id.eq(id))
|
||||||
.first::<Self>(conn)
|
.first(conn)
|
||||||
.map_err(Into::into)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&self, conn: &SqliteConnection) -> Result<(), DbError> {
|
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<usize> {
|
||||||
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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&self, conn: &SqliteConnection) -> Result<(), DbError> {
|
pub fn update(&self, conn: &SqliteConnection) -> QueryResult<usize> {
|
||||||
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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(&self, conn: &SqliteConnection) -> Result<(), DbError> {
|
pub fn delete(&self, conn: &SqliteConnection) -> QueryResult<usize> {
|
||||||
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(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue