diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index c05a3a4..5677800 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -7,6 +7,28 @@ router.get("/", function (req, res, next) { res.json(pokedex); }); +/* GET Pokemon by HP */ +router.get("/hp", function (req, res, next) { + const queryKeys = Object.keys(req.query) + if (queryKeys.length > 1 || !queryKeys.length) res.status(404).json({ error: "Not found" }); + let predicate; + switch (queryKeys[0]) { + case 'gt': predicate = pokemon => pokemon.base.HP > req.query["gt"]; + break; + case 'lt': predicate = pokemon => pokemon.base.HP < req.query["lt"]; + break; + case 'gte': predicate = pokemon => pokemon.base.HP > req.query["gte"]; + break; + case 'lte': predicate = pokemon => pokemon.base.HP < req.query["lte"]; + break; + default: res.status(400).json({ error: 'Invalid Operator. Must be one of ["gt","gte","lt","lte"]' }) + break; + } + const searchResults = pokedex.filter(predicate); + res.status(200).json(searchResults) + return; +}); + /* GET Pokemon by Id. */ router.get("/:id", function (req, res, next) { try { @@ -38,11 +60,4 @@ router.get("/type/:type", function (req, res, next) { return; }); -/* GET Pokemon by HP */ -router.get("/hp", function (req, res, next) { - // TODO: Implement this route. See swagger docs for details, by visiting http://localhost:3000/api-docs - res.status(501).json({ message: "Not Implemented" }); - return; -}); - module.exports = router;