domain: add cross-cutting value objects (SystemId, DateTimeStamp, Checksum, StructuredData)
This commit is contained in:
@@ -3,9 +3,8 @@ use domain::{
|
||||
entities::User,
|
||||
errors::DomainError,
|
||||
ports::UserRepository,
|
||||
value_objects::{Email, PasswordHash, Role, UserId},
|
||||
value_objects::{Email, PasswordHash, SystemId},
|
||||
};
|
||||
use std::str::FromStr;
|
||||
use crate::db::PgPool;
|
||||
|
||||
pub struct PostgresUserRepository {
|
||||
@@ -18,9 +17,9 @@ impl PostgresUserRepository {
|
||||
|
||||
#[async_trait]
|
||||
impl UserRepository for PostgresUserRepository {
|
||||
async fn find_by_id(&self, id: &UserId) -> Result<Option<User>, DomainError> {
|
||||
async fn find_by_id(&self, id: &SystemId) -> Result<Option<User>, DomainError> {
|
||||
let row = sqlx::query!(
|
||||
"SELECT id, email, password_hash, role, created_at FROM users WHERE id = $1",
|
||||
"SELECT id, email, password_hash, created_at FROM users WHERE id = $1",
|
||||
*id.as_uuid()
|
||||
)
|
||||
.fetch_optional(&self.pool)
|
||||
@@ -28,10 +27,9 @@ impl UserRepository for PostgresUserRepository {
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
|
||||
row.map(|r| Ok(User {
|
||||
id: UserId::from_uuid(r.id),
|
||||
id: SystemId::from_uuid(r.id),
|
||||
email: Email::new(r.email)?,
|
||||
password_hash: PasswordHash::from_hash(r.password_hash),
|
||||
role: Role::from_str(&r.role).map_err(DomainError::Internal)?,
|
||||
created_at: r.created_at,
|
||||
}))
|
||||
.transpose()
|
||||
@@ -39,7 +37,7 @@ impl UserRepository for PostgresUserRepository {
|
||||
|
||||
async fn find_by_email(&self, email: &Email) -> Result<Option<User>, DomainError> {
|
||||
let row = sqlx::query!(
|
||||
"SELECT id, email, password_hash, role, created_at FROM users WHERE email = $1",
|
||||
"SELECT id, email, password_hash, created_at FROM users WHERE email = $1",
|
||||
email.as_str()
|
||||
)
|
||||
.fetch_optional(&self.pool)
|
||||
@@ -47,10 +45,9 @@ impl UserRepository for PostgresUserRepository {
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
|
||||
row.map(|r| Ok(User {
|
||||
id: UserId::from_uuid(r.id),
|
||||
id: SystemId::from_uuid(r.id),
|
||||
email: Email::new(r.email)?,
|
||||
password_hash: PasswordHash::from_hash(r.password_hash),
|
||||
role: Role::from_str(&r.role).map_err(DomainError::Internal)?,
|
||||
created_at: r.created_at,
|
||||
}))
|
||||
.transpose()
|
||||
@@ -58,16 +55,14 @@ impl UserRepository for PostgresUserRepository {
|
||||
|
||||
async fn save(&self, user: &User) -> Result<(), DomainError> {
|
||||
sqlx::query!(
|
||||
"INSERT INTO users (id, email, password_hash, role, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
"INSERT INTO users (id, email, password_hash, created_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
email = EXCLUDED.email,
|
||||
password_hash = EXCLUDED.password_hash,
|
||||
role = EXCLUDED.role",
|
||||
password_hash = EXCLUDED.password_hash",
|
||||
*user.id.as_uuid(),
|
||||
user.email.as_str(),
|
||||
user.password_hash.as_str(),
|
||||
user.role.to_string(),
|
||||
user.created_at
|
||||
)
|
||||
.execute(&self.pool)
|
||||
@@ -76,7 +71,7 @@ impl UserRepository for PostgresUserRepository {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete(&self, id: &UserId) -> Result<(), DomainError> {
|
||||
async fn delete(&self, id: &SystemId) -> Result<(), DomainError> {
|
||||
sqlx::query!("DELETE FROM users WHERE id = $1", *id.as_uuid())
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user