diff --git a/src/identity/adapters/output/db/mod.rs b/src/identity/adapters/output/db/mod.rs new file mode 100644 index 0000000..6be5441 --- /dev/null +++ b/src/identity/adapters/output/db/mod.rs @@ -0,0 +1,5 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +pub mod postgres; diff --git a/src/identity/adapters/output/db/postgres/errors.rs b/src/identity/adapters/output/db/postgres/errors.rs new file mode 100644 index 0000000..d9772e9 --- /dev/null +++ b/src/identity/adapters/output/db/postgres/errors.rs @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use std::borrow::Cow; + +use sqlx::Error as SqlxError; + +use crate::identity::application::port::output::db::errors::OutDBPortError; + +impl From for OutDBPortError { + fn from(e: SqlxError) -> Self { + log::error!("[postgres] err: {}", e); + if let SqlxError::Database(err) = e { + if err.code() == Some(Cow::from("23505")) { + let msg = err.message(); + if msg.contains("user_query_username_key") { + return Self::InternalError; + } else if msg.contains("user_query_email_key") { + return Self::InternalError; + } else if msg.contains("verification_otp_secret_key") { + return Self::VerificationOTPSecretExists; + } else { + println!("{msg}"); + } + } + } + Self::InternalError + } +} diff --git a/src/identity/adapters/output/db/postgres/mod.rs b/src/identity/adapters/output/db/postgres/mod.rs new file mode 100644 index 0000000..c6f79ca --- /dev/null +++ b/src/identity/adapters/output/db/postgres/mod.rs @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +//use crate::auth::application::port::out::db::save_oauth_state::SaveOAuthState; +use std::sync::Arc; + +use sqlx::postgres::PgPool; + +use crate::db::{migrate::RunMigrations, sqlx_postgres::Postgres}; + +pub mod create_verification_secret; +pub mod email_exists; +mod errors; +pub mod username_exists; + +#[derive(Clone)] +pub struct DBOutPostgresAdapter { + pool: PgPool, +} + +impl DBOutPostgresAdapter { + pub fn new(pool: PgPool) -> Self { + Self { pool } + } + + pub fn migratable(&self) -> Arc { + Arc::new(Postgres::new(self.pool.clone())) + } +}