import { ConflictException, Injectable, Logger, NotFoundException, } from "@nestjs/common"; import { FavoritesRepository } from "./repositories/favorites.repository"; @Injectable() export class FavoritesService { private readonly logger = new Logger(FavoritesService.name); constructor(private readonly favoritesRepository: FavoritesRepository) {} async addFavorite(userId: string, contentId: string) { this.logger.log(`Adding favorite: user ${userId}, content ${contentId}`); const content = await this.favoritesRepository.findContentById(contentId); if (!content) { throw new NotFoundException("Content not found"); } try { return await this.favoritesRepository.add(userId, contentId); } catch (_error) { throw new ConflictException("Content already in favorites"); } } async removeFavorite(userId: string, contentId: string) { this.logger.log(`Removing favorite: user ${userId}, content ${contentId}`); const result = await this.favoritesRepository.remove(userId, contentId); if (result.length === 0) { throw new NotFoundException("Favorite not found"); } return result[0]; } async getUserFavorites(userId: string, limit: number, offset: number) { return await this.favoritesRepository.findByUserId(userId, limit, offset); } }