backbone-bucket 0.2.1

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

/// Input for creating a UserQuota
#[derive(Debug, Clone)]
pub struct CreateUserQuotaInput {
    pub user_id: Uuid,
    pub limit_bytes: i64,
    pub used_bytes: i64,
    pub file_count: i32,
    pub max_file_size: Option<i64>,
    pub max_file_count: Option<i32>,
    pub tier: String,
    pub warning_threshold_percent: i32,
    pub last_warning_sent_at: Option<DateTime<Utc>>,
    pub peak_usage_bytes: i64,
    pub peak_usage_at: Option<DateTime<Utc>>,
    pub metadata: serde_json::Value,
}

/// Output after creating a UserQuota
#[derive(Debug, Clone)]
pub struct CreateUserQuotaOutput {
    pub user_quota: UserQuota,
}

/// Use case for creating a new UserQuota
pub struct CreateUserQuotaUseCase {
    repository: Arc<UserQuotaRepository>,
}

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

#[async_trait]
impl UseCase for CreateUserQuotaUseCase {
    type Input = CreateUserQuotaInput;
    type Output = CreateUserQuotaOutput;

    async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
        // Create entity from input
        let entity = UserQuota {
            id: None,
            user_id: input.user_id,
            limit_bytes: input.limit_bytes,
            used_bytes: input.used_bytes,
            file_count: input.file_count,
            max_file_size: input.max_file_size,
            max_file_count: input.max_file_count,
            tier: input.tier,
            warning_threshold_percent: input.warning_threshold_percent,
            last_warning_sent_at: input.last_warning_sent_at,
            peak_usage_bytes: input.peak_usage_bytes,
            peak_usage_at: input.peak_usage_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(CreateUserQuotaOutput { user_quota: created })
    }
}