use std::sync::Arc;
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use uuid::Uuid;
use super::UseCase;
use crate::domain::entity::FileLock;
use crate::infrastructure::persistence::FileLockRepository;
#[derive(Debug, Clone)]
pub struct UpdateFileLockInput {
pub id: Uuid,
pub file_id: Option<Uuid>,
pub user_id: Option<Uuid>,
pub locked_at: Option<DateTime<Utc>>,
pub expires_at: Option<DateTime<Utc>>,
pub refreshed_at: Option<DateTime<Utc>>,
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone)]
pub struct UpdateFileLockOutput {
pub file_lock: FileLock,
}
pub struct UpdateFileLockUseCase {
repository: Arc<FileLockRepository>,
}
impl UpdateFileLockUseCase {
pub fn new(repository: Arc<FileLockRepository>) -> Self {
Self { repository }
}
}
#[async_trait]
impl UseCase for UpdateFileLockUseCase {
type Input = UpdateFileLockInput;
type Output = UpdateFileLockOutput;
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!("FileLock not found"))?;
if let Some(val) = input.file_id {
entity.file_id = val;
}
if let Some(val) = input.user_id {
entity.user_id = val;
}
if let Some(val) = input.locked_at {
entity.locked_at = val;
}
if let Some(val) = input.expires_at {
entity.expires_at = val;
}
if input.refreshed_at.is_some() {
entity.refreshed_at = input.refreshed_at;
}
if input.metadata.is_some() {
entity.metadata = input.metadata;
}
let updated = self.repository.update(entity).await?;
Ok(UpdateFileLockOutput { file_lock: updated })
}
}