Skip to content

Commit

Permalink
developer-job-simulation#4 Get Pokemon by HP
Browse files Browse the repository at this point in the history
  • Loading branch information
AjelmarMedina committed Dec 16, 2023
1 parent fa7d584 commit 74910b4
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/routes/pokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

0 comments on commit 74910b4

Please sign in to comment.