use std::sync::Arc;
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use uuid::Uuid;
use super::UseCase;
use crate::domain::entity::ConversionJob;
use crate::infrastructure::persistence::ConversionJobRepository;
#[derive(Debug, Clone)]
pub struct UpdateConversionJobInput {
pub id: Uuid,
pub source_file_id: Option<Uuid>,
pub target_format: Option<String>,
pub status: Option<ConversionStatus>,
pub conversion_options: Option<serde_json::Value>,
pub result_file_id: Option<Uuid>,
pub progress: Option<i32>,
pub error_message: Option<String>,
pub started_at: Option<DateTime<Utc>>,
pub completed_at: Option<DateTime<Utc>>,
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone)]
pub struct UpdateConversionJobOutput {
pub conversion_job: ConversionJob,
}
pub struct UpdateConversionJobUseCase {
repository: Arc<ConversionJobRepository>,
}
impl UpdateConversionJobUseCase {
pub fn new(repository: Arc<ConversionJobRepository>) -> Self {
Self { repository }
}
}
#[async_trait]
impl UseCase for UpdateConversionJobUseCase {
type Input = UpdateConversionJobInput;
type Output = UpdateConversionJobOutput;
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!("ConversionJob not found"))?;
if let Some(val) = input.source_file_id {
entity.source_file_id = val;
}
if let Some(val) = input.target_format {
entity.target_format = val;
}
if let Some(val) = input.status {
entity.status = val;
}
if input.conversion_options.is_some() {
entity.conversion_options = input.conversion_options;
}
if input.result_file_id.is_some() {
entity.result_file_id = input.result_file_id;
}
if let Some(val) = input.progress {
entity.progress = val;
}
if input.error_message.is_some() {
entity.error_message = input.error_message;
}
if input.started_at.is_some() {
entity.started_at = input.started_at;
}
if input.completed_at.is_some() {
entity.completed_at = input.completed_at;
}
if input.metadata.is_some() {
entity.metadata = input.metadata;
}
let updated = self.repository.update(entity).await?;
Ok(UpdateConversionJobOutput { conversion_job: updated })
}
}