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

Dian_pokedex-express-post #214

Open
wants to merge 7 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
199 changes: 148 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,167 @@
const express = require('express');
const jsonfile = require('jsonfile');
const express = require("express");
const jsonfile = require("jsonfile");
const FILE = "pokedex.json";
const app = express();

const FILE = 'pokedex.json';
const reactEngine = require("express-react-views").createEngine();
app.engine("jsx", reactEngine);

/**
* ===================================
* Configurations and set up
* ===================================
*/
app.set("views", __dirname + "/views");

// Init express app
const app = express();
app.set("view engine", "jsx");

/**
* ===================================
* Routes
* ===================================
*/
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);

app.get('/pokemon/:id', (request, response) => {
//----------------------------
//----------------------------

// get json from specified file
//Sorting pokemon depending on user selection
app.get("/pokemon/sort", (request, response) => {
jsonfile.readFile(FILE, (err, obj) => {

// check to make sure the file was properly read
if( err ){

console.log("error with json read file:",err);
response.status(503).send("error reading filee");
return;
if (err) {
return;
}
// obj is the object from the pokedex json file
// extract input data from request
let inputId = parseInt( request.params.id );

var pokemon;

// find pokemon by id from the pokedex json file
for( let i=0; i<obj.pokemon.length; i++ ){

let currentPokemon = obj.pokemon[i];
//SORT BY NAME *****
else if (request.query.options === "sortbyname") {
let nameArr = obj.pokemon.map((element) => {
return element.name.toUpperCase();
});
nameArr.sort();
let sortedNameObj = {
nameArray: nameArr,
};
response.render("sort-name", sortedNameObj);
}
//SORT BY ASCENDING WEIGHT *****
else if (request.query.options === "sortbyweight") {
let nameWeightArr = obj.pokemon.map((element) => {
let weightArr = element.weight.split(" ");
let weightNum = parseFloat(weightArr[0]);
let newObj = {
Name: element.name.toUpperCase(),
Weight: weightNum,
};
return newObj;
});
// console.log(nameWeightArr);
nameWeightArr.sort((a, b) => {
return a.Weight - b.Weight;
});
// console.log(nameWeightArr);
let sortedWeightObj = {
arr: nameWeightArr,
};
response.render("sort-weight", sortedWeightObj);
//SORT BY ASCENDING HEIGHT *****
} else if (request.query.options === "sortbyheight") {
let nameHeightArr = obj.pokemon.map((element) => {
let heightArr = element.height.split(" ");
let heightNum = parseFloat(heightArr[0]);
let newObj = {
Name: element.name.toUpperCase(),
Height: heightNum,
};
return newObj;
});
// console.log(nameHeightArr);
nameHeightArr.sort((a, b) => {
return a.Height - b.Height;
});
// console.log(nameHeightArr);
let sortedHeightObj = {
arr: nameHeightArr,
};
response.render("sort-height", sortedHeightObj);
}
});
});

if( currentPokemon.id === inputId ){
pokemon = currentPokemon;
//Submitting a new pokemon
app.post("/pokemon/new", (req, res) => {
let newPokemonObj = req.body;
jsonfile.readFile(FILE, (err, obj) => {
if (err) {
return;
}
//Disallows blank fields from being submitted
for (const property in req.body) {
if (newPokemonObj[property] === "") {
const comments = { comments: "Please complete all fields!" };
res.render("pokemon-form", comments);
return;
}
}
//Ensures that pokemon names are longer than one character
if (newPokemonObj.name.length < 2) {
const comments = { comments: "Error: Please enter a valid name with more than one character!" };
res.render("pokemon-form", comments);
return;
}
//Ensures that pokemon names are not purely numbers
// console.log(parseInt(newPokemonObj.name));
if (!!parseInt(newPokemonObj.name)) {
const comments = { comments: "Error: Please enter a valid name and not a number!" };
res.render("pokemon-form", comments);
return;
}
//Checks that there are no duplicate names in existing pokemon array
let pokemonArr = obj.pokemon;
let duplicate = false;
for (let index = 0; index < pokemonArr.length; index++) {
const name = pokemonArr[index].name;
if (name === newPokemonObj.name) {
duplicate = true;
break;
}
}
if (duplicate) {
const comments = { comments: "Error: Duplicate found in Name!" };
res.render("pokemon-form", comments);
return;
}
//Assigns correct Id and Number to new pokemon object before pushing it into the array
obj.lastKey = pokemonArr.length;
newPokemonObj.id = obj.lastKey + 1;
newPokemonObj.num = newPokemonObj.id.toString();
pokemonArr.push(newPokemonObj);
jsonfile.writeFile(FILE, obj, (err) => {
if (err) {
return;
}
});
//Redirect to display newly added pokemon
res.redirect("http://127.0.0.1:3000/pokemon/display-new");
});
});

if (pokemon === undefined) {
//Form for creating new pokemon
app.get("/pokemon/new", (req, res) => {
const comments = { comments: "" };
res.render("pokemon-form", comments);
});

// send 404 back
response.status(404);
response.send("not found");
} else {
//Displays latest pokemon only
app.get("/pokemon/display-new", (req, res) => {
jsonfile.readFile(FILE, (err, obj) => {
let newIndex = obj.pokemon.length - 1;
res.render("pokemon-display-last", obj.pokemon[newIndex]);
});
});

response.send(pokemon);
}
//Displays all pokemon
app.get("/pokemon", (req, res) => {
jsonfile.readFile(FILE, (err, obj) => {
res.render("pokemon-display-all", obj);
});
});

app.get('/', (request, response) => {
response.send("yay");
app.get("/", (request, response) => {
response.render("home");
});

/**
* ===================================
* Listen to requests on port 3000
* ===================================
*/
app.listen(3000, () => console.log('~~~ Tuning in to the waves of port 3000 ~~~'));
app.listen(3000, () => console.log("Listening to port 3000"));
Loading