Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 2x 2x 5x 5x 7x 7x 7x 7x 1x 1x 6x 7x | import { GetTranslatedPokemonQuery } from './GetTranslatedPokemonQuery';
import { Result } from '@Domain/Types/Result';
import { Pokemon } from '@Domain/ValueObjects/Pokemon';
import { LoggerInterface } from '@Application/Shared/Monitoring/LoggerInterface';
import { ClientInterface } from '@Application/Shared/Translator/ClientInterface';
/**
* Handler for the GetTranslatedPokemonQuery
*/
export class GetTranslatedPokemonQueryHandler {
constructor(
private readonly logger: LoggerInterface,
private readonly client: ClientInterface
) { }
/**
* Executes the query to retrieve a Pokemon with a translated description
* @param query The query containing the Pokemon to translate
* @returns A promise resolving to a Result containing either the translated Pokemon or an Error
*/
async execute(query: GetTranslatedPokemonQuery): Promise<Result<Error, Pokemon>> {
this.logger.info(`Translating pokemon description`, { name: query.pokemon.name });
const translationType = this.getTranslationType(query.pokemon);
const translationResult = await this.client.getTranslation(translationType, query.pokemon.description);
if (!translationResult.success) {
this.logger.error(`Failed to translate description, returning original pokemon`, {
name: query.pokemon.name,
error: translationResult.error.message,
});
return {
success: true,
data: query.pokemon,
};
}
return {
success: true,
data: new Pokemon(
query.pokemon.name,
translationResult.data,
query.pokemon.habitat,
query.pokemon.isLegendary,
)
};
}
/**
* Determines the translation type based on Pokemon characteristics
* @param pokemon The Pokemon to determine translation for
* @returns The translation type ('yoda' or 'shakespeare')
*/
private getTranslationType(pokemon: Pokemon): string {
return pokemon.habitat === 'cave' || pokemon.isLegendary
? 'yoda'
: 'shakespeare';
}
}
|