Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1, #2, #3, #4 #17

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions src/routes/pokemon.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,65 @@
const express = require("express");
const router = express.Router();
const pokedex = require("../db/pokedex.json");
const { search } = require("./pokemon");

/* GET All Pokemon */
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) return res.status(404).json({ error: "Not found" });

/**
* @returns {Boolean} if a given value tests valid with the respective query
*/
function testQuery(query, value) {
switch (query) {
case 'gt': return value > req.query["gt"];
case 'lt': return value < req.query["lt"];
case 'gte': return value > req.query["gte"];
case 'lte': return value < req.query["lte"];
default: res.status(400).json({ error: 'Invalid Operator. Must be one of ["gt","gte","lt","lte"]' })
}
}

const searchResults = pokedex.filter(pokemon =>
queryKeys.reduce((isPassed, query) =>
isPassed = isPassed ? testQuery(query, pokemon.base.HP) : false
, true));
if (searchResults.length) res.status(200).json(searchResults);
else res.status(404).json({ error: "Not found" })
return;
});

/* GET Pokemon by Id. */
router.get("/:id", 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" });
const id = parseInt(req.params.id);
if (isNaN(id)) return res.status(400).json({ error: "Invalid ID" });
const searchResult = pokedex.find(pokemon => pokemon.id === id);
if (searchResult) res.status(200).json(searchResult);
else res.status(404).json({ error: "Not found" });
return;
});

/* GET Pokemon by English Name */
router.get("/name/:name", 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" });
const name = req.params.name.toLowerCase();
const searchResult = pokedex.find(pokemon => `${pokemon.name.english}`.toLowerCase() == name);
if (searchResult) res.status(200).json(searchResult);
else res.status(404).json({ error: "Not found" });
return;
});

/* GET Pokemon by Type */
router.get("/type/:type", 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;
});

/* 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" });
const qType = req.params.type.toLowerCase();
const searchResults = pokedex.filter(pokemon => pokemon.type.find(type => type.toLowerCase() === qType));
if (searchResults.length) res.status(200).json(searchResults);
else res.status(400).json({ error: "Bad request" });
return;
});