Add a Transaction type to simplify dealing with Transactions

This commit is contained in:
Ryan Leckey
2020-01-03 22:42:10 -08:00
parent 28ed854b03
commit b1a27ddac2
10 changed files with 272 additions and 41 deletions

View File

@@ -50,7 +50,9 @@ async fn register(mut req: Request<PgPool>) -> Response {
let body: RegisterRequestBody = req.body_json().await.unwrap();
let hash = hash_password(&body.password).unwrap();
let mut pool = req.state();
// Make a new transaction
let pool = req.state();
let mut tx = pool.begin().await.unwrap();
let rec = sqlx::query!(
r#"
@@ -62,12 +64,15 @@ RETURNING id, username, email
body.email,
hash,
)
.fetch_one(&mut pool)
.fetch_one(&mut tx)
.await
.unwrap();
let token = generate_token(rec.id).unwrap();
// Explicitly commit
tx.commit().await.unwrap();
#[derive(serde::Serialize)]
struct RegisterResponseBody {
user: User,