Skip to content

Fixed all issues #14

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

Open
wants to merge 5 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
97 changes: 71 additions & 26 deletions src/routes/pokemon.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,83 @@
const express = require("express");
const router = express.Router();
const pokedex = require("../db/pokedex.json");
const express = require('express')
const router = express.Router()
const pokedex = require('../db/pokedex.json')

/* GET All Pokemon */
router.get("/", function (req, res, next) {
res.json(pokedex);
});

/* 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" });
return;
});
router.get('/', function (req, res, next) {
res.json(pokedex)
})

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

/* GET Pokemon by Type */
router.get("/type/:type", function (req, res, next) {
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;
});
let type = req.params.type
type = type.charAt(0).toUpperCase() + type.slice(1)
const foundPokemon = pokedex.filter((item) => item.type.includes(type))
if (foundPokemon.length > 0) {
return res.status(200).json(foundPokemon)
}
return res.status(400).json({ error: 'Bad request' })
})

/* GET Pokemon by HP */
router.get("/hp", function (req, res, next) {
router.get('/hp', function (req, res, next) {
// TODO: Implement this route. See swagger docs for details, by visiting http://localhost:3000/api-docs
const query = req.query
const filter = Object.keys(query)[0]
const value = Number(query[filter])
let foundPokemon = []
switch (filter) {
case 'gte':
foundPokemon = pokedex.filter((item) => item.base.HP >= value)
break
case 'gt':
foundPokemon = pokedex.filter((item) => item.base.HP > value)
break
case 'lt':
foundPokemon = pokedex.filter((item) => item.base.HP < value)
break
case 'lte':
foundPokemon = pokedex.filter((item) => item.base.HP <= value)
break
default:
return res.status(400).json({
error: 'Invalid Operator. Must be one of ["gt","gte","lt","lte"]',
})
}
if (foundPokemon.length > 0) {
return res.status(200).json(foundPokemon)
}
return res.status(404).json({ error: 'Not found' })
})

/* 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" });
return;
});
const id = req.params.id
const isNumber = id.match(/\d+/)
if (!isNumber) {
return res.status(400).json({ error: 'Invalid ID' })
}

const foundPokemon = pokedex.find((item) => item.id === Number(id))
if (foundPokemon) {
return res.status(200).json(foundPokemon)
}

return res.status(404).json({ error: 'Not found' })
})

module.exports = router;
module.exports = router