Skip to content

text #6

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 69 additions & 9 deletions src/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ info:
version: "1.0"
servers:
- url: https://localhost:3000/

paths:
/pokemon:
get:
Expand All @@ -29,9 +30,23 @@ paths:
example:
{
id: 482,
name: { english: "Azelf", japanese: "アグノム", chinese: "亚克诺姆", french: "Créfadet" },
name:
{
english: "Azelf",
japanese: "アグノム",
chinese: "亚克诺姆",
french: "Créfadet",
},
type: ["Psychic"],
base: { HP: 75, Attack: 125, Defense: 70, "Sp. Attack": 125, "Sp. Defense": 70, Speed: 115 },
base:
{
HP: 75,
Attack: 125,
Defense: 70,
"Sp. Attack": 125,
"Sp. Defense": 70,
Speed: 115,
},
}
"400":
description: Invalid ID. Id must be number.
Expand Down Expand Up @@ -62,9 +77,23 @@ paths:
example:
{
id: 151,
name: { english: "Mew", japanese: "ミュウ", chinese: "梦幻", french: "Mew" },
name:
{
english: "Mew",
japanese: "ミュウ",
chinese: "梦幻",
french: "Mew",
},
type: ["Psychic"],
base: { HP: 100, Attack: 100, Defense: 100, "Sp. Attack": 100, "Sp. Defense": 100, Speed: 100 },
base:
{
HP: 100,
Attack: 100,
Defense: 100,
"Sp. Attack": 100,
"Sp. Defense": 100,
Speed: 100,
},
}
"404":
description: Pokemon not found with name of {name}
Expand Down Expand Up @@ -111,9 +140,23 @@ paths:
[
{
id: 35,
name: { english: "Clefairy", japanese: "ピッピ", chinese: "皮皮", french: "Mélofée" },
name:
{
english: "Clefairy",
japanese: "ピッピ",
chinese: "皮皮",
french: "Mélofée",
},
type: ["Fairy"],
base: { HP: 70, Attack: 45, Defense: 48, "Sp. Attack": 60, "Sp. Defense": 65, Speed: 35 },
base:
{
HP: 70,
Attack: 45,
Defense: 48,
"Sp. Attack": 60,
"Sp. Defense": 65,
Speed: 35,
},
},
// more values ...,
]
Expand Down Expand Up @@ -181,9 +224,23 @@ paths:
example:
{
id: 242,
name: { english: "Blissey", japanese: "ハピナス", chinese: "幸福蛋", french: "Leuphorie" },
name:
{
english: "Blissey",
japanese: "ハピナス",
chinese: "幸福蛋",
french: "Leuphorie",
},
type: ["Normal"],
base: { HP: 255, Attack: 10, Defense: 10, "Sp. Attack": 75, "Sp. Defense": 135, Speed: 55 },
base:
{
HP: 255,
Attack: 10,
Defense: 10,
"Sp. Attack": 75,
"Sp. Defense": 135,
Speed: 55,
},
}
"400":
description: Invalid HP Filter(s) provided
Expand All @@ -194,7 +251,10 @@ paths:
properties:
error:
type: string
example: { error: 'Invalid Operator. Must be one of ["gt","gte","lt","lte"]' }
example:
{
error: 'Invalid Operator. Must be one of ["gt","gte","lt","lte"]',
}

"404":
description: No pokemon found for given filter(s)
Expand Down
158 changes: 135 additions & 23 deletions src/routes/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,148 @@ import pokedex from "../db/pokedex.json";
const router = Router();

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

/* GET Pokemon by Id. */
router.get("/:id", function (req: Request, res: Response, next: NextFunction): void {
// 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(
"/:id",
function (req: Request, res: Response, next: NextFunction): void {
// TODO: Implement this route. See swagger docs for details, by visiting http://localhost:3000/api-docs
const id = Number(req.params.id);

if (!id) {
res.status(400).json({
error: "Invalid ID",
});
return;
}

const findPokemon = pokedex.find((obj) => obj.id === id);

if (!findPokemon) {
res.status(404).json({
error: "Not found",
});
return;
}

res.status(200).json(findPokemon);
return;
}
);

/* GET Pokemon by English Name */
router.get("/name/:name", function (req: Request, res: Response, next: NextFunction): void {
// 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(
"/name/:name",
function (req: Request, res: Response, next: NextFunction): void {
// TODO: Implement this route. See swagger docs for details, by visiting http://localhost:3000/api-docs

const pokemonName = req.params.name;

const findPokemon = pokedex.find((item) => {
return Object.values(item.name).find((val) => {
return val === pokemonName;
});
});

if (!findPokemon) {
res.status(404).json({
error: "Not found",
});
return;
}
res.status(200).json(findPokemon);
return;
}
);

/* GET Pokemon by Type */
router.get("/type/:type", function (req: Request, res: Response, next: NextFunction): void {
// 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(
"/type/:type",
function (req: Request, res: Response, next: NextFunction): void {
// TODO: Implement this route. See swagger docs for details, by visiting http://localhost:3000/api-docs

const type = req.params.type as string;

const findPokemon = pokedex.filter((val) => {
return val.type.includes(type);
});
if (findPokemon.length === 0) {
res.status(400).json({
error: "Bad request",
});
return;
}
res.status(200).json(findPokemon);
return;
}
);

/* GET Pokemon by HP */
router.get("/hp", function (req: Request, res: Response, next: NextFunction): void {
// 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(
"/hp",
function (req: Request, res: Response, next: NextFunction): void {
// TODO: Implement this route. See swagger docs for details, by visiting http://localhost:3000/api-docs
const lt = Number(req.query.lt);
const gt = Number(req.query.gt);
const lte = Number(req.query.lte);
const gte = Number(req.query.gte);

const validQueryStrings = ["gt", "gte", "lt", "lte"];
const queryStringKey = Object.keys(req.query);
const isValid = queryStringKey.every((val) =>
validQueryStrings.includes(val)
);
let findPokemon;

if (!isValid) {
res.status(400).json({
error: 'Invalid Operator. Must be one of ["gt","gte","lt","lte"]',
});
return;
}

if (lt) {
findPokemon = pokedex.filter((val) => {
return val.base.HP < lt;
});
} else if (gt) {
findPokemon = pokedex.filter((val) => {
return val.base.HP > gt;
});
} else if (lte) {
findPokemon = pokedex.filter((val) => {
return val.base.HP <= lte;
});
} else if (gte) {
findPokemon = pokedex.filter((val) => {
return val.base.HP >= gte;
});
} else if (lte && gt) {
findPokemon = pokedex.filter((val) => {
return val.base.HP <= lte && val.base.HP > gt;
});
} else if (gte && lte) {
findPokemon = pokedex.filter((val) => {
return val.base.HP <= gte && val.base.HP >= lte;
});
}

if (!findPokemon) {
res.status(404).json({
error: "Not found",
});
return;
}

res.status(200).json(findPokemon);
return;
}
);

export default router;