From d3b9cd1d05662fda7160810d52a5d1d6dc2708b1 Mon Sep 17 00:00:00 2001 From: JeraldStephenson <62264085+JeraldStephenson@users.noreply.github.com> Date: Fri, 26 May 2023 16:58:02 -0700 Subject: [PATCH 1/7] implemented Get Pokemon by ID --- src/routes/pokemon.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index f1b6a3c..cd702ef 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -1,37 +1,46 @@ -const express = require("express"); +const express = require('express'); const router = express.Router(); -const pokedex = require("../db/pokedex.json"); +const pokedex = require('../db/pokedex.json'); /* GET All Pokemon */ -router.get("/", function (req, res, next) { +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 = Number(req.params.id); + // console.log('id: ', typeof id, id); + if (typeof id !== 'number') { + res.status(400).json({ error: 'Invalid ID' }); + } + if (!pokedex[id]) { + res.status(400).json({ error: 'Not found' }); + } + res.status(200).json(pokedex[id - 1]); + return next(); }); /* 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" }); + res.status(501).json({ message: 'Not Implemented' }); return; }); /* 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" }); + res.status(501).json({ message: 'Not Implemented' }); return; }); /* 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" }); + res.status(501).json({ message: 'Not Implemented' }); return; }); From 48ac0e9503b3b3f482961903bab1cae3db7e6723 Mon Sep 17 00:00:00 2001 From: JeraldStephenson <62264085+JeraldStephenson@users.noreply.github.com> Date: Fri, 26 May 2023 17:37:55 -0700 Subject: [PATCH 2/7] fixed bugs with implementation of Get Pokemon by ID --- src/routes/pokemon.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index cd702ef..dff78a4 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -11,14 +11,17 @@ router.get('/', 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 - const id = Number(req.params.id); - // console.log('id: ', typeof id, id); - if (typeof id !== 'number') { + const id = req.params.id; + + if (isNaN(id)) { res.status(400).json({ error: 'Invalid ID' }); + return next(); } if (!pokedex[id]) { - res.status(400).json({ error: 'Not found' }); + res.status(404).json({ error: 'Not found' }); + return next(); } + res.status(200).json(pokedex[id - 1]); return next(); }); From 71841c4d6300efa36196bd0aeb1e70578258c439 Mon Sep 17 00:00:00 2001 From: JeraldStephenson <62264085+JeraldStephenson@users.noreply.github.com> Date: Fri, 26 May 2023 19:22:24 -0700 Subject: [PATCH 3/7] implemented Get Pokemon by Name --- src/routes/pokemon.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index dff78a4..d09608c 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -29,8 +29,25 @@ router.get('/:id', function (req, res, next) { /* 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' }); - return; + + const name = req.params.name.toLowerCase(); + + const result = pokedex.find((pokemon) => { + return ( + pokemon.name.english.toLowerCase() === name || + pokemon.name.chinese.toLowerCase() === name || + pokemon.name.french.toLowerCase() === name || + pokemon.name.japanese.toLowerCase() === name + ); + }); + // console.log('pokemon: ', result); + if (!result) { + res.status(404).json({ error: 'Not found' }); + return next(); + } + + res.status(200).json(result); + return next(); }); /* GET Pokemon by Type */ From 9fe0289b0b3e1d00fc714a371957eaf1a3dd926c Mon Sep 17 00:00:00 2001 From: JeraldStephenson <62264085+JeraldStephenson@users.noreply.github.com> Date: Fri, 26 May 2023 20:20:17 -0700 Subject: [PATCH 4/7] Implemented Get Pokemon by Type --- src/routes/pokemon.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index d09608c..07ed394 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -53,8 +53,20 @@ router.get('/name/:name', function (req, res, next) { /* 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; + const type = req.params.type; + console.log(type); + const capitalizedType = type.replace(type.at(0), type.at(0).toUpperCase()); + console.log(capitalizedType); + const result = pokedex.filter((pokemon) => { + return pokemon.type.includes(capitalizedType); + }); + + if (!result.length) { + res.status(404).json({ error: 'Bad request' }); + return next(); + } + res.status(200).json(result); + return next(); }); /* GET Pokemon by HP */ From 0e37d90acc12ca98d95a361c292f2746a9234e55 Mon Sep 17 00:00:00 2001 From: JeraldStephenson <62264085+JeraldStephenson@users.noreply.github.com> Date: Fri, 26 May 2023 22:59:04 -0700 Subject: [PATCH 5/7] implemented Get Pokemon by HP --- src/routes/pokemon.js | 83 +++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index 07ed394..19c3843 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -7,25 +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 (isNaN(id)) { - res.status(400).json({ error: 'Invalid ID' }); - return next(); - } - if (!pokedex[id]) { - res.status(404).json({ error: 'Not found' }); - return next(); - } - - res.status(200).json(pokedex[id - 1]); - return next(); -}); - /* 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 @@ -46,8 +27,7 @@ router.get('/name/:name', function (req, res, next) { return next(); } - res.status(200).json(result); - return next(); + return res.status(200).json(result); }); /* GET Pokemon by Type */ @@ -65,15 +45,66 @@ router.get('/type/:type', function (req, res, next) { res.status(404).json({ error: 'Bad request' }); return next(); } - res.status(200).json(result); - return next(); + return res.status(200).json(result); }); - /* 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; + + console.log('query: ', req.query); + + const { gt, lt, lte, gte } = req.query; + console.log('gt: ', gt); + console.log('lt: ', lt); + let result; + + if (gt) { + result = pokedex.filter((pokemon) => { + return pokemon.base.HP > parseInt(gt); + }); + } + if (lt) { + result = pokedex.filter((pokemon) => { + return pokemon.base.HP < parseInt(lt); + }); + } + if (gte) { + result = pokedex.filter((pokemon) => { + return pokemon.base.HP >= parseInt(gt); + }); + } + if (lte) { + result = pokedex.filter((pokemon) => { + return pokemon.base.HP <= parseInt(gt); + }); + } + + if (!gt && !lt && !gte && !lte) { + res.status(400).json({ + error: 'Invalid Operator. Must be one of ["gt","gte","lt","lte"]', + }); + return; + } + console.log('result: ', result); + + return res.status(200).json(result); +}); +/* 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 (isNaN(id)) { + res.status(400).json({ error: 'Invalid ID' }); + return next(); + } + if (!pokedex[id]) { + res.status(404).json({ error: 'Not found' }); + return next(); + } + + return res.status(200).json(pokedex[id - 1]); }); module.exports = router; From f7414d1525d3204b5463bf132dbd93f54dc73a64 Mon Sep 17 00:00:00 2001 From: JeraldStephenson <62264085+JeraldStephenson@users.noreply.github.com> Date: Fri, 26 May 2023 23:28:20 -0700 Subject: [PATCH 6/7] fixed bug with implementation of Get Pokemon by HP --- src/routes/pokemon.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/routes/pokemon.js b/src/routes/pokemon.js index 19c3843..c5ac441 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -56,29 +56,32 @@ router.get('/hp', function (req, res, next) { const { gt, lt, lte, gte } = req.query; console.log('gt: ', gt); console.log('lt: ', lt); - let result; + let result = pokedex; if (gt) { - result = pokedex.filter((pokemon) => { + result = result.filter((pokemon) => { return pokemon.base.HP > parseInt(gt); }); } if (lt) { - result = pokedex.filter((pokemon) => { + result = result.filter((pokemon) => { return pokemon.base.HP < parseInt(lt); }); } if (gte) { - result = pokedex.filter((pokemon) => { + result = result.filter((pokemon) => { return pokemon.base.HP >= parseInt(gt); }); } if (lte) { - result = pokedex.filter((pokemon) => { + result = result.filter((pokemon) => { return pokemon.base.HP <= parseInt(gt); }); } + if (!result.length) { + return res.status(404).json({ error: 'Not found' }); + } if (!gt && !lt && !gte && !lte) { res.status(400).json({ error: 'Invalid Operator. Must be one of ["gt","gte","lt","lte"]', From d45b91645e296841f4900df9211b55ff050ea511 Mon Sep 17 00:00:00 2001 From: JeraldStephenson <62264085+JeraldStephenson@users.noreply.github.com> Date: Fri, 26 May 2023 23:37:55 -0700 Subject: [PATCH 7/7] changed status from 404 to 400 on bad request for Get Pokemon by Type --- 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 c5ac441..b7a8e1f 100644 --- a/src/routes/pokemon.js +++ b/src/routes/pokemon.js @@ -42,7 +42,7 @@ router.get('/type/:type', function (req, res, next) { }); if (!result.length) { - res.status(404).json({ error: 'Bad request' }); + res.status(400).json({ error: 'Bad request' }); return next(); } return res.status(200).json(result);