Skip to content
Open

done #397

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
23 changes: 23 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
29 changes: 23 additions & 6 deletions controllers/pokemon.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
const express = require('express');
const router = express.Router();
const db = require('../moduls')

// GET /pokemon - return a page with favorited Pokemon
router.get('/', (req, res) => {
// TODO: Get all records from the DB and render to view
res.send('Render a page of favorites here');
router.get('/', async (req, res) => {
let allFavePokemon = await db.pokemon.findAll()
res.render('pokemon/index', {
pokemon: allFavePokemon
});
});

//GET form /pokemon/name - return a page with on a specific pokemon

router.get('/name', async (req, res) => {
axios.get(`http://pokeapi.co/api/v2/pokemon/${req.params.name}`).then(apiResponse => {
let pokemon = apiResponse.data
console.log(pokemon)
res.render('pokemon/show', {
pokemon: pokemon,
officialArt: pokemon.sprites.other['official-artwork'].font_default
})
})
})

// POST /pokemon - receive the name of a pokemon and add it to the database
router.post('/', (req, res) => {
// TODO: Get form data and add a new record to DB
res.send(req.body);
router.post('/', async (req, res) => {
let newPokemon = await db.pokemon.create({name: req.body.name})
console.log(newPokemon)
res.redirect('/pokemon');
});

module.exports = router;
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ app.use(ejsLayouts);

// GET / - main index of site
app.get('/', (req, res) => {
// TODO: use updated url http://pokeapi.co/api/v2/pokemon/?offset=0&limit=151
let pokemonUrl = 'http://pokeapi.co/api/v2/pokemon/?offset=0&limit=151';
// Use request to call the API
axios.get(pokemonUrl).then(apiResponse => {
Expand Down
43 changes: 43 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const process = require('process');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (
file.indexOf('.') !== 0 &&
file !== basename &&
file.slice(-3) === '.js' &&
file.indexOf('.test.js') === -1
);
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
Loading