feat: db postgres adapter: bootstrap
This commit is contained in:
parent
a7f9c3f9c6
commit
2f90d245c7
3 changed files with 65 additions and 0 deletions
5
src/identity/adapters/output/db/mod.rs
Normal file
5
src/identity/adapters/output/db/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
pub mod postgres;
|
30
src/identity/adapters/output/db/postgres/errors.rs
Normal file
30
src/identity/adapters/output/db/postgres/errors.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
30
src/identity/adapters/output/db/postgres/mod.rs
Normal file
30
src/identity/adapters/output/db/postgres/mod.rs
Normal 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()))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue