backbone-bucket 0.2.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Domain events for FileAccessLog entity
//!
//! Generated by backbone-schema. Do not edit manually.
//!
//! These events are published after each CRUD operation.

use backbone_messaging::DomainEvent;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::domain::entity::FileAccessLog;

// ==========================================================================
// Domain Event Enum
// ==========================================================================

/// All domain events for FileAccessLog entity
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event_type")]
pub enum FileAccessLogEvent {
    /// Entity was created
    Created(FileAccessLogCreatedEvent),
    /// Entity was updated
    Updated(FileAccessLogUpdatedEvent),
    /// Entity was partially updated
    PartiallyUpdated(FileAccessLogPartiallyUpdatedEvent),
    /// Entity was deleted
    Deleted(FileAccessLogDeletedEvent),
    /// Bulk entities were created
    BulkCreated(FileAccessLogBulkCreatedEvent),
}

impl DomainEvent for FileAccessLogEvent {
    fn event_type(&self) -> &'static str {
        match self {
            Self::Created(_) => "file_access_log.created",
            Self::Updated(_) => "file_access_log.updated",
            Self::PartiallyUpdated(_) => "file_access_log.partially_updated",
            Self::Deleted(_) => "file_access_log.deleted",
            Self::BulkCreated(_) => "file_access_log.bulk_created",
        }
    }

    fn aggregate_id(&self) -> &str {
        match self {
            Self::Created(e) => &e.entity_id,
            Self::Updated(e) => &e.entity_id,
            Self::PartiallyUpdated(e) => &e.entity_id,
            Self::Deleted(e) => &e.entity_id,
            Self::BulkCreated(_) => "bulk",
        }
    }
}

// ==========================================================================
// Individual Event Structs
// ==========================================================================

/// Event emitted when a file_access_log is created
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileAccessLogCreatedEvent {
    /// ID of the created entity
    pub entity_id: String,
    /// The created entity
    pub entity: FileAccessLog,
    /// When the event occurred
    pub occurred_at: DateTime<Utc>,
    /// User who triggered the event (if auth enabled)
    pub triggered_by: Option<String>,
}

impl FileAccessLogCreatedEvent {
    pub fn new(entity: FileAccessLog, triggered_by: Option<String>) -> Self {
        let entity_id = entity.id.to_string();
        Self {
            entity_id,
            entity,
            occurred_at: Utc::now(),
            triggered_by,
        }
    }
}

/// Event emitted when a file_access_log is updated
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileAccessLogUpdatedEvent {
    pub entity_id: String,
    pub entity: FileAccessLog,
    pub occurred_at: DateTime<Utc>,
    pub triggered_by: Option<String>,
}

impl FileAccessLogUpdatedEvent {
    pub fn new(entity: FileAccessLog, triggered_by: Option<String>) -> Self {
        let entity_id = entity.id.to_string();
        Self {
            entity_id,
            entity,
            occurred_at: Utc::now(),
            triggered_by,
        }
    }
}

/// Event emitted when a file_access_log is partially updated
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileAccessLogPartiallyUpdatedEvent {
    pub entity_id: String,
    pub updated_fields: Vec<String>,
    pub occurred_at: DateTime<Utc>,
    pub triggered_by: Option<String>,
}

impl FileAccessLogPartiallyUpdatedEvent {
    pub fn new(entity_id: String, updated_fields: Vec<String>, triggered_by: Option<String>) -> Self {
        Self {
            entity_id,
            updated_fields,
            occurred_at: Utc::now(),
            triggered_by,
        }
    }
}

/// Event emitted when a file_access_log is deleted
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileAccessLogDeletedEvent {
    pub entity_id: String,
    pub occurred_at: DateTime<Utc>,
    pub triggered_by: Option<String>,
}

impl FileAccessLogDeletedEvent {
    pub fn new(entity_id: String, triggered_by: Option<String>) -> Self {
        Self {
            entity_id,
            occurred_at: Utc::now(),
            triggered_by,
        }
    }
}

/// Event emitted when multiple file_access_log entities are bulk created
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileAccessLogBulkCreatedEvent {
    pub entity_ids: Vec<String>,
    pub count: usize,
    pub occurred_at: DateTime<Utc>,
    pub triggered_by: Option<String>,
}

impl FileAccessLogBulkCreatedEvent {
    pub fn new(entities: &[FileAccessLog], triggered_by: Option<String>) -> Self {
        let entity_ids: Vec<String> = entities
            .iter()
            .map(|e| e.id.to_string())
            .collect();
        let count = entities.len();
        Self {
            entity_ids,
            count,
            occurred_at: Utc::now(),
            triggered_by,
        }
    }
}

// ==========================================================================
// Event Publishing Helpers
// ==========================================================================

use backbone_messaging::EventBus;

/// Helper to publish file_access_log events
pub struct FileAccessLogEventPublisher {
    bus: EventBus<FileAccessLogEvent>,
}

impl FileAccessLogEventPublisher {
    pub fn new(bus: EventBus<FileAccessLogEvent>) -> Self {
        Self { bus }
    }

    pub async fn publish_created(&self, entity: FileAccessLog, triggered_by: Option<String>) -> anyhow::Result<()> {
        let event = FileAccessLogEvent::Created(FileAccessLogCreatedEvent::new(entity, triggered_by));
        self.bus.publish(event).await.map_err(|e| anyhow::anyhow!(e))
    }

    pub async fn publish_updated(&self, entity: FileAccessLog, triggered_by: Option<String>) -> anyhow::Result<()> {
        let event = FileAccessLogEvent::Updated(FileAccessLogUpdatedEvent::new(entity, triggered_by));
        self.bus.publish(event).await.map_err(|e| anyhow::anyhow!(e))
    }

    pub async fn publish_deleted(&self, entity_id: String, triggered_by: Option<String>) -> anyhow::Result<()> {
        let event = FileAccessLogEvent::Deleted(FileAccessLogDeletedEvent::new(entity_id, triggered_by));
        self.bus.publish(event).await.map_err(|e| anyhow::anyhow!(e))
    }

    pub async fn publish_bulk_created(&self, entities: &[FileAccessLog], triggered_by: Option<String>) -> anyhow::Result<()> {
        let event = FileAccessLogEvent::BulkCreated(FileAccessLogBulkCreatedEvent::new(entities, triggered_by));
        self.bus.publish(event).await.map_err(|e| anyhow::anyhow!(e))
    }
}