Skip to content

Commit

Permalink
pokedex by generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin-Morette committed Nov 11, 2024
1 parent 6a9ea70 commit 17ce20a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
5 changes: 3 additions & 2 deletions backend/src/controllers/PokemonController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ const myCache = new NodeCache({ stdTTL: 86400, checkperiod: 90000 });
class PokemonController {
static readByTrainer = (req, res) => {
const isShiny = req.params.type === "shiny" ? 1 : 0;
const generation = req.params.generation;
let countPokemon = 0;
let sumPokemon = 0;
models.pokemon_trainer
.countAndSumPokemonByTrainer(req.params.id, isShiny)
.countAndSumPokemonByTrainer(req.params.id, isShiny, generation)
.then(([result]) => {
countPokemon = result[0].count;
sumPokemon = parseInt(result[0].sum, 10);
models.pokemon
.findByTrainer(req.params.id, isShiny)
.findByTrainer(req.params.id, isShiny, generation)
.then(([rows]) => {
res.send({ countPokemon, sumPokemon, pokemon: rows });
})
Expand Down
5 changes: 3 additions & 2 deletions backend/src/models/PokemonManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,17 @@ class PokemonManager extends AbstractManager {
);
}

findByTrainer(idTrainer, isShiny = 0) {
findByTrainer(idTrainer, isShiny = 0, generation = 1) {
return this.connection.query(
`SELECT p.*, pt.quantity
FROM ${PokemonManager.table} AS p
INNER JOIN pokemon_trainer AS pt ON p.id = pt.idPokemon
WHERE pt.idTrainer = ?
AND pt.quantity > 0
AND pt.isShiny = ?
AND p.generation = ?
ORDER BY p.id ASC`,
[idTrainer, isShiny]
[idTrainer, isShiny, generation]
);
}

Expand Down
12 changes: 9 additions & 3 deletions backend/src/models/PokemonTrainerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ class PokemonTrainerManager extends AbstractManager {
);
}

countAndSumPokemonByTrainer(idTrainer, isShiny = false) {
countAndSumPokemonByTrainer(idTrainer, isShiny = false, generation = 1) {
return this.connection.query(
`select count(*) as count, sum(quantity) as sum from ${PokemonTrainerManager.table} where idTrainer = ? and quantity > 0 AND isShiny = ?`,
[idTrainer, isShiny]
`SELECT COUNT(*) AS count, SUM(pt.quantity) AS sum
FROM ${PokemonTrainerManager.table} AS pt
JOIN pokemon AS p ON pt.idPokemon = p.id
WHERE pt.idTrainer = ?
AND pt.quantity > 0
AND pt.isShiny = ?
AND p.generation = ?`,
[idTrainer, isShiny, generation]
);
}

Expand Down
5 changes: 4 additions & 1 deletion backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ router.get("/pokeball/:id", PokeballController.read);
router.get("/pokeball/trainer/:id", PokeballController.readByTrainer);
router.post("/pokeball/buy", PokeballController.buy);

router.get("/pokemon/trainer/:id/:type", PokemonController.readByTrainer);
router.get(
"/pokemon/trainer/:id/:generation/:type",
PokemonController.readByTrainer
);
router.post("/pokemon", PokemonController.import);
router.post("/pokemon/catch", PokemonController.catchPokemon);
router.post("/pokemon/evolve", PokemonController.evolvePokemon);
Expand Down

0 comments on commit 17ce20a

Please sign in to comment.