feat: db postgres adapter: bootstrap

This commit is contained in:
Aravinth Manivannan 2024-05-17 23:23:22 +05:30
parent a7f9c3f9c6
commit 2f90d245c7
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 65 additions and 0 deletions

View file

@ -0,0 +1,5 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod postgres;

View file

@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// 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<SqlxError> 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
}
}

View file

@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// 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<dyn RunMigrations> {
Arc::new(Postgres::new(self.pool.clone()))
}
}