Do reset password operations in db transaction

And modify signature of model methods to accept an executor instead of a
pool connection which will also allow transactions.
This commit is contained in:
2023-10-13 14:44:40 +02:00
parent 60671d5865
commit 835e9dc748
7 changed files with 126 additions and 82 deletions

View File

@@ -1,7 +1,7 @@
use chrono::{DateTime, Utc};
use ipnetwork::IpNetwork;
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use sqlx::{Executor, Postgres};
use uuid::Uuid;
use crate::error::{Error, Result};
@@ -31,7 +31,10 @@ impl UserPasswordResetToken {
Utc::now() > self.expires_at
}
pub async fn get(pool: &PgPool, token_id: Uuid) -> Result<UserPasswordResetToken> {
pub async fn get(
pool: impl Executor<'_, Database = Postgres>,
token_id: Uuid,
) -> Result<UserPasswordResetToken> {
sqlx::query_as!(
UserPasswordResetToken,
r#"select
@@ -51,7 +54,7 @@ impl UserPasswordResetToken {
}
pub async fn create(
pool: &PgPool,
pool: impl Executor<'_, Database = Postgres>,
payload: CreatePasswordResetToken,
) -> Result<UserPasswordResetToken> {
Ok(sqlx::query_as!(
@@ -72,7 +75,7 @@ impl UserPasswordResetToken {
}
pub async fn delete(
pool: &PgPool,
pool: impl Executor<'_, Database = Postgres>,
token_id: Uuid,
) -> Result<()> {
sqlx::query!(