Skip to content

Commit 8618da7

Browse files
committed
WIP: new feature to show pokemon
1 parent af6f814 commit 8618da7

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed

src/models/pokemon.ts

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ interface PokemonData {
77
description: string;
88
}
99

10+
export class NotFoundError extends Error {
11+
constructor(message: string) {
12+
super(message);
13+
this.name = "NotFoundError";
14+
}
15+
}
16+
1017
export class Pokemon implements PokemonData {
1118
id: number;
1219
name: string;
@@ -35,4 +42,22 @@ export class Pokemon implements PokemonData {
3542
});
3643
});
3744
}
45+
46+
static getById(db: Database, id: string) {
47+
return new Promise<Pokemon>((resolve, reject) => {
48+
console.log(id);
49+
db.get(`SELECT * FROM POKEDEX WHERE id = ${id};`, (error, row) => {
50+
if (error) {
51+
console.error(error);
52+
reject(error);
53+
} else if (row) {
54+
const pokemonData: PokemonData = row as PokemonData;
55+
const pokemon = new Pokemon(pokemonData);
56+
resolve(pokemon);
57+
} else {
58+
reject(new NotFoundError(`Pokemon with id ${id} not found`));
59+
}
60+
});
61+
});
62+
}
3863
}

src/routes.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Request, Router, Response } from "express";
2-
import { Pokemon } from "./models/pokemon.js";
2+
import { NotFoundError, Pokemon } from "./models/pokemon.js";
33
import { Subscriber } from "./models/subscriber.js";
44
import { db } from "./db.js";
55

@@ -13,6 +13,22 @@ export async function home(_req: Request, res: Response) {
1313
}
1414
}
1515

16+
export async function getPokemon(req: Request, res: Response) {
17+
const { pokemonId } = req.params;
18+
try {
19+
const pokemon = await Pokemon.getById(db, pokemonId);
20+
res.render("pokemon", { pokemon, pokemonId });
21+
} catch (error) {
22+
if (error instanceof NotFoundError) {
23+
res
24+
.status(404)
25+
.render("pokemon", { error: "Pokemon not found", pokemonId });
26+
} else {
27+
res.status(500).send("An error occurred, please try again later.");
28+
}
29+
}
30+
}
31+
1632
export async function subscribe(req: Request, res: Response) {
1733
const { email } = req.body;
1834
try {
@@ -36,3 +52,4 @@ export async function subscribe(req: Request, res: Response) {
3652
export const router = Router();
3753
router.get("/", home);
3854
router.post("/subscriptions", subscribe);
55+
router.get("/pokemon/:pokemonId", getPokemon);

src/views/home.handlebars

+20-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
<h1>Pokedex</h1>
33
</div>
44

5-
<div class="container d-flex flex-row flex-wrap justify-content-start align-items-center text-center mt-3">
5+
<div
6+
class="container d-flex flex-row flex-wrap justify-content-start align-items-center text-center mt-3"
7+
>
68
{{#pokemon}}
79
<div class="col-6 col-md-4 col-lg-3 p-2">
8-
<img alt="{{ description }}" title="{{ description }}" src="{{ imageUrl }}" />
9-
<p>{{ name }} #{{ id }}</p>
10+
<img alt="{{description}}" title="{{description}}" src="{{imageUrl}}" />
11+
<p><a href="/pokemon/{{id}}">{{name}} #{{id}}</a></p>
1012
</div>
1113
{{/pokemon}}
1214
</div>
@@ -21,16 +23,27 @@
2123
<div class="col-md-5 col-12">
2224
{{#if error}}
2325
<div class="alert alert-danger" role="alert">
24-
{{ error }}
26+
{{error}}
2527
</div>
2628
{{/if}}
2729
<div class="form-outline form-white mb-4">
28-
<input type="text" value="{{ email }}" class="form-control" id="input_email" name="email" aria-describedby="emailHelp">
29-
<div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
30+
<input
31+
type="text"
32+
value="{{email}}"
33+
class="form-control"
34+
id="input_email"
35+
name="email"
36+
aria-describedby="emailHelp"
37+
/>
38+
<div id="emailHelp" class="form-text">We'll never share your email
39+
with anyone else.</div>
3040
</div>
3141
</div>
3242
<div class="col-auto">
33-
<button type="submit" class="btn btn-outline-light mb-4">Subscribe</button>
43+
<button
44+
type="submit"
45+
class="btn btn-outline-light mb-4"
46+
>Subscribe</button>
3447
</div>
3548
</div>
3649
</form>

src/views/pokemon.handlebars

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<div
2+
class="full-page d-flex flex-wrap justify-content-center align-items-center"
3+
>
4+
<div
5+
class="container d-flex flex-column flex-wrap justify-content-start align-items-center text-center py-5 m-auto margin-mobile"
6+
>
7+
{{#if pokemon}}
8+
<h1>{{pokemon.name}} #{{{pokemonId}}} </h1>
9+
<div
10+
class="flex-row container d-flex flex-wrap justify-content-center align-items-center text-center mt-2"
11+
>
12+
<img src="{{pokemon.imageUrl}}" alt="{{pokemon.name}}" />
13+
</div>
14+
<p>{{pokemon.description}}</p>
15+
{{else}}
16+
<h1>404 - Pokemon #{{{pokemonId}}} not found</h1>
17+
{{/if}}
18+
</div>
19+
</div>

0 commit comments

Comments
 (0)