@@ -2,26 +2,41 @@ const express = require("express");
22const router = express . Router ( ) ;
33const pokedex = require ( "../db/pokedex.json" ) ;
44
5- /* GET All Pokemon */
6- router . get ( "/" , function ( req , res , next ) {
7- res . json ( pokedex ) ;
5+
6+
7+ // router.get("/hp", (req, res, next) =>{
8+ // const filredParams = req.query;
9+ // res.json(filredParams)
10+
11+ // });
12+ router . get ( "/hp" , ( req , res ) => {
13+ const { gte, lte, gt, lt } = req . query ;
14+
15+ const filteredPokemons = pokedex . filter ( p =>
16+ ( ! gte || p . base . HP >= gte ) &&
17+ ( ! lte || p . base . HP <= lte ) &&
18+ ( ! gt || p . base . HP > gt ) &&
19+ ( ! lt || p . base . HP < lt )
20+ ) ;
21+
22+ res . json ( filteredPokemons . length ? filteredPokemons : { message : "No Pokémon found" } ) ;
823} ) ;
924
10- /* GET Pokemon by English Name */
11- router . get ( "/name/:name" , function ( req , res , next ) {
12- // TODO: Implement this route. See swagger docs for details, by visiting http://localhost:3000/api-docs
13-
14- const name = req . params . name ;
15- const pokemon = pokedex . find ( p => p . name . english === "Pikachu" ) ;
25+
26+
27+ //
28+ router . get ( "/name/:name" , ( req , res , next ) => {
29+
30+ const queryName = req . params . name . toLowerCase ( ) ;
31+ const pokemon = pokedex . find ( p => p . name . english . toLowerCase ( ) === queryName ) ;
1632 if ( ! pokemon ) {
1733 return res . status ( 404 ) . json ( { error : "Pokemon not found" } ) ;
1834 }
1935 res . json ( pokemon ) ;
2036} ) ;
2137
22- /* GET Pokemon by Id. */
23- router . get ( "/:id" , function ( req , res , next ) {
24- // TODO: Implement this route. See swagger docs for details, by visiting http://localhost:3000/api-docs
38+ //
39+ router . get ( "/:id" , ( req , res , next ) => {
2540
2641 const id = req . params . id ;
2742 const pokemon = pokedex . find ( p => p . id == id ) ;
@@ -30,43 +45,23 @@ router.get("/:id", function (req, res, next) {
3045 res . json ( pokemon ) ;
3146 return ;
3247 }
33-
34- res . status ( 501 ) . json ( { message : "Not Implemented" } ) ;
35- return ;
36- } ) ;
37-
38-
39-
40- /* GET Pokemon by Type */
41- router . get ( "/type/:type" , function ( req , res , next ) {
42- // TODO: Implement this route. See swagger docs for details, by visiting http://localhost:3000/api-docs
43-
44- const type = req . params . type ;
45- const pokemon = pokedex . filter ( p => p . type . includes ( "Fire" ) ) ;
46-
47- if ( pokemon ) {
48- res . json ( pokemon ) ;
49- return ;
50- }
5148
52-
53- res . status ( 501 ) . json ( { message : "Not Implemented" } ) ;
49+ return res . status ( 404 ) . json ( { error : "Pokemon not found" } ) ;
5450 return ;
5551} ) ;
5652
57- /* GET Pokemon by HP */
58- router . get ( "/hp" , function ( req , res , next ) {
59- // TODO: Implement this route. See swagger docs for details, by visiting http://localhost:3000/api-docs
53+
54+ //
55+ router . get ( "/type/:type" , ( req , res , next ) => {
6056
61- const hp = req . params . hp ;
62- const pokemon = pokedex . filter ( p => p . base . HP >= 90 ) ;
57+ const queryType = req . params . type . toLowerCase ( ) ;
58+ const pokemon = pokedex . filter ( p => p . type . toLowerCase ( ) === queryType ) ;
6359
64- if ( pokemon ) {
65- res . json ( pokemon ) ;
66- return ;
60+ if ( ! pokemon ) {
61+ res . status ( 404 ) . json ( { error : "Pokemon not found" } ) ;
62+ return ;
6763 }
68-
69- res . status ( 501 ) . json ( { message : "Not Implemented" } ) ;
64+ res . status ( 200 ) . json ( pokemon ) ;
7065 return ;
7166} ) ;
7267
0 commit comments