Skip to content

Commit bb79bb9

Browse files
Nisreen OweidatNisreen Oweidat
authored andcommitted
final edit
1 parent 6fcf15e commit bb79bb9

File tree

3 files changed

+39
-43
lines changed

3 files changed

+39
-43
lines changed

src/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const swaggerUi = require("swagger-ui-express");
66
const YAML = require("yamljs");
77
const swaggerDocument = YAML.load("./docs/swagger.yaml");
88

9+
910
const indexRouter = require("./routes/index");
1011
const pokemonRouter = require("./routes/pokemon");
1112

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.0",
44
"private": true,
55
"scripts": {
6-
"start": "node ./bin/www"
6+
"start": "nodemon app.js"
77
},
88
"dependencies": {
99
"cookie-parser": "~1.4.4",

src/routes/pokemon.js

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,41 @@ const express = require("express");
22
const router = express.Router();
33
const 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

Comments
 (0)