diff --git a/data/schema.sql b/data/schema.sql index e69de29..21ee261 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -0,0 +1,76 @@ +drop database IF EXISTS classicmodels; +drop database IF EXISTS pokedex ; +create database pokedex; +use pokedex; +CREATE TABLE pokemon ( + pokemon_id INT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + -- color VARCHAR(255), + type VARCHAR(255) + -- type2 VARCHAR(255), + -- weakness VARCHAR(255) + ); + CREATE TABLE types ( +type_id INT PRIMARY KEY NOT NULL auto_increment, +type VARCHAR(255) NOT NULL + ); + CREATE TABLE pokemon_type ( + pokemon_type_id INT PRIMARY KEY NOT NULL auto_increment, + type_id INT NOT NULL, + pokemon_id INT NOT NULL, + FOREIGN KEY (pokemon_id) REFERENCES pokemon(pokemon_id), + FOREIGN KEY (type_id) REFERENCES types(type_id) +); + +ALTER TABLE pokemon_type ADD FOREIGN KEY(pokemon_id) REFERENCES pokemon(pokemon_id); +ALTER TABLE pokemon_type ADD FOREIGN KEY(type_id) REFERENCES types(type_id); +INSERT INTO pokemon (pokemon_id, name) +VALUES +(1,'bulbasaur'), +(2, 'ivysaur'), +(3, 'venusaur'), +(4, 'charmander'), +(5, 'charmeleon'), +(6, 'charizard'), +(7, 'squirtle'), +(8, 'wartortle'), +(9, 'blastoise'), +(10, 'caterpie'); +INSERT INTO types(type) +VALUES +('fire'), +('bug'), +('grass'), +('water'), +('normal'), +('electric'), +('poison'), +('ground'), +('fairy'), +('psychic'), +('rock'), +('fighting'), +('ghost'), +('ice'), +('dragon'), +('steel'), +('dark'), +('flying'); +INSERT INTO pokemon_type(type_id, pokemon_id) +VALUES +(3, 1), +(3, 2), +(3, 3), +(1, 4), +(1, 5), +(1, 6), +(4, 7), +(4, 8), +(4, 9), +(2, 10); +SELECT p.name, tp.type, pt.pokemon_type_id +FROM pokemon p +LEFT JOIN pokemon_type pt +ON p.pokemon_id = pt.pokemon_id +INNER JOIN types tp +ON tp.type_id = pt.type_id; diff --git a/db/index.js b/db/index.js new file mode 100644 index 0000000..ba4e25c --- /dev/null +++ b/db/index.js @@ -0,0 +1,25 @@ +const mysql = require("mysql"); + +const connectionDb = mysql.createPool({ + //allows queries + connectionLimit: 10, + password: "secret", + user: "root", + database: "Pokedex", + host: "localhost", +}); + +let pokeDb = {}; + +pokeDb.all = () => { + return new Promise((resolve, reject) => { + connectionDb.query(`SELECT * FROM Pokemon`, (err, results) => { + if (err) { + return reject(err); // Error Handling + } + return resolve(results); // Results from DB + }); + }); +}; + +module.exports = pokeDb; diff --git a/index.js b/index.js index bba5463..89bfe51 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,24 @@ -const express = require('express') -const app = express() -const port = 3000 +const express = require("express"); +const bodyParser = require("body-parser"); +const cors = require("cors"); +const apiRouter = require("./rest"); -app.get('/', (req, res) => { - res.send('Hello World!') -}) +const app = express(); -app.listen(port, () => { - console.log(`Example app listening at http://localhost:${port}`) -}) \ No newline at end of file +// Body Parser Middleware +app.use(bodyParser.json()); +app.use(cors()); +app.use(express.static("public")); +app.use("/api/v2/allPokemon", apiRouter); + +/* +function (req, res) { + res.sendFile(path.join(public, "index.html")); +}); +*/ + +//Setting up server +var server = app.listen(process.env.PORT || 4000, function () { + var port = server.address().port; + console.log("App now running on port", port); +}); diff --git a/rest/index.js b/rest/index.js index bba5463..8e5f11a 100644 --- a/rest/index.js +++ b/rest/index.js @@ -1,11 +1,16 @@ -const express = require('express') -const app = express() -const port = 3000 +const express = require("express"); +const db = require("../db"); //database -app.get('/', (req, res) => { - res.send('Hello World!') -}) +const router = express.Router(); -app.listen(port, () => { - console.log(`Example app listening at http://localhost:${port}`) -}) \ No newline at end of file +router.get("/", async (req, res, next) => { + try { + let results = await db.all(); + res.json(results); + } catch (e) { + console.log(e); + res.sendStatus(500); + } +}); + +module.exports = router; diff --git a/web/index.js b/web/index.js new file mode 100644 index 0000000..82ac630 --- /dev/null +++ b/web/index.js @@ -0,0 +1,7 @@ +//Basic promise +function getPokemon() { + fetch("http://localhost:4000/api/v2/allPokemon") + .then((response) => response.json()) + .then((data) => console.log(data)); +} +getPokemon();