import LibraryItem from '../infra/typeorm/entities/LibraryItem' ; import ILibraryRepository from '../repositories/ILibraryRepository' ; import { inject , injectable } from 'tsyringe' ; import IFilterLibrary from '../dtos/IFilterLibrary' ; import ICacheProvider from '@shared/container/providers/CacheProvider/models/ICacheProvider' ; @ injectable ( ) class ShowLibraryItemsService { constructor ( @ inject ( 'LibraryRepository' ) private libraryRepository , @ inject ( 'CacheProvider' ) private cacheProvider ) { } public async execute ( filters ) { const cacheName = `library-list:${ filters . type }-${ filters . author_id }-${ filters . category_id }-${ filters . page_size }-${ filters . current_page }-${ filters . order_by }` ; let libraryItems = await this . cacheProvider . recover < LibraryItem [ ] > ( cacheName ) ; if ( ! libraryItems ) { libraryItems = await this . libraryRepository . findAll ( filters ) ; this . cacheProvider . save ( cacheName , libraryItems ) ; } return libraryItems ; } } export default ShowLibraryItemsService ;