diff --git a/src/models/pokemon.ts b/src/models/pokemon.ts index 0f5c963..36f3f1f 100644 --- a/src/models/pokemon.ts +++ b/src/models/pokemon.ts @@ -7,6 +7,13 @@ interface PokemonData { description: string; } +export class NotFoundError extends Error { + constructor(message: string) { + super(message); + this.name = "NotFoundError"; + } +} + export class Pokemon implements PokemonData { id: number; name: string; @@ -35,4 +42,22 @@ export class Pokemon implements PokemonData { }); }); } + + static getById(db: Database, id: string) { + return new Promise((resolve, reject) => { + console.log(id); + db.get(`SELECT * FROM POKEDEX WHERE id = ${id};`, (error, row) => { + if (error) { + console.error(error); + reject(error); + } else if (row) { + const pokemonData: PokemonData = row as PokemonData; + const pokemon = new Pokemon(pokemonData); + resolve(pokemon); + } else { + reject(new NotFoundError(`Pokemon with id ${id} not found`)); + } + }); + }); + } } diff --git a/src/routes.ts b/src/routes.ts index 1b99251..1353619 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -1,5 +1,5 @@ import { Request, Router, Response } from "express"; -import { Pokemon } from "./models/pokemon.js"; +import { NotFoundError, Pokemon } from "./models/pokemon.js"; import { Subscriber } from "./models/subscriber.js"; import { db } from "./db.js"; @@ -13,6 +13,22 @@ export async function home(_req: Request, res: Response) { } } +export async function getPokemon(req: Request, res: Response) { + const { pokemonId } = req.params; + try { + const pokemon = await Pokemon.getById(db, pokemonId); + res.render("pokemon", { pokemon, pokemonId }); + } catch (error) { + if (error instanceof NotFoundError) { + res + .status(404) + .render("pokemon", { error: "Pokemon not found", pokemonId }); + } else { + res.status(500).send("An error occurred, please try again later."); + } + } +} + export async function subscribe(req: Request, res: Response) { const { email } = req.body; try { @@ -36,3 +52,4 @@ export async function subscribe(req: Request, res: Response) { export const router = Router(); router.get("/", home); router.post("/subscriptions", subscribe); +router.get("/pokemon/:pokemonId", getPokemon); diff --git a/src/views/home.handlebars b/src/views/home.handlebars index 0e84369..7c037bc 100644 --- a/src/views/home.handlebars +++ b/src/views/home.handlebars @@ -2,11 +2,13 @@

Pokedex

-
+
{{#pokemon}}
- {{ description }} -

{{ name }} #{{ id }}

+ {{description}} +

{{name}} #{{id}}

{{/pokemon}}
@@ -21,16 +23,27 @@
{{#if error}} {{/if}}
- -
We'll never share your email with anyone else.
+ +
We'll never share your email + with anyone else.
- +
diff --git a/src/views/pokemon.handlebars b/src/views/pokemon.handlebars new file mode 100644 index 0000000..010090f --- /dev/null +++ b/src/views/pokemon.handlebars @@ -0,0 +1,19 @@ +
+
+ {{#if pokemon}} +

{{pokemon.name}} #{{{pokemonId}}}

+
+ {{pokemon.name}} +
+

{{pokemon.description}}

+ {{else}} +

404 - Pokemon #{{{pokemonId}}} not found

+ {{/if}} +
+
\ No newline at end of file