use backbone_messaging::DomainEvent;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::domain::entity::FileAccessLog;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event_type")]
pub enum FileAccessLogEvent {
Created(FileAccessLogCreatedEvent),
Updated(FileAccessLogUpdatedEvent),
PartiallyUpdated(FileAccessLogPartiallyUpdatedEvent),
Deleted(FileAccessLogDeletedEvent),
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",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileAccessLogCreatedEvent {
pub entity_id: String,
pub entity: FileAccessLog,
pub occurred_at: DateTime<Utc>,
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,
}
}
}
#[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,
}
}
}
#[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,
}
}
}
#[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,
}
}
}
#[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,
}
}
}
use backbone_messaging::EventBus;
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))
}
}