From 7e98fc993ced9cd0cc55ad24052add446ad70ebc Mon Sep 17 00:00:00 2001 From: Stephanie Czetli Date: Wed, 24 Feb 2021 16:59:23 -0500 Subject: [PATCH 1/8] first commit - sql schema --- data/schema.sql | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/data/schema.sql b/data/schema.sql index e69de29..348bc9a 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -0,0 +1,45 @@ +DROP TABLE IF EXISTS poke_type; +DROP TABLE IF EXISTS pokemon; +CREATE TABLE IF NOT EXISTS pokemon ( + poke_id INT NOT NULL PRIMARY KEY, + poke_name varchar (100) NOT NULL, + poke_sprite varchar (1000) +); + +-- DROP TABLE IF EXISTS type; +CREATE TABLE IF NOT EXISTS type ( + type_id INT NOT NULL PRIMARY KEY, + type_name VARCHAR(40) NOT NULL +); + +-- DROP TABLE IF EXISTS poke_type; +CREATE TABLE IF NOT EXISTS poke_type ( + poke_type_id varchar(40) NOT NULL PRIMARY KEY, + poke_id INT NOT NULL, + FOREIGN KEY (poke_id) REFERENCES pokemon (poke_id), + type_id INT NOT NULL, + FOREIGN KEY (type_id) REFERENCES type (type_id) +); + +INSERT INTO pokemon +( poke_id, + poke_name, + poke_sprite +) +VALUES +("0", "bulbasaur", "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png"); + +INSERT INTO type ( + type_id, + type_name +) +VALUES +("0", "https://pokeapi.co/api/v2/type/12/"); + +INSERT INTO poke_type ( + poke_type_id, + poke_id, + type_id +) +VALUES +("0", "0", "0"); \ No newline at end of file From 6b7865b8c6d869b88151b44373118a7f9f4bf30a Mon Sep 17 00:00:00 2001 From: Stephanie Czetli Date: Mon, 1 Mar 2021 21:24:26 -0500 Subject: [PATCH 2/8] first commit - connected the pieces --- rest/index.js | 39 ++++++++++++++++++++++++++++++++++----- web/index.html | 4 ++++ web/script.js | 13 +++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 web/script.js diff --git a/rest/index.js b/rest/index.js index bba5463..feac206 100644 --- a/rest/index.js +++ b/rest/index.js @@ -1,11 +1,40 @@ const express = require('express') +const cors = require('cors') const app = express() const port = 3000 -app.get('/', (req, res) => { - res.send('Hello World!') -}) +const password = "myPasswordGoesHere" +let mysql = require('mysql'); + +let con = mysql.createConnection({ + host: "localhost", + user: "root", //your username + password: password, + database: "classicmodels" +}); + +app.use(cors()); + +con.connect(err => { + if(err) { + throw err; + } + con.query("SELECT * FROM customers", (err, result, fields) => { + if (err) { + throw err; + } + // console.log(result); //prints results to terminal + + app.get('/customer', (req, res) => { + res.send(result); //sends results to browser + }) + }); + +}); + +//Sets up server app.listen(port, () => { - console.log(`Example app listening at http://localhost:${port}`) -}) \ No newline at end of file + console.log(`Server listening at http://localhost:${ port }`) +}) + diff --git a/web/index.html b/web/index.html index 1de928b..5bfb91e 100644 --- a/web/index.html +++ b/web/index.html @@ -7,5 +7,9 @@

Hello Pokemon!

+

+ + + \ No newline at end of file diff --git a/web/script.js b/web/script.js new file mode 100644 index 0000000..5c31972 --- /dev/null +++ b/web/script.js @@ -0,0 +1,13 @@ +fetch('http://localhost:3000/customer') //connects with app.get () in rest/index.js file + // .then(res => res.json()); + .then(function (res) + { + res.json() + .then(function (res) + { + document.getElementById('fetchPokeData').innerHTML = res[3].customerName; + // console.log(res); + }); + + }); + \ No newline at end of file From 244c08b68edaa8cf713c10fe4f7c5b163cac8ccd Mon Sep 17 00:00:00 2001 From: Stephanie Czetli Date: Mon, 1 Mar 2021 22:00:37 -0500 Subject: [PATCH 3/8] 2nd commit: deleted lines 24-45 --- data/schema.sql | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/data/schema.sql b/data/schema.sql index 348bc9a..950ce9f 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -20,26 +20,3 @@ CREATE TABLE IF NOT EXISTS poke_type ( type_id INT NOT NULL, FOREIGN KEY (type_id) REFERENCES type (type_id) ); - -INSERT INTO pokemon -( poke_id, - poke_name, - poke_sprite -) -VALUES -("0", "bulbasaur", "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png"); - -INSERT INTO type ( - type_id, - type_name -) -VALUES -("0", "https://pokeapi.co/api/v2/type/12/"); - -INSERT INTO poke_type ( - poke_type_id, - poke_id, - type_id -) -VALUES -("0", "0", "0"); \ No newline at end of file From 77b769204dddc1c7abbabe168ad98b2d2c7d9d81 Mon Sep 17 00:00:00 2001 From: Stephanie Czetli Date: Tue, 2 Mar 2021 12:51:28 -0500 Subject: [PATCH 4/8] Added dependencies: nodemon, mysql & cors --- package.json | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 944e450..fa8d857 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "pokedex-app", "version": "1.0.0", "description": "![pickachu](https://media.giphy.com/media/xuXzcHMkuwvf2/giphy.gif)", - "main": "index.js", + "main": "script.js", "scripts": { "test": "mocha", "start": "node rest/index.js" @@ -18,6 +18,9 @@ }, "homepage": "https://github.com/code-differently/Pokedex-App#readme", "dependencies": { - "express": "^4.17.1" + "cors": "^2.8.5", + "express": "^4.17.1", + "mysql": "^2.18.1", + "nodemon": "^2.0.7" } } From 547f310b4b9127f7bb2cee071ed056d29bfd481f Mon Sep 17 00:00:00 2001 From: Stephanie Czetli Date: Thu, 4 Mar 2021 20:16:38 -0500 Subject: [PATCH 5/8] moved script tag inside head tags --- web/index.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/web/index.html b/web/index.html index 5bfb91e..8f7338b 100644 --- a/web/index.html +++ b/web/index.html @@ -4,12 +4,14 @@ Document + +

Hello Pokemon!

-

+
+ - \ No newline at end of file From 53ad33ccf85b880b1f16b94bedbb5aff66393fc0 Mon Sep 17 00:00:00 2001 From: Stephanie Czetli Date: Thu, 4 Mar 2021 20:18:37 -0500 Subject: [PATCH 6/8] revised fetch to return all customer data --- web/script.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/web/script.js b/web/script.js index 5c31972..2788d6a 100644 --- a/web/script.js +++ b/web/script.js @@ -1,13 +1,7 @@ -fetch('http://localhost:3000/customer') //connects with app.get () in rest/index.js file - // .then(res => res.json()); - .then(function (res) - { - res.json() - .then(function (res) - { - document.getElementById('fetchPokeData').innerHTML = res[3].customerName; - // console.log(res); + +fetch('http://localhost:3000/customers') + .then(res => res.json()) + // .then(console.log); + .then(customerData => { + document.getElementById("allCustomers").innerHTML = customerData.map(customer => `
${customer.customerNumber}: ${customer.customerName}
`).join("") }); - - }); - \ No newline at end of file From e6b065ad121d1da846026a7b6f4d9b5b1b985e9d Mon Sep 17 00:00:00 2001 From: Stephanie Czetli Date: Thu, 4 Mar 2021 20:20:18 -0500 Subject: [PATCH 7/8] wrapped db connection & query in function --- rest/index.js | 65 ++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/rest/index.js b/rest/index.js index feac206..333cf4f 100644 --- a/rest/index.js +++ b/rest/index.js @@ -1,40 +1,47 @@ +const password = "myPasswordGoesHere!" //my pw to access db goes here + const express = require('express') -const cors = require('cors') +let mysql = require('mysql') const app = express() -const port = 3000 +const PORT = 3000 +const cors = require('cors') -const password = "myPasswordGoesHere" -let mysql = require('mysql'); +app.use(cors()) //required to permit cross-origin resource sharing -let con = mysql.createConnection({ - host: "localhost", - user: "root", //your username - password: password, - database: "classicmodels" -}); +app.get('/customers', async (req, res) => { + let customerData = await getCustomerData(); + res.send(customerData); +}) -app.use(cors()); +app.listen(PORT, () => { //set up server + console.log(`Server is listening at http://localhost:${PORT}`) +}); -con.connect(err => { - if(err) { - throw err; - } - con.query("SELECT * FROM customers", (err, result, fields) => { - if (err) { - throw err; - } - // console.log(result); //prints results to terminal +async function getCustomerData(){ + + let con = mysql.createConnection({ //open db connection + host: "localhost", + user: "root", //your username + password: password, + database: "classicmodels" + }); - app.get('/customer', (req, res) => { - res.send(result); //sends results to browser + let data = await new Promise((resolve, reject) => { + con.query("SELECT * FROM customers", (err, result, fields) => + { + if (err) { + reject(err) + } + else { + resolve(result) + console.log(result) + } + //above as ternary - (err) ? reject(err) : resolve(result); }) + }) - }); + con.end(); //close db connection -}); - -//Sets up server -app.listen(port, () => { - console.log(`Server listening at http://localhost:${ port }`) -}) + return data; //from promise above +} From bc203b54bc1f85572b9b8cda1b0f9ad538fde65b Mon Sep 17 00:00:00 2001 From: Stephanie Czetli Date: Thu, 4 Mar 2021 20:24:22 -0500 Subject: [PATCH 8/8] added dependencies & changed scripts: start --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fa8d857..67391da 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "pokedex-app", "version": "1.0.0", "description": "![pickachu](https://media.giphy.com/media/xuXzcHMkuwvf2/giphy.gif)", - "main": "script.js", + "main": "rest/index.js", "scripts": { "test": "mocha", "start": "node rest/index.js"