backbone-bucket 0.2.1

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Update FileAccessLog use case
//!
//! Generated by backbone-schema. Do not edit manually.

use std::sync::Arc;

use anyhow::{Result, anyhow};
use async_trait::async_trait;
use uuid::Uuid;

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

/// Input for updating a FileAccessLog
#[derive(Debug, Clone)]
pub struct UpdateFileAccessLogInput {
    pub id: Uuid,
    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>>,
}

/// Output after updating a FileAccessLog
#[derive(Debug, Clone)]
pub struct UpdateFileAccessLogOutput {
    pub file_access_log: FileAccessLog,
}

/// Use case for updating an existing FileAccessLog
pub struct UpdateFileAccessLogUseCase {
    repository: Arc<FileAccessLogRepository>,
}

impl UpdateFileAccessLogUseCase {
    pub fn new(repository: Arc<FileAccessLogRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for UpdateFileAccessLogUseCase {
    type Input = UpdateFileAccessLogInput;
    type Output = UpdateFileAccessLogOutput;

    async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
        // Fetch existing entity
        let mut entity = self.repository.find_by_id(input.id).await?
            .ok_or_else(|| anyhow!("FileAccessLog not found"))?;

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

        // <<< CUSTOM VALIDATION START >>>
        // Add custom validation logic here
        // <<< CUSTOM VALIDATION END >>>

        // Persist updates
        let updated = self.repository.update(entity).await?;

        Ok(UpdateFileAccessLogOutput { file_access_log: updated })
    }
}