backbone-bucket 0.2.0

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

/// Input for updating a FileLock
#[derive(Debug, Clone)]
pub struct UpdateFileLockInput {
    pub id: Uuid,
    pub file_id: Option<Uuid>,
    pub user_id: Option<Uuid>,
    pub locked_at: Option<DateTime<Utc>>,
    pub expires_at: Option<DateTime<Utc>>,
    pub refreshed_at: Option<DateTime<Utc>>,
    pub metadata: Option<serde_json::Value>,
}

/// Output after updating a FileLock
#[derive(Debug, Clone)]
pub struct UpdateFileLockOutput {
    pub file_lock: FileLock,
}

/// Use case for updating an existing FileLock
pub struct UpdateFileLockUseCase {
    repository: Arc<FileLockRepository>,
}

impl UpdateFileLockUseCase {
    pub fn new(repository: Arc<FileLockRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for UpdateFileLockUseCase {
    type Input = UpdateFileLockInput;
    type Output = UpdateFileLockOutput;

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

        // Apply updates
        if let Some(val) = input.file_id {
            entity.file_id = val;
        }
        if let Some(val) = input.user_id {
            entity.user_id = val;
        }
        if let Some(val) = input.locked_at {
            entity.locked_at = val;
        }
        if let Some(val) = input.expires_at {
            entity.expires_at = val;
        }
        if input.refreshed_at.is_some() {
            entity.refreshed_at = input.refreshed_at;
        }
        if input.metadata.is_some() {
            entity.metadata = input.metadata;
        }

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

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

        Ok(UpdateFileLockOutput { file_lock: updated })
    }
}