1
0
Fork 0

[NOT WORK] Begin refactor run_query

This commit is contained in:
Florian RICHER (MrDev023) 2022-03-21 23:50:50 +01:00
parent 6facdae336
commit 65e20c2fef
4 changed files with 33 additions and 37 deletions

View file

@ -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<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);

View file

@ -7,17 +7,12 @@ use crate::{
user::{UserComponent, UserComponentProps},
users::{UsersComponent, UsersComponentProps},
},
DbPool,
DbPool, controllers::run_query,
};
#[get("")]
async fn index(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
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::<UsersComponent>::with_props(UsersComponentProps {
users: users.clone(),
@ -32,12 +27,7 @@ async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
});
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::<UserComponent>::with_props(UserComponentProps { user: user.clone() });

View file

@ -1,3 +1 @@
pub(self) type DbError = Box<dyn std::error::Error + Send + Sync>;
pub mod user;
pub mod user;

View file

@ -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<Vec<Self>, DbError> {
users::table.load::<Self>(conn).map_err(Into::into)
pub fn all(conn: &SqliteConnection) -> QueryResult<Vec<Self>> {
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
.filter(users::id.eq(id))
.first::<Self>(conn)
.map_err(Into::into)
.first(conn)
}
pub fn insert(&self, conn: &SqliteConnection) -> Result<(), DbError> {
pub fn insert(&self, conn: &SqliteConnection) -> QueryResult<usize> {
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<usize> {
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<usize> {
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)
}
}