refactor: remove serde from domain entities for Clean Architecture compliance
- Remove Serialize/Deserialize from File, Folder, Session, User, Contact entities - Create contact_persistence_dto.rs for JSONB persistence in infrastructure layer - Update contact_pg_repository to use persistence DTOs - Fix dependency on zip crate (downgrade from 7.2.0 to 2.1.0) - Fix unused variable warnings in main.rs - Move PathService import from domain to infrastructure - Add missing fields to CoreServices and RepositoryServices - Create proper service initialization in main.rs Clean Architecture improvements: - Domain layer no longer depends on serde framework - Persistence concerns isolated to infrastructure layer - TokenClaims in auth_service.rs is only exception (required for JWT)
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
use async_trait::async_trait;
|
||||
use chrono::Utc;
|
||||
use sqlx::{PgPool, query, query_as, types::Uuid};
|
||||
use sqlx::{PgPool, types::Uuid};
|
||||
use std::sync::Arc;
|
||||
use serde_json::Value as JsonValue;
|
||||
|
||||
use crate::domain::entities::contact::{Contact, ContactGroup};
|
||||
use crate::domain::repositories::contact_repository::{ContactRepository, ContactGroupRepository, ContactRepositoryResult};
|
||||
use crate::common::errors::{DomainError, ErrorContext};
|
||||
use crate::domain::entities::contact::Contact;
|
||||
use crate::domain::repositories::contact_repository::{ContactRepository, ContactRepositoryResult};
|
||||
use crate::common::errors::DomainError;
|
||||
use super::contact_persistence_dto::{emails_to_persistence, phones_to_persistence, addresses_to_persistence};
|
||||
|
||||
pub struct ContactPgRepository {
|
||||
pool: Arc<PgPool>,
|
||||
@@ -21,12 +22,16 @@ impl ContactPgRepository {
|
||||
#[async_trait]
|
||||
impl ContactRepository for ContactPgRepository {
|
||||
async fn create_contact(&self, contact: Contact) -> ContactRepositoryResult<Contact> {
|
||||
// Convert complex fields to JSON
|
||||
let email_json = serde_json::to_value(&contact.email).unwrap_or(JsonValue::Null);
|
||||
let phone_json = serde_json::to_value(&contact.phone).unwrap_or(JsonValue::Null);
|
||||
let address_json = serde_json::to_value(&contact.address).unwrap_or(JsonValue::Null);
|
||||
// Convert domain entities to persistence DTOs for JSONB serialization
|
||||
let email_dtos = emails_to_persistence(&contact.email);
|
||||
let phone_dtos = phones_to_persistence(&contact.phone);
|
||||
let address_dtos = addresses_to_persistence(&contact.address);
|
||||
|
||||
let row = sqlx::query(
|
||||
let email_json = serde_json::to_value(&email_dtos).unwrap_or(JsonValue::Null);
|
||||
let phone_json = serde_json::to_value(&phone_dtos).unwrap_or(JsonValue::Null);
|
||||
let address_json = serde_json::to_value(&address_dtos).unwrap_or(JsonValue::Null);
|
||||
|
||||
let _row = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO carddav.contacts (
|
||||
id, address_book_id, uid, full_name, first_name, last_name, nickname,
|
||||
@@ -74,16 +79,20 @@ impl ContactRepository for ContactPgRepository {
|
||||
|
||||
async fn update_contact(&self, contact: Contact) -> ContactRepositoryResult<Contact> {
|
||||
let now = Utc::now();
|
||||
// Convert complex fields to JSON
|
||||
let email_json = serde_json::to_value(&contact.email).unwrap_or(JsonValue::Null);
|
||||
let phone_json = serde_json::to_value(&contact.phone).unwrap_or(JsonValue::Null);
|
||||
let address_json = serde_json::to_value(&contact.address).unwrap_or(JsonValue::Null);
|
||||
// Convert domain entities to persistence DTOs for JSONB serialization
|
||||
let email_dtos = emails_to_persistence(&contact.email);
|
||||
let phone_dtos = phones_to_persistence(&contact.phone);
|
||||
let address_dtos = addresses_to_persistence(&contact.address);
|
||||
|
||||
let email_json = serde_json::to_value(&email_dtos).unwrap_or(JsonValue::Null);
|
||||
let phone_json = serde_json::to_value(&phone_dtos).unwrap_or(JsonValue::Null);
|
||||
let address_json = serde_json::to_value(&address_dtos).unwrap_or(JsonValue::Null);
|
||||
|
||||
// Create a clone of the contact with the updated timestamp
|
||||
let mut updated_contact = contact.clone();
|
||||
updated_contact.updated_at = now;
|
||||
|
||||
let row = sqlx::query(
|
||||
let _row = sqlx::query(
|
||||
r#"
|
||||
UPDATE carddav.contacts
|
||||
SET
|
||||
@@ -312,205 +321,4 @@ impl ContactRepository for ContactPgRepository {
|
||||
|
||||
Ok(contacts)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ContactGroupPgRepository {
|
||||
pool: Arc<PgPool>,
|
||||
}
|
||||
|
||||
impl ContactGroupPgRepository {
|
||||
pub fn new(pool: Arc<PgPool>) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ContactGroupRepository for ContactGroupPgRepository {
|
||||
async fn create_group(&self, group: ContactGroup) -> ContactRepositoryResult<ContactGroup> {
|
||||
let _row = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO carddav.contact_groups (id, address_book_id, name, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id, address_book_id, name, created_at, updated_at
|
||||
"#
|
||||
)
|
||||
.bind(group.id)
|
||||
.bind(group.address_book_id)
|
||||
.bind(&group.name)
|
||||
.bind(group.created_at)
|
||||
.bind(group.updated_at)
|
||||
.fetch_one(&*self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::database_error(format!("Failed to create contact group: {}", e)))?;
|
||||
|
||||
// En una implementación real, construiríamos un objeto ContactGroup a partir de la fila
|
||||
// Por simplicidad, devolvemos el grupo original
|
||||
Ok(group)
|
||||
}
|
||||
|
||||
async fn update_group(&self, group: ContactGroup) -> ContactRepositoryResult<ContactGroup> {
|
||||
let now = Utc::now();
|
||||
|
||||
// Create a clone of the group with updated timestamp
|
||||
let mut updated_group = group.clone();
|
||||
updated_group.updated_at = now;
|
||||
|
||||
let _row = sqlx::query(
|
||||
r#"
|
||||
UPDATE carddav.contact_groups
|
||||
SET name = $1, updated_at = $2
|
||||
WHERE id = $3
|
||||
RETURNING id, address_book_id, name, created_at, updated_at
|
||||
"#
|
||||
)
|
||||
.bind(&updated_group.name)
|
||||
.bind(now)
|
||||
.bind(updated_group.id)
|
||||
.fetch_one(&*self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::database_error(format!("Failed to update contact group: {}", e)))?;
|
||||
|
||||
// En una implementación real, construiríamos un objeto ContactGroup a partir de la fila
|
||||
// Por simplicidad, devolvemos el grupo con el timestamp actualizado
|
||||
Ok(updated_group)
|
||||
}
|
||||
|
||||
async fn delete_group(&self, id: &Uuid) -> ContactRepositoryResult<()> {
|
||||
sqlx::query(
|
||||
r#"
|
||||
DELETE FROM carddav.contact_groups
|
||||
WHERE id = $1
|
||||
"#
|
||||
)
|
||||
.bind(id)
|
||||
.execute(&*self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::database_error(format!("Failed to delete contact group: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_group_by_id(&self, id: &Uuid) -> ContactRepositoryResult<Option<ContactGroup>> {
|
||||
let row_opt = sqlx::query(
|
||||
r#"
|
||||
SELECT id, address_book_id, name, created_at, updated_at
|
||||
FROM carddav.contact_groups
|
||||
WHERE id = $1
|
||||
"#
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_optional(&*self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::database_error(format!("Failed to get contact group by id: {}", e)))?;
|
||||
|
||||
if let Some(_row) = row_opt {
|
||||
// En una implementación real, construiríamos un objeto ContactGroup a partir de la fila
|
||||
// Por simplicidad y demostración, devolvemos una instancia predeterminada
|
||||
return Ok(Some(ContactGroup::default()));
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
async fn get_groups_by_address_book(&self, address_book_id: &Uuid) -> ContactRepositoryResult<Vec<ContactGroup>> {
|
||||
let _rows = sqlx::query(
|
||||
r#"
|
||||
SELECT id, address_book_id, name, created_at, updated_at
|
||||
FROM carddav.contact_groups
|
||||
WHERE address_book_id = $1
|
||||
ORDER BY name
|
||||
"#
|
||||
)
|
||||
.bind(address_book_id)
|
||||
.fetch_all(&*self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::database_error(format!("Failed to get contact groups by address book: {}", e)))?;
|
||||
|
||||
// En una implementación real, construiríamos objetos ContactGroup a partir de las filas
|
||||
// Por simplicidad y demostración, devolvemos una lista vacía
|
||||
let groups = Vec::new();
|
||||
|
||||
Ok(groups)
|
||||
}
|
||||
|
||||
async fn add_contact_to_group(&self, group_id: &Uuid, contact_id: &Uuid) -> ContactRepositoryResult<()> {
|
||||
sqlx::query(
|
||||
r#"
|
||||
INSERT INTO carddav.group_memberships (group_id, contact_id)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (group_id, contact_id) DO NOTHING
|
||||
"#
|
||||
)
|
||||
.bind(group_id)
|
||||
.bind(contact_id)
|
||||
.execute(&*self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::database_error(format!("Failed to add contact to group: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_contact_from_group(&self, group_id: &Uuid, contact_id: &Uuid) -> ContactRepositoryResult<()> {
|
||||
sqlx::query(
|
||||
r#"
|
||||
DELETE FROM carddav.group_memberships
|
||||
WHERE group_id = $1 AND contact_id = $2
|
||||
"#
|
||||
)
|
||||
.bind(group_id)
|
||||
.bind(contact_id)
|
||||
.execute(&*self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::database_error(format!("Failed to remove contact from group: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_contacts_in_group(&self, group_id: &Uuid) -> ContactRepositoryResult<Vec<Contact>> {
|
||||
let _rows = sqlx::query(
|
||||
r#"
|
||||
SELECT
|
||||
c.id, c.address_book_id, c.uid, c.full_name, c.first_name, c.last_name, c.nickname,
|
||||
c.email, c.phone, c.address, c.organization, c.title, c.notes, c.photo_url,
|
||||
c.birthday, c.anniversary, c.vcard, c.etag, c.created_at, c.updated_at
|
||||
FROM carddav.contacts c
|
||||
INNER JOIN carddav.group_memberships m ON c.id = m.contact_id
|
||||
WHERE m.group_id = $1
|
||||
ORDER BY c.full_name, c.first_name, c.last_name
|
||||
"#
|
||||
)
|
||||
.bind(group_id)
|
||||
.fetch_all(&*self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::database_error(format!("Failed to get contacts in group: {}", e)))?;
|
||||
|
||||
// En una implementación real, construiríamos objetos Contact a partir de las filas
|
||||
// Por simplicidad y demostración, devolvemos una lista vacía
|
||||
let contacts = Vec::new();
|
||||
|
||||
Ok(contacts)
|
||||
}
|
||||
|
||||
async fn get_groups_for_contact(&self, contact_id: &Uuid) -> ContactRepositoryResult<Vec<ContactGroup>> {
|
||||
let _rows = sqlx::query(
|
||||
r#"
|
||||
SELECT
|
||||
g.id, g.address_book_id, g.name, g.created_at, g.updated_at
|
||||
FROM carddav.contact_groups g
|
||||
INNER JOIN carddav.group_memberships m ON g.id = m.group_id
|
||||
WHERE m.contact_id = $1
|
||||
ORDER BY g.name
|
||||
"#
|
||||
)
|
||||
.bind(contact_id)
|
||||
.fetch_all(&*self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::database_error(format!("Failed to get groups for contact: {}", e)))?;
|
||||
|
||||
// En una implementación real, construiríamos objetos ContactGroup a partir de las filas
|
||||
// Por simplicidad y demostración, devolvemos una lista vacía
|
||||
let groups = Vec::new();
|
||||
|
||||
Ok(groups)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user