-
Notifications
You must be signed in to change notification settings - Fork 22
Pokedex codes for mysql activity #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| 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 | ||
| -- FOREIGN KEY (type_id) REFERENCES pokemon_type(type_id) | ||
| ); | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to submit queries in schema |
||
| 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; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| const mysql = require("mysql"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should all be in rest |
||
|
|
||
| const connectionDb = mysql.createPool({ | ||
| //allows queries | ||
| connectionLimit: 10, | ||
| password: "Pakistan@777", | ||
| 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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,24 @@ | ||
| const express = require('express') | ||
| const app = express() | ||
| const port = 3000 | ||
| const express = require("express"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should all be in rest |
||
| 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}`) | ||
| }) | ||
| // 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); | ||
| }); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}`) | ||
| }) | ||
| // app.listen(port, () => { | ||
| // console.log(`Example app listening at http://localhost:${port}`) | ||
| // }) | ||
|
|
||
| const express = require("express"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice import! |
||
| 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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| function getPokemon() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should display pokemon on webpage. Use your code from the group pokedex project here |
||
| fetch("http://localhost:4000/api/v2/allPokemon") | ||
| .then((res) => res.json()) | ||
| .then((data) => console.log(data)); | ||
| } | ||
| getPokemon(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing image src