Skip to content
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

Azhar Pokedex Express #188

Open
wants to merge 5 commits into
base: master
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
92 changes: 89 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');

// const jsonfile = require('jsonfile');
const jsonfile = require('jsonfile');

/**
* ===================================
Expand All @@ -17,9 +17,72 @@ const app = express();
* ===================================
*/

app.get('*', (request, response) => {
// app.get('*', (request, response) => {
// // send response with some data (a string)
// response.send(request.path);
// });

const file = 'pokedex.json';
let pokemon;
jsonfile.readFile(file, (err, obj) => {
pokemon = obj.pokemon;

});
// console.log(pokeName)

// app.get("/pokemon/:id", (request, response) => {
// var pokeId = request.params.id;
// pokeId = parseInt(pokeId);
// var pokeName = pokemon[pokeId].name;
// response.send(pokeName);

// });
app.get('/pokemon/', (request, response) => {
// send response with some data (a string)
response.send(request.path);
response.send(`Welcome to the online Pokedex`);
});
app.get("/pokemon/:name", (request, response) => {
var found = false;
var foundId;

for (id in pokemon) {
// console.log(pokemon[id].name)
if (pokemon[id].name.toLowerCase().includes(request.params.name)) {
found = true;
foundId = id;
break;
}
continue;
}
if (found == true) {
var pokeName = pokemon[foundId].name;
var pokeWeight = pokemon[foundId].weight;
var pokeHeight = pokemon[foundId].height;

var pokeType;
if (pokemon[foundId].type.length > 1) {
pokeType = pokemon[foundId].type[0] + "/" + pokemon[foundId].type[1];
} else {
pokeType = pokemon[foundId].type[0];
}



response.send(`This is ${pokeName}, a ${pokeType} type Pokemon, that weighs ${pokeWeight} and is ${pokeHeight} tall!`);
}
else {
var properName = toProperCase(request.params.name);
response.status(404);
response.send(`Could not find information about ${properName} - Is that a new pokemon? Gotta catch em' all!`);

}

});

app.get('/type/:type', (request, response) => {
var targetType = request.params.type;
var outType = getAllPokeOfType(targetType);
response.send(`${outType}`);
});

/**
Expand All @@ -28,3 +91,26 @@ app.get('*', (request, response) => {
* ===================================
*/
app.listen(3000, () => console.log('~~~ Tuning in to the waves of port 3000 ~~~'));

function toProperCase(string) {
var firstLetter = string.substring(0, 1);
var restOfLetter = string.substring(1, string.length);
firstLetter = firstLetter.toUpperCase();
restOfLetter = restOfLetter.toLowerCase();
return firstLetter + restOfLetter;
}

function getAllPokeOfType(type) {
var resultArr = [];
for (id in pokemon) {
var typeArr = pokemon[id].type;
for (id in typeArr){
typeArr[id] = typeArr[id].toLowerCase();
}
if (typeArr.includes(type)){
resultArr.push(pokemon[id].name);
}

}
return resultArr;
}
Loading