From 4005ed32c4ed35494615eb4e861ac2494d1a3760 Mon Sep 17 00:00:00 2001 From: Anton Gundermann Date: Thu, 10 Aug 2023 12:24:14 +0200 Subject: [PATCH 1/5] fixed: issues #1, #2 and #3 --- src/routes/pokemon.js | 68 ++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index f1b6a3c..e2bc4a8 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -1,38 +1,60 @@ -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); -}); +router.get('/', function (req, res, next) { + res.json(pokedex) +}) /* GET Pokemon by Id. */ -router.get("/:id", function (req, res, next) { +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 + if (!Number(id)) { + 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({ message: 'Not found' }) +}) /* 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({ message: '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({ message: '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 - res.status(501).json({ message: "Not Implemented" }); - return; -}); + console.log(req) + + res.status(501).json(req) + return +}) -module.exports = router; +module.exports = router From 35cda079a3dede8eeff4a6782beb0f251139d462 Mon Sep 17 00:00:00 2001 From: Anton Gundermann Date: Fri, 11 Aug 2023 20:26:47 +0200 Subject: [PATCH 2/5] fixed: issues #4 --- src/routes/pokemon.js | 61 +++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index e2bc4a8..54c992b 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -7,22 +7,6 @@ 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 - const id = req.params.id - if (!Number(id)) { - 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({ message: 'Not found' }) -}) - /* 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 @@ -51,10 +35,49 @@ router.get('/type/:type', function (req, res, next) { /* 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 - console.log(req) + 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) { + return res.status(200).json(foundPokemon) + } + return res.status(404).json({ message: '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 + 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) + } - res.status(501).json(req) - return + return res.status(404).json({ message: 'Not found' }) }) module.exports = router From 336540a751b53e21f34905c9de13e1e21332251f Mon Sep 17 00:00:00 2001 From: Anton Gundermann Date: Fri, 11 Aug 2023 20:30:53 +0200 Subject: [PATCH 3/5] fixed: correct naming --- src/routes/pokemon.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index 54c992b..92d0404 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -17,7 +17,7 @@ router.get('/name/:name', function (req, res, next) { if (foundPokemon) { return res.status(200).json(foundPokemon) } - return res.status(404).json({ message: 'Not found' }) + return res.status(404).json({ error: 'Not found' }) }) /* GET Pokemon by Type */ @@ -29,7 +29,7 @@ router.get('/type/:type', function (req, res, next) { if (foundPokemon.length > 0) { return res.status(200).json(foundPokemon) } - return res.status(400).json({ message: 'Bad request' }) + return res.status(400).json({ error: 'Bad request' }) }) /* GET Pokemon by HP */ @@ -60,7 +60,7 @@ router.get('/hp', function (req, res, next) { if (foundPokemon.length) { return res.status(200).json(foundPokemon) } - return res.status(404).json({ message: 'Not found' }) + return res.status(404).json({ error: 'Not found' }) }) /* GET Pokemon by Id. */ @@ -77,7 +77,7 @@ router.get('/:id', function (req, res, next) { return res.status(200).json(foundPokemon) } - return res.status(404).json({ message: 'Not found' }) + return res.status(404).json({ error: 'Not found' }) }) module.exports = router From 97076a0210794015e3262a9d766fdc2ba08ab6c9 Mon Sep 17 00:00:00 2001 From: Anton Gundermann Date: Fri, 11 Aug 2023 20:34:03 +0200 Subject: [PATCH 4/5] fixed: hp route --- src/routes/pokemon.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index 92d0404..dd9a4d3 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -73,7 +73,7 @@ router.get('/:id', function (req, res, next) { } const foundPokemon = pokedex.find((item) => item.id === Number(id)) - if (foundPokemon) { + if (foundPokemon.length) { return res.status(200).json(foundPokemon) } From bcd9956d75bd74083f10059d226e16d9cb33575b Mon Sep 17 00:00:00 2001 From: Anton Gundermann Date: Fri, 11 Aug 2023 20:37:43 +0200 Subject: [PATCH 5/5] fixed: hp route --- src/routes/pokemon.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index dd9a4d3..8dd7aca 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -57,7 +57,7 @@ router.get('/hp', function (req, res, next) { error: 'Invalid Operator. Must be one of ["gt","gte","lt","lte"]', }) } - if (foundPokemon.length) { + if (foundPokemon.length > 0) { return res.status(200).json(foundPokemon) } return res.status(404).json({ error: 'Not found' }) @@ -73,7 +73,7 @@ router.get('/:id', function (req, res, next) { } const foundPokemon = pokedex.find((item) => item.id === Number(id)) - if (foundPokemon.length) { + if (foundPokemon) { return res.status(200).json(foundPokemon) }