feat: db pg adapter: define email_exists adapter
This commit is contained in:
parent
2f90d245c7
commit
4e4287c418
1 changed files with 68 additions and 0 deletions
68
src/identity/adapters/output/db/postgres/email_exists.rs
Normal file
68
src/identity/adapters/output/db/postgres/email_exists.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
use super::DBOutPostgresAdapter;
|
||||
use crate::identity::application::port::output::db::{
|
||||
email_exists::EmailExistsOutDBPort, errors::*,
|
||||
};
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl EmailExistsOutDBPort for DBOutPostgresAdapter {
|
||||
async fn email_exists(&self, email: &str) -> OutDBPortResult<bool> {
|
||||
let res = sqlx::query!(
|
||||
"SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM user_query
|
||||
WHERE
|
||||
email = $1
|
||||
);",
|
||||
email
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
if let Some(x) = res.exists {
|
||||
Ok(x)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_postgres_email_exists() {
|
||||
let email = "foo@exmaple.com";
|
||||
let settings = crate::settings::tests::get_settings().await;
|
||||
let db = super::DBOutPostgresAdapter::new(
|
||||
sqlx::postgres::PgPool::connect(&settings.database.url)
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
// state doesn't exist
|
||||
assert!(!db.email_exists(email).await.unwrap());
|
||||
|
||||
sqlx::query!(
|
||||
"INSERT INTO user_query
|
||||
(view_id, version, username, email, hashed_password)
|
||||
VALUES ($1, $2, $3, $4, $5);",
|
||||
"1",
|
||||
1,
|
||||
"foo",
|
||||
email,
|
||||
"passwd"
|
||||
)
|
||||
.execute(&db.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// state exists
|
||||
assert!(db.email_exists(email).await.unwrap());
|
||||
|
||||
settings.drop_db().await;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue