aboutsummaryrefslogtreecommitdiffstats
path: root/lib/auth-service/src/service/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/auth-service/src/service/mod.rs')
-rw-r--r--lib/auth-service/src/service/mod.rs66
1 files changed, 48 insertions, 18 deletions
diff --git a/lib/auth-service/src/service/mod.rs b/lib/auth-service/src/service/mod.rs
index 80b29de..6d04b67 100644
--- a/lib/auth-service/src/service/mod.rs
+++ b/lib/auth-service/src/service/mod.rs
@@ -1,7 +1,7 @@
use async_session::{Result, Session, SessionStore};
use async_trait::async_trait;
use shared_svc::cache::{CacheKey, RedisManager, redis::AsyncCommands};
-use sqlx::{PgPool, Postgres, Transaction};
+use sqlx::{Executor, PgPool, Postgres};
use tracing::{debug, instrument};
use crate::Provider;
@@ -9,14 +9,13 @@ use crate::Provider;
#[async_trait]
pub trait AccountMgr {
async fn get_apid_by_email(&self, email: &str) -> Result<Option<String>>;
- async fn create_account(&self, provider: Provider, provider_user_id: &str, ap_id: &str);
- async fn create_account_step(
+ async fn create_account<'c>(
&self,
provider: Provider,
provider_user_id: &str,
ap_id: &str,
email: &str,
- transaction: &mut Transaction<'_, Postgres>,
+ transaction: Option<impl Executor<'c, Database = Postgres>>,
) -> Result;
async fn persist_session(&self);
}
@@ -34,31 +33,34 @@ impl AccountMgr for AuthService {
todo!()
}
- #[instrument(skip(self))]
- async fn create_account(&self, provider: Provider, provider_user_id: &str, ap_id: &str) {
- todo!()
- }
- #[instrument(skip(self, transaction))]
- async fn create_account_step(
+ #[instrument(skip(transaction))]
+ async fn create_account<'c>(
&self,
provider: Provider,
provider_user_id: &str,
ap_id: &str,
email: &str,
- transaction: &mut Transaction<'_, Postgres>,
+ transaction: Option<impl Executor<'c, Database = Postgres>>,
) -> Result {
- sqlx::query!(
+ let query = sqlx::query!(
"insert into account
(provider_id, provider_user_id, email, ap_id)
- values ($1, $2, $3, $4)
+ values
+ ($1, $2, $3, $4)
+ on conflict (provider_id, provider_user_id)
+ do nothing
",
- "",
+ provider.to_string(),
provider_user_id,
- "",
+ email,
ap_id
- )
- .execute(&mut **transaction)
- .await?;
+ );
+
+ if let Some(con) = transaction {
+ query.execute(con).await?;
+ } else {
+ query.execute(&self.database).await?;
+ };
todo!()
}
@@ -129,3 +131,31 @@ impl SessionStore for AuthService {
Ok(())
}
}
+
+async fn create_account_step<'c, E>(
+ provider: Provider,
+ provider_user_id: &str,
+ ap_id: &str,
+ email: &str,
+ transaction: E,
+) -> Result
+where
+ E: Executor<'c, Database = Postgres>,
+{
+ sqlx::query!(
+ "insert into account
+ (provider_id, provider_user_id, email, ap_id)
+ values
+ ($1, $2, $3, $4)
+ on conflict (provider_id, provider_user_id)
+ do nothing
+ ",
+ provider.to_string(),
+ provider_user_id,
+ email,
+ ap_id
+ )
+ .execute(transaction)
+ .await?;
+ todo!()
+}