backbone-bucket 0.2.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Update AccessLog 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::AccessLog;
use crate::infrastructure::persistence::AccessLogRepository;

/// Input for updating a AccessLog
#[derive(Debug, Clone)]
pub struct UpdateAccessLogInput {
    pub id: Uuid,
    pub file_id: Option<Uuid>,
    pub bucket_id: Option<Uuid>,
    pub action: Option<AccessAction>,
    pub user_id: Option<Uuid>,
    pub share_id: Option<Uuid>,
    pub is_owner: Option<bool>,
    pub is_shared: Option<bool>,
    pub is_public: Option<bool>,
    pub ip_address: Option<String>,
    pub user_agent: Option<String>,
    pub referer: Option<String>,
    pub country_code: Option<String>,
    pub city: Option<String>,
    pub bytes_transferred: Option<i64>,
    pub duration_ms: Option<i32>,
    pub success: Option<bool>,
    pub error_message: Option<String>,
    pub accessed_at: Option<DateTime<Utc>>,
    pub metadata: Option<serde_json::Value>,
}

/// Output after updating a AccessLog
#[derive(Debug, Clone)]
pub struct UpdateAccessLogOutput {
    pub access_log: AccessLog,
}

/// Use case for updating an existing AccessLog
pub struct UpdateAccessLogUseCase {
    repository: Arc<AccessLogRepository>,
}

impl UpdateAccessLogUseCase {
    pub fn new(repository: Arc<AccessLogRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for UpdateAccessLogUseCase {
    type Input = UpdateAccessLogInput;
    type Output = UpdateAccessLogOutput;

    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!("AccessLog not found"))?;

        // Apply updates
        if let Some(val) = input.file_id {
            entity.file_id = val;
        }
        if let Some(val) = input.bucket_id {
            entity.bucket_id = val;
        }
        if let Some(val) = input.action {
            entity.action = val;
        }
        if let Some(val) = input.user_id {
            entity.user_id = val;
        }
        if let Some(val) = input.share_id {
            entity.share_id = val;
        }
        if let Some(val) = input.is_owner {
            entity.is_owner = val;
        }
        if let Some(val) = input.is_shared {
            entity.is_shared = val;
        }
        if let Some(val) = input.is_public {
            entity.is_public = val;
        }
        if let Some(val) = input.ip_address {
            entity.ip_address = val;
        }
        if let Some(val) = input.user_agent {
            entity.user_agent = val;
        }
        if let Some(val) = input.referer {
            entity.referer = val;
        }
        if let Some(val) = input.country_code {
            entity.country_code = val;
        }
        if let Some(val) = input.city {
            entity.city = val;
        }
        if let Some(val) = input.bytes_transferred {
            entity.bytes_transferred = val;
        }
        if let Some(val) = input.duration_ms {
            entity.duration_ms = val;
        }
        if let Some(val) = input.success {
            entity.success = val;
        }
        if let Some(val) = input.error_message {
            entity.error_message = val;
        }
        if let Some(val) = input.accessed_at {
            entity.accessed_at = val;
        }
        if let Some(val) = input.metadata {
            entity.metadata = val;
        }

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

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

        Ok(UpdateAccessLogOutput { access_log: updated })
    }
}