-
Notifications
You must be signed in to change notification settings - Fork 22
Succesful fetch to api #12
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 all 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,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 | ||
| (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; | ||
| 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. 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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,24 @@ | ||
| const express = require('express') | ||
| const app = 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. I would recommend moving this logic to |
||
| 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); | ||
| }); | ||
| 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; |
| 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(); |
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.
You generally don't want to include insertions in your schema, just database and table creations