backbone-bucket 0.2.0

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

use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;

use super::UseCase;
use crate::domain::entity::FileLock;
use crate::infrastructure::persistence::FileLockRepository;

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

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

/// Use case for creating a new FileLock
pub struct CreateFileLockUseCase {
    repository: Arc<FileLockRepository>,
}

impl CreateFileLockUseCase {
    /// Create a new use case instance
    pub fn new(repository: Arc<FileLockRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for CreateFileLockUseCase {
    type Input = CreateFileLockInput;
    type Output = CreateFileLockOutput;

    async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
        // Create entity from input
        let entity = FileLock {
            id: None,
            file_id: input.file_id,
            user_id: input.user_id,
            locked_at: input.locked_at,
            expires_at: input.expires_at,
            refreshed_at: input.refreshed_at,
            metadata: input.metadata,
        };

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

        // Persist entity
        let created = self.repository.create(entity).await?;

        Ok(CreateFileLockOutput { file_lock: created })
    }
}