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