backbone-bucket 0.2.1

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Commands for FileAccessLog aggregate
//!
//! Generated by backbone-schema. Do not edit manually.
//!
//! CQRS Commands represent intent to change state.

use async_trait::async_trait;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::domain::entity::FileAccessLog;
use crate::infrastructure::persistence::FileAccessLogRepository;
use super::CommandHandler;

// ============================================================================
// COMMAND TRAIT (DEPRECATED)
// ============================================================================

/// Command trait for FileAccessLog operations
///
/// **DEPRECATED**: Use the generic `CommandHandler<C>` trait from `mod.rs` instead.
/// This trait is kept for backwards compatibility.
#[deprecated(since = "0.2.0", note = "Use CommandHandler<C> trait instead")]
#[async_trait]
pub trait FileAccessLogCommand: Send + Sync {
    type Output;
    async fn execute(&self) -> Result<Self::Output>;
}

// ============================================================================
// CREATE COMMAND
// ============================================================================

/// Command to create a new file_access_log
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateFileAccessLogCommand {
    pub file_id: Uuid,
    pub user_id: Option<Uuid>,
    pub share_id: Option<Uuid>,
    pub action: AccessAction,
    pub ip_address: Option<String>,
    pub user_agent: Option<String>,
    pub accessed_at: DateTime<Utc>,
}

/// Handler for CreateFileAccessLogCommand
pub struct CreateFileAccessLogHandler<R: FileAccessLogRepository> {
    repository: std::sync::Arc<R>,
}

impl<R: FileAccessLogRepository> CreateFileAccessLogHandler<R> {
    pub fn new(repository: std::sync::Arc<R>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl<R: FileAccessLogRepository + 'static> CommandHandler<CreateFileAccessLogCommand> for CreateFileAccessLogHandler<R> {
    type Output = FileAccessLog;

    async fn handle(&self, cmd: CreateFileAccessLogCommand) -> Result<Self::Output> {
        let entity = FileAccessLog::builder()
            .id(Uuid::new_v4().to_string())
            .file_id(cmd.file_id)
            .user_id(cmd.user_id)
            .share_id(cmd.share_id)
            .action(cmd.action)
            .ip_address(cmd.ip_address)
            .user_agent(cmd.user_agent)
            .accessed_at(cmd.accessed_at)
            .build()?;

        self.repository.save(&entity).await
    }
}

// ============================================================================
// UPDATE COMMAND
// ============================================================================

/// Command to update an existing file_access_log
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateFileAccessLogCommand {
    pub id: String,
    pub file_id: Option<Uuid>,
    pub user_id: Option<Uuid>,
    pub share_id: Option<Uuid>,
    pub action: Option<AccessAction>,
    pub ip_address: Option<String>,
    pub user_agent: Option<String>,
    pub accessed_at: Option<DateTime<Utc>>,
}

/// Handler for UpdateFileAccessLogCommand
pub struct UpdateFileAccessLogHandler<R: FileAccessLogRepository> {
    repository: std::sync::Arc<R>,
}

impl<R: FileAccessLogRepository> UpdateFileAccessLogHandler<R> {
    pub fn new(repository: std::sync::Arc<R>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl<R: FileAccessLogRepository + 'static> CommandHandler<UpdateFileAccessLogCommand> for UpdateFileAccessLogHandler<R> {
    type Output = Option<FileAccessLog>;

    async fn handle(&self, cmd: UpdateFileAccessLogCommand) -> Result<Self::Output> {
        let existing = self.repository.find_by_id(&cmd.id).await?;

        let Some(mut entity) = existing else {
            return Ok(None);
        };

        // Apply updates
        if let Some(value) = cmd.file_id {
            entity.file_id = value;
        }
        entity.user_id = cmd.user_id;
        entity.share_id = cmd.share_id;
        if let Some(value) = cmd.action {
            entity.action = value;
        }
        entity.ip_address = cmd.ip_address;
        entity.user_agent = cmd.user_agent;
        if let Some(value) = cmd.accessed_at {
            entity.accessed_at = value;
        }

        self.repository.update(&cmd.id, &entity).await
    }
}

// ============================================================================
// DELETE COMMAND
// ============================================================================

/// Command to delete a file_access_log
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeleteFileAccessLogCommand {
    pub id: String,
    pub hard_delete: bool,
}

/// Handler for DeleteFileAccessLogCommand
pub struct DeleteFileAccessLogHandler<R: FileAccessLogRepository> {
    repository: std::sync::Arc<R>,
}

impl<R: FileAccessLogRepository> DeleteFileAccessLogHandler<R> {
    pub fn new(repository: std::sync::Arc<R>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl<R: FileAccessLogRepository + 'static> CommandHandler<DeleteFileAccessLogCommand> for DeleteFileAccessLogHandler<R> {
    type Output = bool;

    async fn handle(&self, cmd: DeleteFileAccessLogCommand) -> Result<Self::Output> {
        self.repository.delete(&cmd.id).await
    }
}

// ============================================================================
// CUSTOM COMMANDS
// ============================================================================

// <<< CUSTOM COMMANDS START >>>
// Add your custom commands here
// Example:
//
// #[derive(Debug, Clone, Serialize, Deserialize)]
// pub struct ActivateFileAccessLogCommand {
//     pub id: String,
// }
// <<< CUSTOM COMMANDS END >>>