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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ Temporary Items

# Linux trash folder which might appear on any partition or disk
.Trash-*
node_modules
9 changes: 9 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"development": {
"username": "sequelize",
"password": "sequelize",
"database": "pokedex",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
22 changes: 13 additions & 9 deletions controllers/pokemon.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const express = require('express');
const router = express.Router();
const db = require('../models');
const Pokemon = db.pokemon;

// 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 Pokemon.findAll();
console.log(allFavePokemon);
res.render('pokemon', {
pokemon: allFavePokemon
});
});

// 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 Pokemon.create({name: req.body.name});
console.log(newPokemon);
res.redirect('pokemon');
});

module.exports = router;
module.exports = router;
Empty file added db-test.js
Empty file.
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const express = require('express');
const axios = require('axios');
const ejsLayouts = require('express-ejs-layouts');
const db = require('./models');
const Pokemon = db.pokemon;

const app = express();
const port = process.env.PORT || 3000;
Expand All @@ -26,3 +28,27 @@ app.use('/pokemon', require('./controllers/pokemon'));
app.listen(port, () => {
console.log('...listening on', port );
});



app.get('/:name', async (req, res) => {
try {
// Make a request to the PokeAPI to get information about the Pokemon
const response = await axios.get(`https://pokeapi.co/api/v2/pokemon/${req.params.name}`);

const { abilities, sprites, stats, moves } = response.data;

// Render the show page with information about the Pokemon
res.render('pokemonpage', { pokemon: req.params.name, abilities, sprites, stats, moves });
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});

// app.js
// app.js




28 changes: 28 additions & 0 deletions migrations/20230404051423-create-pokemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('pokemons', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('pokemons');
}
};
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;
29 changes: 29 additions & 0 deletions models/pokemons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const { Model } = require('sequelize');
const sequelize = require('./index');

module.exports = (sequelize, DataTypes) => {
class pokemon extends Model {
static associate(models) {
// define association here
}
}
pokemon.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false
}
}, {
sequelize,
modelName: 'pokemon',
});

// Export the `pokemon` model
return pokemon;
};

Loading