Skip to content

Commit

Permalink
Shield SeaORM: Fix entity feature in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Dec 28, 2024
1 parent 395e6ac commit d49574c
Showing 1 changed file with 99 additions and 64 deletions.
163 changes: 99 additions & 64 deletions packages/storage/shield-sea-orm/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,27 @@ impl Storage<user::Model> for SeaOrmStorage {
}

async fn user_by_email(&self, email: &str) -> Result<Option<user::Model>, StorageError> {
User::find()
.left_join(email_address::Entity)
.filter(email_address::Column::Email.eq(email))
.one(&self.database)
.await
.map_err(|err| StorageError::Engine(err.to_string()))
#[cfg(feature = "entity")]
{
use sea_orm::{JoinType, QuerySelect, RelationTrait};

User::find()
.join(JoinType::LeftJoin, user::Relation::Entity.def())
.join(JoinType::LeftJoin, entity::Relation::EmailAddress.def())
.filter(email_address::Column::Email.eq(email))
.one(&self.database)
.await
.map_err(|err| StorageError::Engine(err.to_string()))
}
#[cfg(not(feature = "entity"))]
{
User::find()
.left_join(email_address::Entity)
.filter(email_address::Column::Email.eq(email))
.one(&self.database)
.await
.map_err(|err| StorageError::Engine(err.to_string()))
}
}

async fn create_user(
Expand All @@ -62,63 +77,81 @@ impl Storage<user::Model> for SeaOrmStorage {
self.database
.transaction::<_, user::Model, StorageError>(|database_transaction| {
Box::pin(async move {
let user = {
#[cfg(feature = "entity")]
{
let active_model = entity::ActiveModel {
name: ActiveValue::Set(user.name.unwrap_or_default()),
..Default::default()
};

let entity = active_model
.insert(database_transaction)
.await
.map_err(|err| StorageError::Engine(err.to_string()))?;

let active_model = user::ActiveModel {
entity_id: ActiveValue::Set(entity.id),
..Default::default()
};

active_model
.insert(database_transaction)
.await
.map_err(|err| StorageError::Engine(err.to_string()))?
}
#[cfg(feature = "entity")]
{
let active_model = entity::ActiveModel {
name: ActiveValue::Set(user.name.unwrap_or_default()),
..Default::default()
};

#[cfg(not(feature = "entity"))]
{
let active_model = user::ActiveModel {
name: ActiveValue::Set(user.name.unwrap_or_default()),
..Default::default()
};

active_model
.insert(database_transaction)
.await
.map_err(|err| StorageError::Engine(err.to_string()))?
}
};

let active_model = email_address::ActiveModel {
email: ActiveValue::Set(email_address.email),
is_primary: ActiveValue::Set(email_address.is_primary),
is_verified: ActiveValue::Set(email_address.is_verified),
verification_token: ActiveValue::Set(email_address.verification_token),
verification_token_expired_at: ActiveValue::Set(
email_address.verification_token_expired_at,
),
verified_at: ActiveValue::Set(email_address.verified_at),
user_id: ActiveValue::Set(user.id),
..Default::default()
};

active_model
.insert(database_transaction)
.await
.map_err(|err| StorageError::Engine(err.to_string()))?;
let entity = active_model
.insert(database_transaction)
.await
.map_err(|err| StorageError::Engine(err.to_string()))?;

Ok(user)
let active_model = user::ActiveModel {
entity_id: ActiveValue::Set(entity.id),
..Default::default()
};

let user = active_model
.insert(database_transaction)
.await
.map_err(|err| StorageError::Engine(err.to_string()))?;

let active_model = email_address::ActiveModel {
email: ActiveValue::Set(email_address.email),
is_primary: ActiveValue::Set(email_address.is_primary),
is_verified: ActiveValue::Set(email_address.is_verified),
verification_token: ActiveValue::Set(email_address.verification_token),
verification_token_expired_at: ActiveValue::Set(
email_address.verification_token_expired_at,
),
verified_at: ActiveValue::Set(email_address.verified_at),
entity_id: ActiveValue::Set(entity.id),
..Default::default()
};

active_model
.insert(database_transaction)
.await
.map_err(|err| StorageError::Engine(err.to_string()))?;

Ok(user)
}

#[cfg(not(feature = "entity"))]
{
let active_model = user::ActiveModel {
name: ActiveValue::Set(user.name.unwrap_or_default()),
..Default::default()
};

let user = active_model
.insert(database_transaction)
.await
.map_err(|err| StorageError::Engine(err.to_string()))?;

let active_model = email_address::ActiveModel {
email: ActiveValue::Set(email_address.email),
is_primary: ActiveValue::Set(email_address.is_primary),
is_verified: ActiveValue::Set(email_address.is_verified),
verification_token: ActiveValue::Set(email_address.verification_token),
verification_token_expired_at: ActiveValue::Set(
email_address.verification_token_expired_at,
),
verified_at: ActiveValue::Set(email_address.verified_at),
user_id: ActiveValue::Set(user.id),
..Default::default()
};

active_model
.insert(database_transaction)
.await
.map_err(|err| StorageError::Engine(err.to_string()))?;

Ok(user)
}
})
})
.await
Expand All @@ -145,17 +178,18 @@ impl Storage<user::Model> for SeaOrmStorage {
{
use sea_orm::ModelTrait;

let mut entity_active_model = user_entity
let mut entity_active_model: entity::ActiveModel = user_entity
.find_related(entity::Entity)
.one(database_transaction)
.await
.map_err(|err| StorageError::Engine(err.to_string()))?
.ok_or_else(|| {
StorageError::NotFound(
"Entity".to_owned(),
user_entity.entity_id.clone(),
user_entity.entity_id.to_string(),
)
})?;
})?
.into();

if let Some(Some(name)) = user.name {
entity_active_model.name = ActiveValue::Set(name);
Expand All @@ -167,6 +201,7 @@ impl Storage<user::Model> for SeaOrmStorage {
.map_err(|err| StorageError::Engine(err.to_string()))?;
}

#[allow(unused_mut)]
let mut user_active_model: user::ActiveModel = user_entity.into();

#[cfg(not(feature = "entity"))]
Expand Down

0 comments on commit d49574c

Please sign in to comment.