backbone-bucket 0.2.1

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

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

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

/// Use case for updating an existing UserQuota
pub struct UpdateUserQuotaUseCase {
    repository: Arc<UserQuotaRepository>,
}

impl UpdateUserQuotaUseCase {
    pub fn new(repository: Arc<UserQuotaRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for UpdateUserQuotaUseCase {
    type Input = UpdateUserQuotaInput;
    type Output = UpdateUserQuotaOutput;

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

        // Apply updates
        if let Some(val) = input.user_id {
            entity.user_id = val;
        }
        if let Some(val) = input.limit_bytes {
            entity.limit_bytes = val;
        }
        if let Some(val) = input.used_bytes {
            entity.used_bytes = val;
        }
        if let Some(val) = input.file_count {
            entity.file_count = val;
        }
        if input.max_file_size.is_some() {
            entity.max_file_size = input.max_file_size;
        }
        if input.max_file_count.is_some() {
            entity.max_file_count = input.max_file_count;
        }
        if let Some(val) = input.tier {
            entity.tier = val;
        }
        if let Some(val) = input.warning_threshold_percent {
            entity.warning_threshold_percent = val;
        }
        if input.last_warning_sent_at.is_some() {
            entity.last_warning_sent_at = input.last_warning_sent_at;
        }
        if let Some(val) = input.peak_usage_bytes {
            entity.peak_usage_bytes = val;
        }
        if input.peak_usage_at.is_some() {
            entity.peak_usage_at = input.peak_usage_at;
        }
        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(UpdateUserQuotaOutput { user_quota: updated })
    }
}