1- export var pokemonApiUrl = 'http://pokeapi.co/api/v2' ;
2-
31import {
2+ BadRequestException ,
43 Controller ,
54 Get ,
65 HttpException ,
7- Inject ,
6+ InternalServerErrorException ,
87 NotFoundException ,
9- Param ,
10- Post ,
118 Query ,
129 UseInterceptors ,
1310} from '@nestjs/common' ;
@@ -16,51 +13,62 @@ import { GetPokemonByNameQuery } from './dtos/get-pokemon-by-name.query';
1613import { Pokemon } from './types/pokemon' ;
1714import { AuthInterceptor } from '../auth/auth.interceptor' ;
1815import {
16+ ApiBadRequestResponse ,
1917 ApiInternalServerErrorResponse ,
18+ ApiNotFoundResponse ,
2019 ApiQuery ,
2120 ApiResponse ,
2221 ApiTags ,
2322} from '@nestjs/swagger' ;
23+ import { PokemonNotFoundError } from './types/error' ;
24+ import { GetPokemonByNameResponse } from './dtos/get-pokemon-by-name.response' ;
2425
2526@Controller ( 'pokemon' )
2627@ApiTags ( 'pokemon' )
2728@UseInterceptors ( AuthInterceptor )
2829export class PokemonController {
2930 constructor ( private pokemonService : PokemonService ) { }
3031
31- @Post ( 'pokemon' )
32+ @Get ( 'pokemon' )
3233 @ApiQuery ( {
3334 type : GetPokemonByNameQuery ,
3435 description : 'Lookup string for pokemons (match against pokemon name)' ,
3536 } )
3637 @ApiResponse ( {
3738 status : 200 ,
38- description : 'Returns pokemons whose name matches a string query param' ,
39+ type : GetPokemonByNameResponse ,
40+ description :
41+ 'Returns a pokemon whose name exactly matches the string query param' ,
42+ } )
43+ @ApiBadRequestResponse ( {
44+ description : 'Returned if query param "name" is empty' ,
45+ } )
46+ @ApiNotFoundResponse ( {
47+ description :
48+ 'Returned if no first-generation pokemon was found for this name' ,
3949 } )
4050 @ApiInternalServerErrorResponse ( {
41- description : 'This will never happen, trust me ' ,
51+ description : 'Returned if any other error is encountered ' ,
4252 } )
4353 async GetPokemonByNameController (
44- @Query ( ) name : any ,
45- ) : Promise < Pokemon | null > {
46- if ( name === null ) return ;
47-
48- name == null
49- ? name . trim ( ) != ''
50- ? ( ( name = name ) ,
51- ( pokemonApiUrl = pokemonApiUrl + '/' ) ,
52- ( pokemonApiUrl = pokemonApiUrl + name ) )
53- : ( ( pokemonApiUrl = pokemonApiUrl + '"?offset=20"' ) ,
54- ( pokemonApiUrl = pokemonApiUrl + '&limit=20' ) )
55- : ( ( pokemonApiUrl = pokemonApiUrl + '"?offset=20"' ) ,
56- ( pokemonApiUrl = pokemonApiUrl + '&limit=20' ) ) ;
57-
58- console . log ( 'Printing name for debug : ' , name ) ;
59-
60- const myPokemon = await this . pokemonService . findPokemonByNameOrFail ( name ) ;
54+ @Query ( 'name' ) name : string ,
55+ ) : Promise < Pokemon > {
56+ try {
57+ if ( name === undefined || name . length === 0 ) {
58+ throw new BadRequestException ( 'Pokemon name cannot be empty' ) ;
59+ }
6160
62- console . log ( 'Printing name for debug : ' , myPokemon ) ;
61+ const myPokemon = await this . pokemonService . findPokemonByNameOrFail ( name ) ;
6362
64- return myPokemon ;
63+ return myPokemon ;
64+ } catch ( error ) {
65+ if ( error instanceof PokemonNotFoundError ) {
66+ throw new NotFoundException ( error ) ;
67+ } else if ( error instanceof HttpException ) {
68+ throw error ;
69+ } else {
70+ throw new InternalServerErrorException ( error ) ;
71+ }
72+ }
6573 }
6674}
0 commit comments