backbone-bucket 0.2.1

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

/// Input for updating a FileComment
#[derive(Debug, Clone)]
pub struct UpdateFileCommentInput {
    pub id: Uuid,
    pub file_id: Option<Uuid>,
    pub user_id: Option<Uuid>,
    pub parent_id: Option<Uuid>,
    pub content: Option<String>,
    pub annotation_region: Option<serde_json::Value>,
    pub mentions: Option<Vec<Uuid>>,
    pub resolved: Option<bool>,
    pub resolved_by: Option<Uuid>,
    pub resolved_at: Option<DateTime<Utc>>,
    pub metadata: Option<serde_json::Value>,
}

/// Output after updating a FileComment
#[derive(Debug, Clone)]
pub struct UpdateFileCommentOutput {
    pub file_comment: FileComment,
}

/// Use case for updating an existing FileComment
pub struct UpdateFileCommentUseCase {
    repository: Arc<FileCommentRepository>,
}

impl UpdateFileCommentUseCase {
    pub fn new(repository: Arc<FileCommentRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for UpdateFileCommentUseCase {
    type Input = UpdateFileCommentInput;
    type Output = UpdateFileCommentOutput;

    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!("FileComment 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 input.parent_id.is_some() {
            entity.parent_id = input.parent_id;
        }
        if let Some(val) = input.content {
            entity.content = val;
        }
        if input.annotation_region.is_some() {
            entity.annotation_region = input.annotation_region;
        }
        if let Some(val) = input.mentions {
            entity.mentions = val;
        }
        if let Some(val) = input.resolved {
            entity.resolved = val;
        }
        if input.resolved_by.is_some() {
            entity.resolved_by = input.resolved_by;
        }
        if input.resolved_at.is_some() {
            entity.resolved_at = input.resolved_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(UpdateFileCommentOutput { file_comment: updated })
    }
}