Files
Oxicloud/src/application/services/recent_service.rs
T

76 lines
2.9 KiB
Rust
Raw Normal View History

2025-04-02 05:08:30 +02:00
use std::sync::Arc;
use async_trait::async_trait;
use tracing::info;
2025-04-02 05:08:30 +02:00
use crate::common::errors::{Result, DomainError, ErrorKind};
use crate::application::ports::recent_ports::{RecentItemsUseCase, RecentItemsRepositoryPort};
2025-04-02 05:08:30 +02:00
use crate::application::dtos::recent_dto::RecentItemDto;
/// Implementation of the use case for managing recent items.
///
/// Depends on `RecentItemsRepositoryPort` (outbound port) instead
/// of accessing `PgPool` directly, following the hexagonal architecture.
2025-04-02 05:08:30 +02:00
pub struct RecentService {
repo: Arc<dyn RecentItemsRepositoryPort>,
max_recent_items: i32,
2025-04-02 05:08:30 +02:00
}
impl RecentService {
/// Create a new recent items service
pub fn new(repo: Arc<dyn RecentItemsRepositoryPort>, max_recent_items: i32) -> Self {
Self {
repo,
max_recent_items: max_recent_items.max(1).min(100),
2025-04-02 05:08:30 +02:00
}
}
}
#[async_trait]
impl RecentItemsUseCase for RecentService {
/// Get recent items for a user
2025-04-02 05:08:30 +02:00
async fn get_recent_items(&self, user_id: &str, limit: Option<i32>) -> Result<Vec<RecentItemDto>> {
info!("Getting recent items for user: {}", user_id);
2025-04-02 05:08:30 +02:00
let limit_value = limit.unwrap_or(self.max_recent_items).min(self.max_recent_items);
let items = self.repo.get_recent_items(user_id, limit_value).await?;
info!("Retrieved {} recent items for user {}", items.len(), user_id);
Ok(items)
2025-04-02 05:08:30 +02:00
}
/// Record access to an item
2025-04-02 05:08:30 +02:00
async fn record_item_access(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<()> {
info!("Recording access to {} '{}' for user {}", item_type, item_id, user_id);
2025-04-02 05:08:30 +02:00
if item_type != "file" && item_type != "folder" {
return Err(DomainError::new(
ErrorKind::InvalidInput,
"RecentItems",
"Item type must be 'file' or 'folder'",
2025-04-02 05:08:30 +02:00
));
}
self.repo.upsert_access(user_id, item_id, item_type).await?;
self.repo.prune(user_id, self.max_recent_items).await?;
info!("Successfully recorded access to {} '{}' for user {}", item_type, item_id, user_id);
2025-04-02 05:08:30 +02:00
Ok(())
}
/// Remove an item from recent
2025-04-02 05:08:30 +02:00
async fn remove_from_recent(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<bool> {
info!("Removing {} '{}' from recent for user {}", item_type, item_id, user_id);
let removed = self.repo.remove_item(user_id, item_id, item_type).await?;
2025-04-02 05:08:30 +02:00
info!(
"{} {} '{}' de recientes para usuario {}",
if removed { "Successfully removed" } else { "Not found" },
item_type, item_id, user_id
2025-04-02 05:08:30 +02:00
);
Ok(removed)
}
/// Clear all recent items
2025-04-02 05:08:30 +02:00
async fn clear_recent_items(&self, user_id: &str) -> Result<()> {
info!("Clearing all recent items for user {}", user_id);
self.repo.clear_all(user_id).await?;
info!("Cleared all recent items for user {}", user_id);
2025-04-02 05:08:30 +02:00
Ok(())
}
}