use std::sync::Arc;
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use uuid::Uuid;
use super::UseCase;
use crate::domain::entity::ContentHash;
use crate::infrastructure::persistence::ContentHashRepository;
#[derive(Debug, Clone)]
pub struct UpdateContentHashInput {
pub id: Uuid,
pub hash: Option<String>,
pub size_bytes: Option<i64>,
pub storage_key: Option<String>,
pub storage_backend: Option<StorageBackend>,
pub reference_count: Option<i32>,
pub first_uploaded_at: Option<DateTime<Utc>>,
pub last_referenced_at: Option<DateTime<Utc>>,
pub fingerprint: Option<String>,
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone)]
pub struct UpdateContentHashOutput {
pub content_hash: ContentHash,
}
pub struct UpdateContentHashUseCase {
repository: Arc<ContentHashRepository>,
}
impl UpdateContentHashUseCase {
pub fn new(repository: Arc<ContentHashRepository>) -> Self {
Self { repository }
}
}
#[async_trait]
impl UseCase for UpdateContentHashUseCase {
type Input = UpdateContentHashInput;
type Output = UpdateContentHashOutput;
async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
let mut entity = self.repository.find_by_id(input.id).await?
.ok_or_else(|| anyhow!("ContentHash not found"))?;
if let Some(val) = input.hash {
entity.hash = val;
}
if let Some(val) = input.size_bytes {
entity.size_bytes = val;
}
if let Some(val) = input.storage_key {
entity.storage_key = val;
}
if let Some(val) = input.storage_backend {
entity.storage_backend = val;
}
if let Some(val) = input.reference_count {
entity.reference_count = val;
}
if let Some(val) = input.first_uploaded_at {
entity.first_uploaded_at = val;
}
if let Some(val) = input.last_referenced_at {
entity.last_referenced_at = val;
}
if input.fingerprint.is_some() {
entity.fingerprint = input.fingerprint;
}
if input.metadata.is_some() {
entity.metadata = input.metadata;
}
let updated = self.repository.update(entity).await?;
Ok(UpdateContentHashOutput { content_hash: updated })
}
}