-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathpokemon.js
More file actions
113 lines (97 loc) · 3.01 KB
/
Copy pathpokemon.js
File metadata and controls
113 lines (97 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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 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
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();
}
return res.status(200).json(result);
});
/* 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
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(400).json({ error: 'Bad request' });
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
console.log('query: ', req.query);
const { gt, lt, lte, gte } = req.query;
console.log('gt: ', gt);
console.log('lt: ', lt);
let result = pokedex;
if (gt) {
result = result.filter((pokemon) => {
return pokemon.base.HP > parseInt(gt);
});
}
if (lt) {
result = result.filter((pokemon) => {
return pokemon.base.HP < parseInt(lt);
});
}
if (gte) {
result = result.filter((pokemon) => {
return pokemon.base.HP >= parseInt(gt);
});
}
if (lte) {
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"]',
});
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;