mirror of
https://github.com/actix/examples
synced 2025-01-22 22:05:57 +01:00
fix diesel example (#460)
This commit is contained in:
parent
414bf927b8
commit
261589ca87
@ -14,7 +14,6 @@ dotenv = "0.15"
|
|||||||
env_logger = "0.8"
|
env_logger = "0.8"
|
||||||
failure = "0.1.8"
|
failure = "0.1.8"
|
||||||
futures = "0.3.1"
|
futures = "0.3.1"
|
||||||
r2d2 = "0.8"
|
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
uuid = { version = "0.8", features = ["serde", "v4"] }
|
uuid = { version = "0.8", features = ["serde", "v4"] }
|
||||||
|
@ -3,11 +3,13 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use crate::models;
|
use crate::models;
|
||||||
|
|
||||||
|
type DbError = Box<dyn std::error::Error + Send + Sync>;
|
||||||
|
|
||||||
/// Run query using Diesel to find user by uid and return it.
|
/// Run query using Diesel to find user by uid and return it.
|
||||||
pub fn find_user_by_uid(
|
pub fn find_user_by_uid(
|
||||||
uid: Uuid,
|
uid: Uuid,
|
||||||
conn: &SqliteConnection,
|
conn: &SqliteConnection,
|
||||||
) -> Result<Option<models::User>, diesel::result::Error> {
|
) -> Result<Option<models::User>, DbError> {
|
||||||
use crate::schema::users::dsl::*;
|
use crate::schema::users::dsl::*;
|
||||||
|
|
||||||
let user = users
|
let user = users
|
||||||
@ -23,7 +25,7 @@ pub fn insert_new_user(
|
|||||||
// prevent collision with `name` column imported inside the function
|
// prevent collision with `name` column imported inside the function
|
||||||
nm: &str,
|
nm: &str,
|
||||||
conn: &SqliteConnection,
|
conn: &SqliteConnection,
|
||||||
) -> Result<models::User, diesel::result::Error> {
|
) -> Result<models::User, DbError> {
|
||||||
// It is common when using Diesel with Actix web to import schema-related
|
// It is common when using Diesel with Actix web to import schema-related
|
||||||
// modules inside a function's scope (rather than the normal module's scope)
|
// modules inside a function's scope (rather than the normal module's scope)
|
||||||
// to prevent import collisions and namespace pollution.
|
// to prevent import collisions and namespace pollution.
|
||||||
|
@ -24,15 +24,17 @@ async fn get_user(
|
|||||||
user_uid: web::Path<Uuid>,
|
user_uid: web::Path<Uuid>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
let user_uid = user_uid.into_inner();
|
let user_uid = user_uid.into_inner();
|
||||||
let conn = pool.get().expect("couldn't get db connection from pool");
|
|
||||||
|
|
||||||
// use web::block to offload blocking Diesel code without blocking server thread
|
// use web::block to offload blocking Diesel code without blocking server thread
|
||||||
let user = web::block(move || actions::find_user_by_uid(user_uid, &conn))
|
let user = web::block(move || {
|
||||||
.await
|
let conn = pool.get()?;
|
||||||
.map_err(|e| {
|
actions::find_user_by_uid(user_uid, &conn)
|
||||||
eprintln!("{}", e);
|
})
|
||||||
HttpResponse::InternalServerError().finish()
|
.await
|
||||||
})?;
|
.map_err(|e| {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
HttpResponse::InternalServerError().finish()
|
||||||
|
})?;
|
||||||
|
|
||||||
if let Some(user) = user {
|
if let Some(user) = user {
|
||||||
Ok(HttpResponse::Ok().json(user))
|
Ok(HttpResponse::Ok().json(user))
|
||||||
@ -49,15 +51,16 @@ async fn add_user(
|
|||||||
pool: web::Data<DbPool>,
|
pool: web::Data<DbPool>,
|
||||||
form: web::Json<models::NewUser>,
|
form: web::Json<models::NewUser>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
let conn = pool.get().expect("couldn't get db connection from pool");
|
|
||||||
|
|
||||||
// use web::block to offload blocking Diesel code without blocking server thread
|
// use web::block to offload blocking Diesel code without blocking server thread
|
||||||
let user = web::block(move || actions::insert_new_user(&form.name, &conn))
|
let user = web::block(move || {
|
||||||
.await
|
let conn = pool.get()?;
|
||||||
.map_err(|e| {
|
actions::insert_new_user(&form.name, &conn)
|
||||||
eprintln!("{}", e);
|
})
|
||||||
HttpResponse::InternalServerError().finish()
|
.await
|
||||||
})?;
|
.map_err(|e| {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
HttpResponse::InternalServerError().finish()
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().json(user))
|
Ok(HttpResponse::Ok().json(user))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user