mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
update diesel to v2
This commit is contained in:
parent
fd86b650f1
commit
ed04cc97b3
30
Cargo.lock
generated
30
Cargo.lock
generated
@ -2273,25 +2273,42 @@ checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "diesel"
|
name = "diesel"
|
||||||
version = "1.4.8"
|
version = "2.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b28135ecf6b7d446b43e27e225622a038cc4e2930a1022f51cdb97ada19b8e4d"
|
checksum = "01e2adfd0a7a81070ed7beec0c62636458926326c16fedb77796d41e447b282d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"chrono",
|
"chrono",
|
||||||
"diesel_derives",
|
"diesel_derives",
|
||||||
|
"itoa 1.0.3",
|
||||||
|
"libsqlite3-sys",
|
||||||
"pq-sys",
|
"pq-sys",
|
||||||
"r2d2",
|
"r2d2",
|
||||||
"uuid 0.8.2",
|
"uuid 1.1.2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diesel-example"
|
||||||
|
version = "1.0.0"
|
||||||
|
dependencies = [
|
||||||
|
"actix-web",
|
||||||
|
"diesel",
|
||||||
|
"dotenv",
|
||||||
|
"env_logger 0.9.0",
|
||||||
|
"log",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"uuid 1.1.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "diesel_derives"
|
name = "diesel_derives"
|
||||||
version = "1.4.1"
|
version = "2.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3"
|
checksum = "22a7ab9d7967e6a1a247ea38aedf88ab808b4ac0c159576bc71866ab8f9f9250"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"proc-macro-error",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn",
|
||||||
@ -5769,7 +5786,7 @@ dependencies = [
|
|||||||
"serde_json",
|
"serde_json",
|
||||||
"sparkpost",
|
"sparkpost",
|
||||||
"time 0.3.14",
|
"time 0.3.14",
|
||||||
"uuid 0.8.2",
|
"uuid 1.1.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -7082,7 +7099,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
|
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getrandom 0.2.7",
|
"getrandom 0.2.7",
|
||||||
"serde",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -14,6 +14,7 @@ members = [
|
|||||||
"basics/todo",
|
"basics/todo",
|
||||||
"cors/backend",
|
"cors/backend",
|
||||||
"data-factory",
|
"data-factory",
|
||||||
|
"databases/diesel",
|
||||||
"databases/mongodb",
|
"databases/mongodb",
|
||||||
"databases/postgres",
|
"databases/postgres",
|
||||||
"databases/redis",
|
"databases/redis",
|
||||||
@ -60,7 +61,3 @@ members = [
|
|||||||
"websockets/echo-actorless",
|
"websockets/echo-actorless",
|
||||||
"websockets/echo",
|
"websockets/echo",
|
||||||
]
|
]
|
||||||
exclude = [
|
|
||||||
# uses incompatible libsqlite-sys to other examples
|
|
||||||
"databases/diesel",
|
|
||||||
]
|
|
||||||
|
@ -9,7 +9,7 @@ actix-identity = "0.4"
|
|||||||
|
|
||||||
chrono = { version = "0.4", features = ["serde"] }
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
derive_more = "0.99.5"
|
derive_more = "0.99.5"
|
||||||
diesel = { version = "1.4", features = ["postgres", "uuidv07", "r2d2", "chrono"] }
|
diesel = { version = "2", features = ["postgres", "r2d2", "uuid", "chrono"] }
|
||||||
dotenv = "0.15"
|
dotenv = "0.15"
|
||||||
env_logger = "0.9"
|
env_logger = "0.9"
|
||||||
r2d2 = "0.8"
|
r2d2 = "0.8"
|
||||||
@ -18,5 +18,5 @@ lazy_static = "1.4"
|
|||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
sparkpost = "0.5"
|
sparkpost = "0.5"
|
||||||
uuid = { version = "0.8", features = ["v4", "serde"] }
|
uuid = { version = "1", features = ["v4", "serde"] }
|
||||||
time = "0.3"
|
time = "0.3"
|
||||||
|
@ -2,7 +2,7 @@ use std::future::{ready, Ready};
|
|||||||
|
|
||||||
use actix_identity::Identity;
|
use actix_identity::Identity;
|
||||||
use actix_web::{dev::Payload, web, Error, FromRequest, HttpRequest, HttpResponse};
|
use actix_web::{dev::Payload, web, Error, FromRequest, HttpRequest, HttpResponse};
|
||||||
use diesel::{prelude::*, PgConnection};
|
use diesel::prelude::*;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -52,6 +52,7 @@ pub async fn login(
|
|||||||
|
|
||||||
let user_string = serde_json::to_string(&user).unwrap();
|
let user_string = serde_json::to_string(&user).unwrap();
|
||||||
id.remember(user_string);
|
id.remember(user_string);
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().finish())
|
Ok(HttpResponse::Ok().finish())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,10 +62,12 @@ pub async fn get_me(logged_user: LoggedUser) -> HttpResponse {
|
|||||||
/// Diesel query
|
/// Diesel query
|
||||||
fn query(auth_data: AuthData, pool: web::Data<Pool>) -> Result<SlimUser, ServiceError> {
|
fn query(auth_data: AuthData, pool: web::Data<Pool>) -> Result<SlimUser, ServiceError> {
|
||||||
use crate::schema::users::dsl::{email, users};
|
use crate::schema::users::dsl::{email, users};
|
||||||
let conn: &PgConnection = &pool.get().unwrap();
|
|
||||||
|
let mut conn = pool.get().unwrap();
|
||||||
|
|
||||||
let mut items = users
|
let mut items = users
|
||||||
.filter(email.eq(&auth_data.email))
|
.filter(email.eq(&auth_data.email))
|
||||||
.load::<User>(conn)?;
|
.load::<User>(&mut conn)?;
|
||||||
|
|
||||||
if let Some(user) = items.pop() {
|
if let Some(user) = items.pop() {
|
||||||
if let Ok(matching) = verify(&user.hash, &auth_data.password) {
|
if let Ok(matching) = verify(&user.hash, &auth_data.password) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use actix_web::{web, HttpResponse};
|
use actix_web::{web, HttpResponse};
|
||||||
use diesel::{prelude::*, PgConnection};
|
use diesel::prelude::*;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -34,12 +34,13 @@ fn create_invitation(
|
|||||||
fn query(eml: String, pool: web::Data<Pool>) -> Result<Invitation, crate::errors::ServiceError> {
|
fn query(eml: String, pool: web::Data<Pool>) -> Result<Invitation, crate::errors::ServiceError> {
|
||||||
use crate::schema::invitations::dsl::invitations;
|
use crate::schema::invitations::dsl::invitations;
|
||||||
|
|
||||||
let new_invitation: Invitation = eml.into();
|
let mut conn = pool.get().unwrap();
|
||||||
let conn: &PgConnection = &pool.get().unwrap();
|
|
||||||
|
let new_invitation = Invitation::from(eml);
|
||||||
|
|
||||||
let inserted_invitation = diesel::insert_into(invitations)
|
let inserted_invitation = diesel::insert_into(invitations)
|
||||||
.values(&new_invitation)
|
.values(&new_invitation)
|
||||||
.get_result(conn)?;
|
.get_result(&mut conn)?;
|
||||||
|
|
||||||
Ok(inserted_invitation)
|
Ok(inserted_invitation)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ use super::schema::*;
|
|||||||
pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
|
pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
|
#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
|
||||||
#[table_name = "users"]
|
#[diesel(table_name = users)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub hash: String,
|
pub hash: String,
|
||||||
@ -27,7 +27,7 @@ impl User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
|
#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
|
||||||
#[table_name = "invitations"]
|
#[diesel(table_name = invitations)]
|
||||||
pub struct Invitation {
|
pub struct Invitation {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
|
@ -36,16 +36,15 @@ fn query(
|
|||||||
password: String,
|
password: String,
|
||||||
pool: web::Data<Pool>,
|
pool: web::Data<Pool>,
|
||||||
) -> Result<SlimUser, crate::errors::ServiceError> {
|
) -> Result<SlimUser, crate::errors::ServiceError> {
|
||||||
use crate::schema::{
|
use crate::schema::{invitations::dsl::*, users::dsl::*};
|
||||||
invitations::dsl::{id, invitations},
|
|
||||||
users::dsl::users,
|
let mut conn = pool.get().unwrap();
|
||||||
};
|
|
||||||
let invitation_id = uuid::Uuid::parse_str(&invitation_id)?;
|
let invitation_id = uuid::Uuid::parse_str(&invitation_id)?;
|
||||||
|
|
||||||
let conn: &PgConnection = &pool.get().unwrap();
|
|
||||||
invitations
|
invitations
|
||||||
.filter(id.eq(invitation_id))
|
.filter(id.eq(invitation_id))
|
||||||
.load::<Invitation>(conn)
|
.load::<Invitation>(&mut conn)
|
||||||
.map_err(|_db_error| ServiceError::BadRequest("Invalid Invitation".into()))
|
.map_err(|_db_error| ServiceError::BadRequest("Invalid Invitation".into()))
|
||||||
.and_then(|mut result| {
|
.and_then(|mut result| {
|
||||||
if let Some(invitation) = result.pop() {
|
if let Some(invitation) = result.pop() {
|
||||||
@ -54,10 +53,13 @@ fn query(
|
|||||||
// try hashing the password, else return the error that will be converted to ServiceError
|
// try hashing the password, else return the error that will be converted to ServiceError
|
||||||
let password: String = hash_password(&password)?;
|
let password: String = hash_password(&password)?;
|
||||||
dbg!(&password);
|
dbg!(&password);
|
||||||
|
|
||||||
let user = User::from_details(invitation.email, password);
|
let user = User::from_details(invitation.email, password);
|
||||||
let inserted_user: User =
|
let inserted_user: User = diesel::insert_into(users)
|
||||||
diesel::insert_into(users).values(&user).get_result(conn)?;
|
.values(&user)
|
||||||
|
.get_result(&mut conn)?;
|
||||||
dbg!(&inserted_user);
|
dbg!(&inserted_user);
|
||||||
|
|
||||||
return Ok(inserted_user.into());
|
return Ok(inserted_user.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,3 @@
|
|||||||
table! {
|
|
||||||
invitations (id) {
|
|
||||||
id -> Uuid,
|
|
||||||
email -> Varchar,
|
|
||||||
expires_at -> Timestamp,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
table! {
|
table! {
|
||||||
users (email) {
|
users (email) {
|
||||||
email -> Varchar,
|
email -> Varchar,
|
||||||
@ -14,4 +6,12 @@ table! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
allow_tables_to_appear_in_same_query!(invitations, users,);
|
table! {
|
||||||
|
invitations (id) {
|
||||||
|
id -> Uuid,
|
||||||
|
email -> Varchar,
|
||||||
|
expires_at -> Timestamp,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allow_tables_to_appear_in_same_query!(users, invitations);
|
||||||
|
@ -5,10 +5,10 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
diesel = { version = "1.4", features = ["sqlite", "r2d2"] }
|
diesel = { version = "2", features = ["sqlite", "r2d2"] }
|
||||||
dotenv = "0.15"
|
dotenv = "0.15"
|
||||||
env_logger = "0.9"
|
env_logger = "0.9"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
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 = "1", features = ["v4", "serde"] }
|
||||||
|
@ -7,8 +7,8 @@ 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(
|
||||||
|
conn: &mut SqliteConnection,
|
||||||
uid: Uuid,
|
uid: Uuid,
|
||||||
conn: &SqliteConnection,
|
|
||||||
) -> Result<Option<models::User>, DbError> {
|
) -> Result<Option<models::User>, DbError> {
|
||||||
use crate::schema::users::dsl::*;
|
use crate::schema::users::dsl::*;
|
||||||
|
|
||||||
@ -22,9 +22,8 @@ pub fn find_user_by_uid(
|
|||||||
|
|
||||||
/// Run query using Diesel to insert a new database row and return the result.
|
/// Run query using Diesel to insert a new database row and return the result.
|
||||||
pub fn insert_new_user(
|
pub fn insert_new_user(
|
||||||
// prevent collision with `name` column imported inside the function
|
conn: &mut SqliteConnection,
|
||||||
nm: &str,
|
nm: &str, // prevent collision with `name` column imported inside the function
|
||||||
conn: &SqliteConnection,
|
|
||||||
) -> Result<models::User, DbError> {
|
) -> 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)
|
||||||
|
@ -27,8 +27,8 @@ async fn get_user(
|
|||||||
|
|
||||||
// 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 || {
|
let user = web::block(move || {
|
||||||
let conn = pool.get()?;
|
let mut conn = pool.get()?;
|
||||||
actions::find_user_by_uid(user_uid, &conn)
|
actions::find_user_by_uid(&mut conn, user_uid)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
.map_err(actix_web::error::ErrorInternalServerError)?;
|
.map_err(actix_web::error::ErrorInternalServerError)?;
|
||||||
@ -49,8 +49,8 @@ async fn add_user(
|
|||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
// 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 || {
|
let user = web::block(move || {
|
||||||
let conn = pool.get()?;
|
let mut conn = pool.get()?;
|
||||||
actions::insert_new_user(&form.name, &conn)
|
actions::insert_new_user(&mut conn, &form.name)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
.map_err(actix_web::error::ErrorInternalServerError)?;
|
.map_err(actix_web::error::ErrorInternalServerError)?;
|
||||||
@ -136,7 +136,7 @@ mod tests {
|
|||||||
// Delete new user from table
|
// Delete new user from table
|
||||||
use crate::schema::users::dsl::*;
|
use crate::schema::users::dsl::*;
|
||||||
diesel::delete(users.filter(id.eq(resp.id)))
|
diesel::delete(users.filter(id.eq(resp.id)))
|
||||||
.execute(&pool.get().expect("couldn't get db connection from pool"))
|
.execute(&mut pool.get().expect("couldn't get db connection from pool"))
|
||||||
.expect("couldn't delete test user from table");
|
.expect("couldn't delete test user from table");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user