From 81fbea62d9a81e438cc7a1f4721e18d657f99bfe Mon Sep 17 00:00:00 2001 From: hina latif Date: Wed, 24 Feb 2021 14:42:09 -0500 Subject: [PATCH 1/3] sql activity codes --- data/schema.sql | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/data/schema.sql b/data/schema.sql index e69de29..a576339 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -0,0 +1,85 @@ +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; \ No newline at end of file From 9facb82da83cedc76e12e1407e4ec8451bc52c04 Mon Sep 17 00:00:00 2001 From: hina latif Date: Mon, 1 Mar 2021 13:29:48 -0500 Subject: [PATCH 2/3] connected to data base --- db/index.js | 25 +++++++++++++ index.js | 31 +++++++++++----- package-lock.json | 91 +++++++++++++++++++++++++++++++++++++++++++---- package.json | 4 ++- rest/index.js | 35 +++++++++++++----- web/index.js | 7 ++++ 6 files changed, 167 insertions(+), 26 deletions(-) create mode 100644 db/index.js create mode 100644 web/index.js diff --git a/db/index.js b/db/index.js new file mode 100644 index 0000000..25d833c --- /dev/null +++ b/db/index.js @@ -0,0 +1,25 @@ +const mysql = require("mysql"); + +const connectionDb = mysql.createPool({ + //allows queries + connectionLimit: 10, + password: "United@2011@", + 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; \ No newline at end of file diff --git a/index.js b/index.js index bba5463..9f66afe 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); +}); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 1fca92a..b90db60 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,11 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==" + }, "body-parser": { "version": "1.19.0", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", @@ -63,6 +68,20 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, + "cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "requires": { + "object-assign": "^4", + "vary": "^1" + } + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -192,6 +211,11 @@ "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -213,16 +237,16 @@ "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==" }, "mime-db": { - "version": "1.45.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.45.0.tgz", - "integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==" + "version": "1.46.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.46.0.tgz", + "integrity": "sha512-svXaP8UQRZ5K7or+ZmfNhg2xX3yKDMUzqadsSqi4NCH/KomcH75MAMYAGVlvXn4+b/xOPhS3I2uHKRUzvjY7BQ==" }, "mime-types": { - "version": "2.1.28", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.28.tgz", - "integrity": "sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==", + "version": "2.1.29", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.29.tgz", + "integrity": "sha512-Y/jMt/S5sR9OaqteJtslsFZKWOIIqMACsJSiHghlCAyhf7jfVYjKBmLiX8OgpWeW+fjJ2b+Az69aPFPkUOY6xQ==", "requires": { - "mime-db": "1.45.0" + "mime-db": "1.46.0" } }, "ms": { @@ -230,11 +254,27 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, + "mysql": { + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz", + "integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==", + "requires": { + "bignumber.js": "9.0.0", + "readable-stream": "2.3.7", + "safe-buffer": "5.1.2", + "sqlstring": "2.3.1" + } + }, "negotiator": { "version": "0.6.2", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + }, "on-finished": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", @@ -253,6 +293,11 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, "proxy-addr": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", @@ -283,6 +328,20 @@ "unpipe": "1.0.0" } }, + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -336,11 +395,24 @@ "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" }, + "sqlstring": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz", + "integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A=" + }, "statuses": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + }, "toidentifier": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", @@ -360,6 +432,11 @@ "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", diff --git a/package.json b/package.json index 944e450..088de2a 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,8 @@ }, "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" } } diff --git a/rest/index.js b/rest/index.js index bba5463..7dd5c41 100644 --- a/rest/index.js +++ b/rest/index.js @@ -1,11 +1,28 @@ -const express = require('express') -const app = express() -const port = 3000 +// const express = require('express') +// const app = express() +// const port = 3000 -app.get('/', (req, res) => { - res.send('Hello World!') -}) +// app.get('/', (req, res) => { +// res.send('Hello World!') +// }) -app.listen(port, () => { - console.log(`Example app listening at http://localhost:${port}`) -}) \ No newline at end of file +// app.listen(port, () => { +// console.log(`Example app listening at http://localhost:${port}`) +// }) + +const express = require("express"); +const db = require("../db"); //database + +const router = express.Router(); + +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; \ No newline at end of file diff --git a/web/index.js b/web/index.js new file mode 100644 index 0000000..1184f40 --- /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(); \ No newline at end of file From 4525130fc88d893d2756420847c776ed295df53e Mon Sep 17 00:00:00 2001 From: hina latif Date: Tue, 2 Mar 2021 22:17:40 -0500 Subject: [PATCH 3/3] pokemon name and ID --- data/schema.sql | 15 +++++------ db/index.js | 25 ----------------- index.js | 24 ----------------- package.json | 2 +- rest/index.js | 72 ++++++++++++++++++++++++++++++------------------- web/index.html | 2 ++ web/index.js | 7 ----- web/script.js | 8 ++++++ 8 files changed, 61 insertions(+), 94 deletions(-) delete mode 100644 db/index.js delete mode 100644 index.js delete mode 100644 web/index.js create mode 100644 web/script.js diff --git a/data/schema.sql b/data/schema.sql index a576339..eb5e78c 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -23,9 +23,6 @@ type VARCHAR(255) NOT NULL 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); @@ -77,9 +74,9 @@ VALUES (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; \ No newline at end of file +-- 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; \ No newline at end of file diff --git a/db/index.js b/db/index.js deleted file mode 100644 index 25d833c..0000000 --- a/db/index.js +++ /dev/null @@ -1,25 +0,0 @@ -const mysql = require("mysql"); - -const connectionDb = mysql.createPool({ - //allows queries - connectionLimit: 10, - password: "United@2011@", - 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; \ No newline at end of file diff --git a/index.js b/index.js deleted file mode 100644 index 9f66afe..0000000 --- a/index.js +++ /dev/null @@ -1,24 +0,0 @@ -const express = require("express"); -const bodyParser = require("body-parser"); -const cors = require("cors"); -const apiRouter = require("./rest"); - -const app = express(); - -// 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); -}); \ No newline at end of file diff --git a/package.json b/package.json index 088de2a..45c4cdc 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" diff --git a/rest/index.js b/rest/index.js index 7dd5c41..598213d 100644 --- a/rest/index.js +++ b/rest/index.js @@ -1,28 +1,44 @@ -// const express = require('express') -// const app = express() -// const port = 3000 - -// app.get('/', (req, res) => { -// res.send('Hello World!') -// }) - -// app.listen(port, () => { -// console.log(`Example app listening at http://localhost:${port}`) -// }) - -const express = require("express"); -const db = require("../db"); //database - -const router = express.Router(); - -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; \ No newline at end of file +const express = require('express') +const mysql = require("mysql"); +const cors = require("cors"); +const app = express() +const port = 3000 + +app.use(cors()) + +app.get('/', (req, res) => { + res.send('Hello World!') +}) + app.get('/pokemon', async (req, res) => { + let customerData = await getCustomerData(); + res.send(customerData); + }) + + app.listen(port, () => { + console.log (`Example app listening at http://localhost:${port}`) + }) + + +async function getCustomerData(){ + + let con = mysql.createConnection ({ + + password: "United@2011@", + user: "root", + database: "pokedex", + host: "localhost", + }); + + let data = await new Promise ((resolve, reject) => { + con.query ("SELECT * FROM pokemon", (err, result) => { + (err) ? reject(err) : resolve(result); + + }) + + }) +con.end(); + +return data; + +} + diff --git a/web/index.html b/web/index.html index 1de928b..88a525c 100644 --- a/web/index.html +++ b/web/index.html @@ -7,5 +7,7 @@

Hello Pokemon!

+
+ \ No newline at end of file diff --git a/web/index.js b/web/index.js deleted file mode 100644 index 1184f40..0000000 --- a/web/index.js +++ /dev/null @@ -1,7 +0,0 @@ -//Basic promise -function getPokemon() { - fetch("http://localhost:4000/api/v2/allPokemon") - .then((response) => response.json()) - .then((data) => console.log(data)); - } - getPokemon(); \ No newline at end of file diff --git a/web/script.js b/web/script.js new file mode 100644 index 0000000..46b9f02 --- /dev/null +++ b/web/script.js @@ -0,0 +1,8 @@ + + fetch("http://localhost:3000/pokemon") + .then(response =>response.json()) + .then(customers => { + document.getElementById("customers").innerHTML = customers.map(customer => `
${customer.pokemon_id}: ${customer.name}
`).join("") + }); + +