From 4adbc39b5e3e559821700e6d56a95a4ff8a2331e Mon Sep 17 00:00:00 2001 From: sean Date: Wed, 22 Feb 2023 23:32:32 +0800 Subject: [PATCH] test --- package-lock.json | 6 ++ src/docs/swagger.yaml | 78 ++++++++++++++++++--- src/routes/pokemon.ts | 158 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 210 insertions(+), 32 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..1898fc5 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "express-ts-backend", + "lockfileVersion": 2, + "requires": true, + "packages": {} +} diff --git a/src/docs/swagger.yaml b/src/docs/swagger.yaml index 2082dbc..0606c53 100644 --- a/src/docs/swagger.yaml +++ b/src/docs/swagger.yaml @@ -4,6 +4,7 @@ info: version: "1.0" servers: - url: https://localhost:3000/ + paths: /pokemon: get: @@ -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. @@ -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} @@ -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 ..., ] @@ -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 @@ -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) diff --git a/src/routes/pokemon.ts b/src/routes/pokemon.ts index 1edf970..5bbca10 100644 --- a/src/routes/pokemon.ts +++ b/src/routes/pokemon.ts @@ -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;