2026-02-16 09:17:54 +01:00
|
|
|
use crate::application::dtos::favorites_dto::{BatchFavoritesResult, FavoriteItemDto};
|
2026-02-14 01:29:34 +01:00
|
|
|
use crate::common::errors::Result;
|
|
|
|
|
use async_trait::async_trait;
|
2025-04-02 03:43:44 +02:00
|
|
|
|
|
|
|
|
/// Defines operations for managing user favorites
|
|
|
|
|
#[async_trait]
|
|
|
|
|
pub trait FavoritesUseCase: Send + Sync {
|
|
|
|
|
/// Get all favorites for a user
|
|
|
|
|
async fn get_favorites(&self, user_id: &str) -> 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: &str, 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: &str,
|
|
|
|
|
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: &str, item_id: &str, item_type: &str) -> Result<bool>;
|
2026-02-16 09:17:54 +01:00
|
|
|
|
|
|
|
|
/// 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: &str,
|
|
|
|
|
items: &[(String, String)],
|
|
|
|
|
) -> Result<BatchFavoritesResult>;
|
2026-02-08 13:40:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─────────────────────────────────────────────────────
|
|
|
|
|
// Outbound port — persistence abstraction
|
|
|
|
|
// ─────────────────────────────────────────────────────
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Secondary (outbound) port for favorites persistence.
|
2026-02-08 13:40:23 +01:00
|
|
|
///
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Application services depend on this trait instead of
|
|
|
|
|
/// accessing `PgPool` directly. The concrete implementation
|
|
|
|
|
/// lives in `infrastructure::repositories::pg`.
|
2026-02-08 13:40:23 +01:00
|
|
|
#[async_trait]
|
|
|
|
|
pub trait FavoritesRepositoryPort: Send + Sync + 'static {
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Gets all favorites for a user.
|
2026-02-08 13:40:23 +01:00
|
|
|
async fn get_favorites(&self, user_id: &str) -> Result<Vec<FavoriteItemDto>>;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Adds an item to favorites. Returns `Ok(())` if it already existed (idempotent).
|
2026-02-08 13:40:23 +01:00
|
|
|
async fn add_favorite(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<()>;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Removes an item from favorites. Returns `true` if it existed.
|
2026-02-08 13:40:23 +01:00
|
|
|
async fn remove_favorite(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<bool>;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Checks if an item is in favorites.
|
2026-02-08 13:40:23 +01:00
|
|
|
async fn is_favorite(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<bool>;
|
2026-02-16 09:17:54 +01:00
|
|
|
|
|
|
|
|
/// Insert multiple items in a single transaction.
|
|
|
|
|
/// Returns the number of rows actually inserted (ignoring duplicates).
|
|
|
|
|
async fn add_favorites_batch(
|
|
|
|
|
&self,
|
|
|
|
|
user_id: &str,
|
|
|
|
|
items: &[(String, String)],
|
|
|
|
|
) -> Result<u64>;
|
2026-02-14 01:29:34 +01:00
|
|
|
}
|