From 2f90d245c77741e346c219b84bc02019a8ecbfc9 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Fri, 17 May 2024 23:23:22 +0530 Subject: [PATCH] feat: db postgres adapter: bootstrap --- src/identity/adapters/output/db/mod.rs | 5 ++++ .../adapters/output/db/postgres/errors.rs | 30 +++++++++++++++++++ .../adapters/output/db/postgres/mod.rs | 30 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/identity/adapters/output/db/mod.rs create mode 100644 src/identity/adapters/output/db/postgres/errors.rs create mode 100644 src/identity/adapters/output/db/postgres/mod.rs 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())) + } +}