diff --git a/data/schema.sql b/data/schema.sql index e69de29..950ce9f 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -0,0 +1,22 @@ +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) +); diff --git a/package.json b/package.json index 944e450..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": "index.js", + "main": "rest/index.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" } } diff --git a/rest/index.js b/rest/index.js index bba5463..333cf4f 100644 --- a/rest/index.js +++ b/rest/index.js @@ -1,11 +1,47 @@ +const password = "myPasswordGoesHere!" //my pw to access db goes here + const express = require('express') +let mysql = require('mysql') const app = express() -const port = 3000 +const PORT = 3000 +const cors = require('cors') + +app.use(cors()) //required to permit cross-origin resource sharing -app.get('/', (req, res) => { - res.send('Hello World!') +app.get('/customers', async (req, res) => { + let customerData = await getCustomerData(); + res.send(customerData); }) -app.listen(port, () => { - console.log(`Example app listening at http://localhost:${port}`) -}) \ No newline at end of file +app.listen(PORT, () => { //set up server + console.log(`Server is listening at http://localhost:${PORT}`) +}); + +async function getCustomerData(){ + + let con = mysql.createConnection({ //open db connection + host: "localhost", + user: "root", //your username + password: password, + database: "classicmodels" + }); + + 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 + + return data; //from promise above + +} diff --git a/web/index.html b/web/index.html index 1de928b..8f7338b 100644 --- a/web/index.html +++ b/web/index.html @@ -4,8 +4,14 @@ Document + +

Hello Pokemon!

+
+ + + \ No newline at end of file diff --git a/web/script.js b/web/script.js new file mode 100644 index 0000000..2788d6a --- /dev/null +++ b/web/script.js @@ -0,0 +1,7 @@ + +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("") + });