Files
Oxicloud/src/application/ports/favorites_ports.rs
T

78 lines
3.2 KiB
Rust
Raw Normal View History

use std::collections::HashSet;
use uuid::Uuid;
use crate::application::dtos::favorites_dto::{BatchFavoritesResult, FavoriteItemDto};
2026-02-14 01:29:34 +01:00
use crate::common::errors::Result;
2025-04-02 03:43:44 +02:00
/// Defines operations for managing user favorites
pub trait FavoritesUseCase: Send + Sync {
/// Get all favorites for a user
async fn get_favorites(&self, user_id: Uuid) -> Result<Vec<FavoriteItemDto>>;
2026-02-14 01:29:34 +01:00
2025-04-02 03:43:44 +02:00
/// Add an item to user's favorites
async fn add_to_favorites(&self, user_id: Uuid, item_id: &str, item_type: &str) -> Result<()>;
2026-02-14 01:29:34 +01:00
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: Uuid,
2026-02-14 01:29:34 +01:00
item_id: &str,
item_type: &str,
) -> Result<bool>;
2025-04-02 03:43:44 +02:00
/// Check if an item is in user's favorites
async fn is_favorite(&self, user_id: Uuid, item_id: &str, item_type: &str) -> Result<bool>;
/// Add multiple items to favorites in a single transaction.
/// Returns enriched favourites list so the client can replace its cache.
async fn batch_add_to_favorites(
&self,
user_id: Uuid,
items: &[(String, String)],
) -> Result<BatchFavoritesResult>;
/// Check which of the given item IDs are favorites for this user.
/// Returns the set of item_ids that are favorites.
async fn batch_check_favorites(
&self,
user_id: Uuid,
item_ids: &[(&str, &str)], // (item_id, item_type) pairs
) -> Result<HashSet<String>>;
}
// ─────────────────────────────────────────────────────
// Outbound port — persistence abstraction
// ─────────────────────────────────────────────────────
/// Secondary (outbound) port for favorites persistence.
///
/// Application services depend on this trait instead of
/// accessing `PgPool` directly. The concrete implementation
/// lives in `infrastructure::repositories::pg`.
pub trait FavoritesRepositoryPort: Send + Sync + 'static {
/// Gets all favorites for a user.
async fn get_favorites(&self, user_id: Uuid) -> Result<Vec<FavoriteItemDto>>;
/// Adds an item to favorites. Returns `Ok(())` if it already existed (idempotent).
async fn add_favorite(&self, user_id: Uuid, item_id: &str, item_type: &str) -> Result<()>;
/// Removes an item from favorites. Returns `true` if it existed.
async fn remove_favorite(&self, user_id: Uuid, item_id: &str, item_type: &str) -> Result<bool>;
/// Checks if an item is in favorites.
async fn is_favorite(&self, user_id: Uuid, item_id: &str, item_type: &str) -> Result<bool>;
/// Insert multiple items in a single transaction.
/// Returns the number of rows actually inserted (ignoring duplicates).
async fn add_favorites_batch(&self, user_id: Uuid, items: &[(String, String)]) -> Result<u64>;
/// Check which of the given item IDs are favorites for this user.
/// Returns the set of item_ids that are favorites.
async fn batch_check_favorites(
&self,
user_id: Uuid,
item_ids: &[(&str, &str)], // (item_id, item_type) pairs
) -> Result<HashSet<String>>;
2026-02-14 01:29:34 +01:00
}