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
76 changes: 76 additions & 0 deletions data/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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
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.

You generally don't want to include insertions in your schema, just database and table creations

(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;
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.

I would recommend moving this logic to the rest folder. While this file represents the persistence layer (the code who's responsible for talking to and maintaining the database) it is a service on the backend


const connectionDb = mysql.createPool({
//allows queries
connectionLimit: 10,
password: "secret",
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()
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.

I would recommend moving this logic to rest/index.js and moving the logic there to a file named APIRouter.js

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}`)
})
// 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);
});
23 changes: 14 additions & 9 deletions rest/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const express = require('express')
const app = express()
const port = 3000
const express = require("express");
const db = require("../db"); //database

app.get('/', (req, res) => {
res.send('Hello World!')
})
const router = express.Router();

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
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;
7 changes: 7 additions & 0 deletions web/index.js
Original file line number Diff line number Diff line change
@@ -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();