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

147 lines
4.5 KiB
Rust
Raw Normal View History

use crate::application::dtos::favorites_dto::{
BatchFavoritesResult, BatchFavoritesStats, FavoriteItemDto,
};
2026-02-14 01:29:34 +01:00
use crate::application::ports::favorites_ports::{FavoritesRepositoryPort, FavoritesUseCase};
use crate::common::errors::{DomainError, ErrorKind, Result};
2025-04-02 03:43:44 +02:00
use async_trait::async_trait;
2026-02-14 01:29:34 +01:00
use std::sync::Arc;
use tracing::info;
2025-04-02 03:43:44 +02:00
/// Implementation of the FavoritesUseCase for managing user favorites.
///
/// Depends on `FavoritesRepositoryPort` (outbound port) instead of
/// accessing the database directly, following hexagonal architecture.
2025-04-02 03:43:44 +02:00
pub struct FavoritesService {
repo: Arc<dyn FavoritesRepositoryPort>,
2025-04-02 03:43:44 +02:00
}
impl FavoritesService {
/// Create a new FavoritesService with the given repository port
pub fn new(repo: Arc<dyn FavoritesRepositoryPort>) -> Self {
Self { repo }
2025-04-02 03:43:44 +02:00
}
}
#[async_trait]
impl FavoritesUseCase for FavoritesService {
/// Get all favorites for a user
async fn get_favorites(&self, user_id: &str) -> Result<Vec<FavoriteItemDto>> {
info!("Getting favorites for user: {}", user_id);
let favorites = self.repo.get_favorites(user_id).await?;
2026-02-14 01:29:34 +01:00
info!(
"Retrieved {} favorites for user {}",
favorites.len(),
user_id
);
2025-04-02 03:43:44 +02:00
Ok(favorites)
}
2025-04-02 03:43:44 +02:00
/// Add an item to user's favorites
async fn add_to_favorites(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<()> {
2026-02-14 01:29:34 +01:00
info!(
"Adding {} '{}' to favorites for user {}",
item_type, item_id, user_id
);
2025-04-02 03:43:44 +02:00
if item_type != "file" && item_type != "folder" {
return Err(DomainError::new(
ErrorKind::InvalidInput,
"Favorites",
"Item type must be 'file' or 'folder'",
2025-04-02 03:43:44 +02:00
));
}
self.repo.add_favorite(user_id, item_id, item_type).await?;
2026-02-14 01:29:34 +01:00
info!(
"Successfully added {} '{}' to favorites for user {}",
item_type, item_id, user_id
);
2025-04-02 03:43:44 +02:00
Ok(())
}
2025-04-02 03:43:44 +02:00
/// Remove an item from user's favorites
2026-02-14 01:29:34 +01:00
async fn remove_from_favorites(
&self,
user_id: &str,
item_id: &str,
item_type: &str,
) -> Result<bool> {
2025-04-02 03:43:44 +02:00
info!(
2026-02-14 01:29:34 +01:00
"Removing {} '{}' from favorites for user {}",
item_type, item_id, user_id
2025-04-02 03:43:44 +02:00
);
2026-02-14 01:29:34 +01:00
let removed = self
.repo
.remove_favorite(user_id, item_id, item_type)
.await?;
info!(
"{} {} '{}' from favorites for user {}",
if removed {
"Successfully removed"
} else {
"Did not find"
},
item_type,
item_id,
user_id
);
2025-04-02 03:43:44 +02:00
Ok(removed)
}
2025-04-02 03:43:44 +02:00
/// Check if an item is in user's favorites
async fn is_favorite(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<bool> {
2026-02-14 01:29:34 +01:00
info!(
"Checking if {} '{}' is favorite for user {}",
item_type, item_id, user_id
);
self.repo.is_favorite(user_id, item_id, item_type).await
2025-04-02 03:43:44 +02:00
}
async fn batch_add_to_favorites(
&self,
user_id: &str,
items: &[(String, String)],
) -> Result<BatchFavoritesResult> {
info!(
"Batch adding {} items to favorites for user {}",
items.len(),
user_id
);
// Validate all item types
for (item_id, item_type) in items {
if item_type != "file" && item_type != "folder" {
return Err(DomainError::new(
ErrorKind::InvalidInput,
"Favorites",
format!(
"Item type must be 'file' or 'folder' for item '{}'",
item_id
),
));
}
}
let requested = items.len();
let inserted = self.repo.add_favorites_batch(user_id, items).await?;
let already_existed = requested as u64 - inserted;
info!(
"Batch favorites for user {}: {} requested, {} inserted, {} already existed",
user_id, requested, inserted, already_existed
);
// Return the full enriched list so the client can replace its cache
let favorites = self.repo.get_favorites(user_id).await?;
Ok(BatchFavoritesResult {
stats: BatchFavoritesStats {
requested,
inserted,
already_existed,
},
favorites,
})
}
2026-02-14 01:29:34 +01:00
}