use std::sync::Arc;
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use uuid::Uuid;
use super::UseCase;
use crate::domain::entity::Thumbnail;
use crate::infrastructure::persistence::ThumbnailRepository;
#[derive(Debug, Clone)]
pub struct UpdateThumbnailInput {
pub id: Uuid,
pub deleted_at: Option<DateTime<Utc>>,
pub file_id: Option<Uuid>,
pub size: Option<ThumbnailSize>,
pub width: Option<i32>,
pub height: Option<i32>,
pub storage_key: Option<String>,
pub storage_backend: Option<String>,
pub mime_type: Option<String>,
pub format: Option<String>,
pub quality: Option<i32>,
pub size_bytes: Option<i64>,
pub generated_at: Option<DateTime<Utc>>,
pub generation_time_ms: Option<i32>,
pub source_version: Option<i32>,
pub cdn_url: Option<String>,
pub cache_expires_at: Option<DateTime<Utc>>,
pub is_stale: Option<bool>,
}
#[derive(Debug, Clone)]
pub struct UpdateThumbnailOutput {
pub thumbnail: Thumbnail,
}
pub struct UpdateThumbnailUseCase {
repository: Arc<ThumbnailRepository>,
}
impl UpdateThumbnailUseCase {
pub fn new(repository: Arc<ThumbnailRepository>) -> Self {
Self { repository }
}
}
#[async_trait]
impl UseCase for UpdateThumbnailUseCase {
type Input = UpdateThumbnailInput;
type Output = UpdateThumbnailOutput;
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!("Thumbnail not found"))?;
entity.updated_at = Some(chrono::Utc::now());
if input.deleted_at.is_some() {
entity.deleted_at = input.deleted_at;
}
if let Some(val) = input.file_id {
entity.file_id = val;
}
if let Some(val) = input.size {
entity.size = val;
}
if let Some(val) = input.width {
entity.width = val;
}
if let Some(val) = input.height {
entity.height = 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.mime_type {
entity.mime_type = val;
}
if let Some(val) = input.format {
entity.format = val;
}
if let Some(val) = input.quality {
entity.quality = val;
}
if let Some(val) = input.size_bytes {
entity.size_bytes = val;
}
if let Some(val) = input.generated_at {
entity.generated_at = val;
}
if let Some(val) = input.generation_time_ms {
entity.generation_time_ms = val;
}
if let Some(val) = input.source_version {
entity.source_version = val;
}
if let Some(val) = input.cdn_url {
entity.cdn_url = val;
}
if let Some(val) = input.cache_expires_at {
entity.cache_expires_at = val;
}
if let Some(val) = input.is_stale {
entity.is_stale = val;
}
let updated = self.repository.update(entity).await?;
Ok(UpdateThumbnailOutput { thumbnail: updated })
}
}