Skip to content
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
22 changes: 22 additions & 0 deletions data/schema.sql
Original file line number Diff line number Diff line change
@@ -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)
);
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
48 changes: 42 additions & 6 deletions rest/index.js
Original file line number Diff line number Diff line change
@@ -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}`)
})
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

}
6 changes: 6 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="script.js"></script>
</head>
<body>

<p>Hello Pokemon!</p>
<div id="allCustomers"></div>



</body>
</html>
7 changes: 7 additions & 0 deletions web/script.js
Original file line number Diff line number Diff line change
@@ -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 => `<div>${customer.customerNumber}: ${customer.customerName}</div>`).join("")
});