feat: db pg adapter: define email_exists adapter

This commit is contained in:
Aravinth Manivannan 2024-05-17 23:23:38 +05:30
parent 2f90d245c7
commit 4e4287c418
Signed by: realaravinth
GPG key ID: F8F50389936984FF

View 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;
}
}