-
-
Save Ozerich/3b2a570668768bd899617d7814f01567 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async findOneGameByUrlAlias(urlAlias: string): Promise<Game | null> { | |
const relations = [ | |
"gamePlatforms", | |
"gamePlatforms.developer", | |
"gamePlatforms.developer.icon", | |
"gamePlatforms.developer.cover", | |
"reviews", | |
"reviews.user", | |
"gameSimilar", | |
"gameSimilar.gameSimilar", | |
"gameSimilar.gameSimilar.gamePlatforms", | |
"gameSimilar.gameSimilar.gamePlatforms.developer", | |
"gameTags", | |
"gameTags.tag", | |
"gameCategories", | |
"gameCategories.category", | |
"videoStreams" | |
]; | |
const [someBaseEntity, someBaseEntityWithScreenshots] = await Promise.all([ | |
this.gameRepository.findOne({ | |
where: { | |
url_alias: urlAlias | |
} | |
}), | |
this.gameRepository.findOne({ | |
where: { | |
url_alias: urlAlias | |
}, | |
relations: ["gamePlatforms", "gamePlatforms.screenshots"] | |
}) | |
]); | |
if (someBaseEntity) { | |
const someBaseEntityRelationObjects = await Promise.all( | |
relations.map(relation => { | |
return this.gameRepository.findOne({ | |
where: { | |
url_alias: urlAlias | |
}, | |
relations: [relation] | |
}); | |
}) | |
); | |
for (const relationObj of someBaseEntityRelationObjects) { | |
const { id, ...rest } = relationObj; | |
const key = Object.keys(rest)[Object.keys(rest).length - 1]; | |
const val = rest[Object.keys(rest)[Object.keys(rest).length - 1]]; | |
Object.assign(someBaseEntity, { [key]: val }); | |
} | |
} | |
if (someBaseEntityWithScreenshots) { | |
someBaseEntityWithScreenshots.gamePlatforms.forEach((gamePlatform, ind) => { | |
someBaseEntity.gamePlatforms[ind].screenshots = gamePlatform.screenshots; | |
}); | |
} | |
return someBaseEntity; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment