|
| 1 | +use surrealdb::types::Value; |
| 2 | + |
| 3 | +use crate::core::domain::entities::User; |
| 4 | +use crate::core::domain::entities::user::StorableUser; |
| 5 | +use crate::core::domain::repositories::MiscRepository; |
| 6 | +use crate::error::Result; |
| 7 | +use crate::infrastructure::persistence::into_iter_objects; |
| 8 | +use crate::providers::avored_database_provider::AvoRedDatabaseProvider; |
| 9 | +use std::collections::BTreeMap; |
| 10 | +use std::sync::Arc; |
| 11 | + |
| 12 | +#[derive(Clone)] |
| 13 | +pub struct MiscRepositoryImpl { |
| 14 | + pub database_provider: Arc<AvoRedDatabaseProvider>, |
| 15 | +} |
| 16 | + |
| 17 | +impl MiscRepositoryImpl { |
| 18 | + pub fn new(database_provider: Arc<AvoRedDatabaseProvider>) -> Self { |
| 19 | + Self { database_provider } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +#[async_trait::async_trait] |
| 24 | +impl MiscRepository for MiscRepositoryImpl { |
| 25 | + async fn create_user(&self, storable_user: StorableUser) -> Result<User> { |
| 26 | + let (datastore, database_session) = &self.database_provider.db; |
| 27 | + |
| 28 | + let sql = "CREATE users SET name=$name, email=$email, password=$password;"; |
| 29 | + let data: BTreeMap<String, Value> = [ |
| 30 | + ("name".into(), Value::String(storable_user.name.into())), |
| 31 | + ("email".into(), Value::String(storable_user.email.into())), |
| 32 | + ("password".into(), Value::String(storable_user.password.into())), |
| 33 | + ] |
| 34 | + .into(); |
| 35 | + |
| 36 | + let responses = datastore |
| 37 | + .execute(sql, database_session, Some(data.into())) |
| 38 | + .await?; |
| 39 | + |
| 40 | + let result_object = into_iter_objects(responses)? |
| 41 | + .next() |
| 42 | + .ok_or_else(|| crate::error::Error::Generic("No user returned from insert".to_string()))??; |
| 43 | + |
| 44 | + let user: User = result_object.try_into()?; |
| 45 | + |
| 46 | + Ok(user) |
| 47 | + } |
| 48 | +} |
| 49 | + |
0 commit comments