import { IFeedRepository } from '../repositories/FeedRepository.js'; import { IFeed, ICreateFeedDto, IUpdateFeedDto, IFeedQuery, IPaginatedResponse, NewsSource } from '../types/Feed.js'; export interface IFeedService { createFeed(feedData: ICreateFeedDto): Promise; getFeedById(id: string): Promise; getAllFeeds(query?: IFeedQuery): Promise>; updateFeed(id: string, updateData: IUpdateFeedDto): Promise; deleteFeed(id: string): Promise; getTodaysFrontPageNews(): Promise; getFeedsBySource(source: NewsSource, limit?: number): Promise; } export class FeedService implements IFeedService { constructor(private feedRepository: IFeedRepository) {} async createFeed(feedData: ICreateFeedDto): Promise { const existingFeed = await this.feedRepository.findByUrl(feedData.url); if (existingFeed) { throw new Error('A feed with this URL already exists'); } return await this.feedRepository.create(feedData); } async getFeedById(id: string): Promise { return await this.feedRepository.findById(id); } async getAllFeeds(query: IFeedQuery = {}): Promise> { const sanitizedQuery = { ...query, limit: query.limit || 20, page: query.page || 1 }; return await this.feedRepository.findAll(sanitizedQuery); } async updateFeed(id: string, updateData: IUpdateFeedDto): Promise { if (updateData.url) { const existingFeed = await this.feedRepository.findByUrl(updateData.url); if (existingFeed && existingFeed._id !== id) { throw new Error('A feed with this URL already exists'); } } return await this.feedRepository.update(id, updateData); } async deleteFeed(id: string): Promise { return await this.feedRepository.delete(id); } async getTodaysFrontPageNews(): Promise { const [elPaisFeeds, elMundoFeeds] = await Promise.all([ this.feedRepository.findTodaysFrontPage(NewsSource.EL_PAIS), this.feedRepository.findTodaysFrontPage(NewsSource.EL_MUNDO) ]); return [...elPaisFeeds, ...elMundoFeeds]; } async getFeedsBySource(source: NewsSource, limit: number = 10): Promise { return await this.feedRepository.findBySource(source); } }