use std::sync::Arc;
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use uuid::Uuid;
use super::UseCase;
use crate::domain::entity::{FileVersion, FileVersionStatus};
use crate::infrastructure::persistence::FileVersionRepository;
#[derive(Debug, Clone)]
pub struct UpdateFileVersionInput {
pub id: Uuid,
pub size_bytes: Option<i64>,
pub file_id: Option<Uuid>,
pub version_number: Option<i32>,
pub version_type: Option<VersionType>,
pub storage_key: Option<String>,
pub storage_backend: Option<String>,
pub name: Option<String>,
pub mime_type: Option<String>,
pub checksum_md5: Option<String>,
pub checksum_sha256: Option<String>,
pub created_by_id: Option<Uuid>,
pub change_summary: Option<String>,
pub is_current: Option<bool>,
pub restored_from_id: Option<Uuid>,
pub expires_at: Option<DateTime<Utc>>,
pub status: Option<FileVersionStatus>,
pub deleted_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone)]
pub struct UpdateFileVersionOutput {
pub file_version: FileVersion,
}
pub struct UpdateFileVersionUseCase {
repository: Arc<FileVersionRepository>,
}
impl UpdateFileVersionUseCase {
pub fn new(repository: Arc<FileVersionRepository>) -> Self {
Self { repository }
}
}
#[async_trait]
impl UseCase for UpdateFileVersionUseCase {
type Input = UpdateFileVersionInput;
type Output = UpdateFileVersionOutput;
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!("FileVersion not found"))?;
entity.updated_at = Some(chrono::Utc::now());
if let Some(val) = input.size_bytes {
entity.size_bytes = val;
}
if let Some(val) = input.file_id {
entity.file_id = val;
}
if let Some(val) = input.version_number {
entity.version_number = val;
}
if let Some(val) = input.version_type {
entity.version_type = 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.name {
entity.name = val;
}
if let Some(val) = input.mime_type {
entity.mime_type = val;
}
if let Some(val) = input.checksum_md5 {
entity.checksum_md5 = val;
}
if let Some(val) = input.checksum_sha256 {
entity.checksum_sha256 = val;
}
if let Some(val) = input.created_by_id {
entity.created_by_id = val;
}
if let Some(val) = input.change_summary {
entity.change_summary = val;
}
if let Some(val) = input.is_current {
entity.is_current = val;
}
if let Some(val) = input.restored_from_id {
entity.restored_from_id = val;
}
if let Some(val) = input.expires_at {
entity.expires_at = val;
}
if let Some(val) = input.status {
entity.status = val;
}
if let Some(val) = input.deleted_at {
entity.deleted_at = val;
}
let updated = self.repository.update(entity).await?;
Ok(UpdateFileVersionOutput { file_version: updated })
}
}