use async_trait::async_trait;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::domain::entity::FileAccessLog;
use crate::infrastructure::persistence::FileAccessLogRepository;
use super::CommandHandler;
#[deprecated(since = "0.2.0", note = "Use CommandHandler<C> trait instead")]
#[async_trait]
pub trait FileAccessLogCommand: Send + Sync {
type Output;
async fn execute(&self) -> Result<Self::Output>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateFileAccessLogCommand {
pub file_id: Uuid,
pub user_id: Option<Uuid>,
pub share_id: Option<Uuid>,
pub action: AccessAction,
pub ip_address: Option<String>,
pub user_agent: Option<String>,
pub accessed_at: DateTime<Utc>,
}
pub struct CreateFileAccessLogHandler<R: FileAccessLogRepository> {
repository: std::sync::Arc<R>,
}
impl<R: FileAccessLogRepository> CreateFileAccessLogHandler<R> {
pub fn new(repository: std::sync::Arc<R>) -> Self {
Self { repository }
}
}
#[async_trait]
impl<R: FileAccessLogRepository + 'static> CommandHandler<CreateFileAccessLogCommand> for CreateFileAccessLogHandler<R> {
type Output = FileAccessLog;
async fn handle(&self, cmd: CreateFileAccessLogCommand) -> Result<Self::Output> {
let entity = FileAccessLog::builder()
.id(Uuid::new_v4().to_string())
.file_id(cmd.file_id)
.user_id(cmd.user_id)
.share_id(cmd.share_id)
.action(cmd.action)
.ip_address(cmd.ip_address)
.user_agent(cmd.user_agent)
.accessed_at(cmd.accessed_at)
.build()?;
self.repository.save(&entity).await
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateFileAccessLogCommand {
pub id: String,
pub file_id: Option<Uuid>,
pub user_id: Option<Uuid>,
pub share_id: Option<Uuid>,
pub action: Option<AccessAction>,
pub ip_address: Option<String>,
pub user_agent: Option<String>,
pub accessed_at: Option<DateTime<Utc>>,
}
pub struct UpdateFileAccessLogHandler<R: FileAccessLogRepository> {
repository: std::sync::Arc<R>,
}
impl<R: FileAccessLogRepository> UpdateFileAccessLogHandler<R> {
pub fn new(repository: std::sync::Arc<R>) -> Self {
Self { repository }
}
}
#[async_trait]
impl<R: FileAccessLogRepository + 'static> CommandHandler<UpdateFileAccessLogCommand> for UpdateFileAccessLogHandler<R> {
type Output = Option<FileAccessLog>;
async fn handle(&self, cmd: UpdateFileAccessLogCommand) -> Result<Self::Output> {
let existing = self.repository.find_by_id(&cmd.id).await?;
let Some(mut entity) = existing else {
return Ok(None);
};
if let Some(value) = cmd.file_id {
entity.file_id = value;
}
entity.user_id = cmd.user_id;
entity.share_id = cmd.share_id;
if let Some(value) = cmd.action {
entity.action = value;
}
entity.ip_address = cmd.ip_address;
entity.user_agent = cmd.user_agent;
if let Some(value) = cmd.accessed_at {
entity.accessed_at = value;
}
self.repository.update(&cmd.id, &entity).await
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeleteFileAccessLogCommand {
pub id: String,
pub hard_delete: bool,
}
pub struct DeleteFileAccessLogHandler<R: FileAccessLogRepository> {
repository: std::sync::Arc<R>,
}
impl<R: FileAccessLogRepository> DeleteFileAccessLogHandler<R> {
pub fn new(repository: std::sync::Arc<R>) -> Self {
Self { repository }
}
}
#[async_trait]
impl<R: FileAccessLogRepository + 'static> CommandHandler<DeleteFileAccessLogCommand> for DeleteFileAccessLogHandler<R> {
type Output = bool;
async fn handle(&self, cmd: DeleteFileAccessLogCommand) -> Result<Self::Output> {
self.repository.delete(&cmd.id).await
}
}