Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
83 changes: 83 additions & 0 deletions data/schema.sql
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing image src

-- 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;

25 changes: 25 additions & 0 deletions db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const mysql = require("mysql");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
31 changes: 22 additions & 9 deletions index.js
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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);
});
96 changes: 89 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"node-fetch": "^2.6.1"
}
}
35 changes: 26 additions & 9 deletions rest/index.js
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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
6 changes: 6 additions & 0 deletions web/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function getPokemon() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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();