@@ -13,17 +13,18 @@ import { GameRepositoryFindOneDto } from "./dto/game-repository-find-one.dto";
1313import { GameMode } from "./entities/game-mode.entity" ;
1414import { GamePlayerPerspective } from "./entities/game-player-perspective.entity" ;
1515import { GameRepositoryFilterDto } from "./dto/game-repository-filter.dto" ;
16- import { platformAbbreviationToIconMap } from "./game-repository.constants" ;
16+ import { PlatformToIconMap } from "./game-repository.constants" ;
1717import { GameExternalStoreDto } from "./dto/game-external-store.dto" ;
1818import { toMap } from "../../utils/toMap" ;
1919import { getRelationLoadStrategy } from "../../utils/getRelationLoadStrategy" ;
20- import { GameRepositoryCacheService } from "./game-repository-cache.service" ;
2120import { ExternalGameService } from "../external-game/external-game.service" ;
2221import {
2322 getIconNameForExternalGameCategory ,
2423 getStoreNameForExternalGameCategory ,
2524} from "../external-game/external-game.utils" ;
2625import { buildGameFilterFindOptions } from "./utils/build-game-filter-find-options" ;
26+ import { Cacheable } from "../../utils/cacheable" ;
27+ import { hours , minutes } from "@nestjs/throttler" ;
2728
2829/**
2930 * Look-up table between resource names and their respective entities
@@ -47,21 +48,23 @@ export class GameRepositoryService {
4748 * @param dataSource
4849 * @param gamePlatformRepository
4950 * @param externalGameService
50- * @param gameRepositoryCacheService
51+ * @param gameRepository
5152 */
5253 constructor (
5354 private readonly dataSource : DataSource ,
5455 @InjectRepository ( GamePlatform )
5556 private readonly gamePlatformRepository : Repository < GamePlatform > ,
5657 private readonly externalGameService : ExternalGameService ,
57- private readonly gameRepositoryCacheService : GameRepositoryCacheService ,
58+ @InjectRepository ( Game )
59+ private readonly gameRepository : Repository < Game > ,
5860 ) { }
5961
62+ @Cacheable ( GameRepositoryService . name , minutes ( 5 ) )
6063 async findOneById (
6164 id : number ,
6265 dto ?: GameRepositoryFindOneDto ,
6366 ) : Promise < Game > {
64- const game = await this . gameRepositoryCacheService . findOne ( {
67+ const game = await this . gameRepository . findOne ( {
6568 where : {
6669 id,
6770 } ,
@@ -85,6 +88,7 @@ export class GameRepositoryService {
8588 . filter ( ( game ) => game != undefined ) as Game [ ] ;
8689 }
8790
91+ @Cacheable ( GameRepositoryService . name , minutes ( 5 ) )
8892 async findAllByIds ( dto : GameRepositoryFindAllDto ) {
8993 if (
9094 dto == undefined ||
@@ -94,7 +98,7 @@ export class GameRepositoryService {
9498 throw new HttpException ( "Invalid query." , HttpStatus . BAD_REQUEST ) ;
9599 }
96100
97- const games = await this . gameRepositoryCacheService . find ( {
101+ const games = await this . gameRepository . find ( {
98102 where : {
99103 id : In ( dto ?. gameIds ) ,
100104 } ,
@@ -114,20 +118,21 @@ export class GameRepositoryService {
114118
115119 async findAll ( dto : BaseFindDto < Game > ) : Promise < TPaginationData < Game > > {
116120 const findOptions = buildBaseFindOptions ( dto ) ;
117- return this . gameRepositoryCacheService . findAndCount ( findOptions ) ;
121+ return this . gameRepository . findAndCount ( findOptions ) ;
118122 }
119123
120124 /**
121125 * @warning This operation can be quite expensive.
122126 * @param filterDto
123127 */
128+ @Cacheable ( GameRepositoryService . name , minutes ( 5 ) )
124129 async findAllIdsWithFilters (
125130 filterDto : GameRepositoryFilterDto ,
126131 ) : Promise < Game [ ] > {
127132 const findOptions = buildBaseFindOptions ( filterDto ) ;
128133 const whereOptions = buildGameFilterFindOptions ( filterDto ) ;
129134
130- const games = await this . gameRepositoryCacheService . find ( {
135+ const games = await this . gameRepository . find ( {
131136 ...findOptions ,
132137 where : whereOptions ,
133138 } ) ;
@@ -139,6 +144,7 @@ export class GameRepositoryService {
139144 return games ;
140145 }
141146
147+ @Cacheable ( GameRepositoryService . name , minutes ( 30 ) )
142148 async findGameExternalStores ( gameId : number ) {
143149 const externalGames = await this . externalGameService . findAllForGameId ( [
144150 gameId ,
@@ -171,6 +177,7 @@ export class GameRepositoryService {
171177 } ) ;
172178 }
173179
180+ @Cacheable ( GameRepositoryService . name , hours ( 1 ) )
174181 async getResource ( resource : TAllowedResource ) : Promise < any > {
175182 const resourceAsEntity = resourceToTargetEntityMap [ resource ] ;
176183 if ( resourceAsEntity == undefined ) {
@@ -182,6 +189,15 @@ export class GameRepositoryService {
182189 return await resourceRepository . find ( ) ;
183190 }
184191
192+ async getGamePlatformsMap < T extends "id" | "name" | "abbreviation" > (
193+ identifier : T ,
194+ ) {
195+ const platforms : GamePlatform [ ] = await this . getResource ( "platforms" ) ;
196+
197+ return toMap ( platforms , identifier ) ;
198+ }
199+
200+ @Cacheable ( GameRepositoryService . name , minutes ( 30 ) )
185201 async getIconsNamesForPlatforms ( gameId : number ) {
186202 const platforms = await this . gamePlatformRepository . find ( {
187203 where : {
@@ -195,9 +211,7 @@ export class GameRepositoryService {
195211 ( platform ) => platform . abbreviation ,
196212 ) ;
197213 const iconsNames : string [ ] = [ ] ;
198- for ( const [ iconName , platforms ] of Object . entries (
199- platformAbbreviationToIconMap ,
200- ) ) {
214+ for ( const [ iconName , platforms ] of Object . entries ( PlatformToIconMap ) ) {
201215 const abbreviationPresent = platformAbbreviations . some (
202216 ( abbreviation ) => platforms . includes ( abbreviation ) ,
203217 ) ;
0 commit comments