use std::sync::Arc;
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use uuid::Uuid;
use super::UseCase;
use crate::domain::entity::AccessLog;
use crate::infrastructure::persistence::AccessLogRepository;
#[derive(Debug, Clone)]
pub struct UpdateAccessLogInput {
pub id: Uuid,
pub file_id: Option<Uuid>,
pub bucket_id: Option<Uuid>,
pub action: Option<AccessAction>,
pub user_id: Option<Uuid>,
pub share_id: Option<Uuid>,
pub is_owner: Option<bool>,
pub is_shared: Option<bool>,
pub is_public: Option<bool>,
pub ip_address: Option<String>,
pub user_agent: Option<String>,
pub referer: Option<String>,
pub country_code: Option<String>,
pub city: Option<String>,
pub bytes_transferred: Option<i64>,
pub duration_ms: Option<i32>,
pub success: Option<bool>,
pub error_message: Option<String>,
pub accessed_at: Option<DateTime<Utc>>,
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone)]
pub struct UpdateAccessLogOutput {
pub access_log: AccessLog,
}
pub struct UpdateAccessLogUseCase {
repository: Arc<AccessLogRepository>,
}
impl UpdateAccessLogUseCase {
pub fn new(repository: Arc<AccessLogRepository>) -> Self {
Self { repository }
}
}
#[async_trait]
impl UseCase for UpdateAccessLogUseCase {
type Input = UpdateAccessLogInput;
type Output = UpdateAccessLogOutput;
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!("AccessLog not found"))?;
if let Some(val) = input.file_id {
entity.file_id = val;
}
if let Some(val) = input.bucket_id {
entity.bucket_id = val;
}
if let Some(val) = input.action {
entity.action = val;
}
if let Some(val) = input.user_id {
entity.user_id = val;
}
if let Some(val) = input.share_id {
entity.share_id = val;
}
if let Some(val) = input.is_owner {
entity.is_owner = val;
}
if let Some(val) = input.is_shared {
entity.is_shared = val;
}
if let Some(val) = input.is_public {
entity.is_public = val;
}
if let Some(val) = input.ip_address {
entity.ip_address = val;
}
if let Some(val) = input.user_agent {
entity.user_agent = val;
}
if let Some(val) = input.referer {
entity.referer = val;
}
if let Some(val) = input.country_code {
entity.country_code = val;
}
if let Some(val) = input.city {
entity.city = val;
}
if let Some(val) = input.bytes_transferred {
entity.bytes_transferred = val;
}
if let Some(val) = input.duration_ms {
entity.duration_ms = val;
}
if let Some(val) = input.success {
entity.success = val;
}
if let Some(val) = input.error_message {
entity.error_message = val;
}
if let Some(val) = input.accessed_at {
entity.accessed_at = val;
}
if let Some(val) = input.metadata {
entity.metadata = val;
}
let updated = self.repository.update(entity).await?;
Ok(UpdateAccessLogOutput { access_log: updated })
}
}