diff --git a/config/config.json b/config/config.json new file mode 100644 index 00000000..cc42d587 --- /dev/null +++ b/config/config.json @@ -0,0 +1,7 @@ +{ + "development": { + "database": "pokedex", + "host": "127.0.0.1", + "dialect": "postgres" + } +} diff --git a/db-test.js b/db-test.js new file mode 100644 index 00000000..5045f8ad --- /dev/null +++ b/db-test.js @@ -0,0 +1,37 @@ +// Make sure to require your models in the files where they will be used. +const db = require('./models'); + +db.pokemon.create({ + name: 'pikachu' + }) + .then(poke => { + console.log('Created: ', poke.name) + db.pokemon.findOne({ + where: { + name: 'pikachu' + } + }) + .then(poke => { + console.log('Found: ', poke.name) + }) + .catch(console.log) + }) + .catch(console.log) + +// create some pokemon with async/await syntax +async function createPokemon() { + try { + const newPokemon = await db.pokemon.create({ name: 'charizard' }) + console.log('the new pokemon is:', newPokemon) + const foundPokemon = await db.pokemon.findOne({ + where: { + name: 'charizard' + } + }) + console.log('the found pokemon is:', foundPokemon) + } catch (err) { + console.log(err) + } +} + +createPokemon() \ No newline at end of file diff --git a/index.js b/index.js index 77cdf635..df007bcf 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,17 @@ const express = require('express'); const axios = require('axios'); const ejsLayouts = require('express-ejs-layouts'); - +const db = require('./models') const app = express(); const port = process.env.PORT || 3000; app.set('view engine', 'ejs'); app.use(express.urlencoded({ extended: false })); app.use(ejsLayouts); +app.use(express.static('public')) + +const path = require('path') +app.set('views', path.join(__dirname, 'views')); // GET / - main index of site app.get('/', (req, res) => { @@ -20,6 +24,43 @@ app.get('/', (req, res) => { }) }); +app.get('/pokemon', async (req, res) => { + try { + const allPokemon = await db.pokemon.findAll(); + console.log(__dirname) + res.render('./pokemon/index.ejs', { pokemon: allPokemon }); + } catch (err) { + console.log(err); + res.status(500).send('Internal Server Error'); + } +}); + +app.post('/pokemon', async (req, res) => { + const { name } = req.body; + try { + const newPokemon = await db.pokemon.findOrCreate({ where: {name} }); + console.log('Created new Pokemon:', newPokemon.name); + res.redirect('/pokemon'); + } catch (err) { + console.log(err); + res.status(500).send('Internal Server Error'); + } +}); + + +app.get('/pokemon/:name', async (req, res) => { + const { name } = req.params + try { + const response = await axios.get(`https://pokeapi.co/api/v2/pokemon/${name}`) + const pokemon = response.data; + res.render('pokemon/show', { pokemon }) + } catch(err) { + console.log(err) + res.status(404).send('Pokemon not found') + } +}) + + // Imports all routes from the pokemon routes file app.use('/pokemon', require('./controllers/pokemon')); diff --git a/migrations/20230404004021-create-pokemon.js b/migrations/20230404004021-create-pokemon.js new file mode 100644 index 00000000..b435d928 --- /dev/null +++ b/migrations/20230404004021-create-pokemon.js @@ -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'); + } +}; \ No newline at end of file diff --git a/models/index.js b/models/index.js new file mode 100644 index 00000000..53040a00 --- /dev/null +++ b/models/index.js @@ -0,0 +1,37 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const Sequelize = require('sequelize'); +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'); + }) + .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; \ No newline at end of file diff --git a/models/pokemon.js b/models/pokemon.js new file mode 100644 index 00000000..d6eae406 --- /dev/null +++ b/models/pokemon.js @@ -0,0 +1,23 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class pokemon extends Model { + /** + * Helper method for defining associations. + * This method is not a part of Sequelize lifecycle. + * The `models/index` file will call this method automatically. + */ + static associate(models) { + // define association here + } + } + pokemon.init({ + name: DataTypes.STRING + }, { + sequelize, + modelName: 'pokemon', + }); + return pokemon; +}; \ No newline at end of file diff --git a/public/css/styles.css b/public/css/styles.css new file mode 100644 index 00000000..baa2b23f --- /dev/null +++ b/public/css/styles.css @@ -0,0 +1,11 @@ +body { + background-color: gray; +} + +a { + color: gray; +} + +a:visited { + color: rgb(9, 9, 9); +} \ No newline at end of file diff --git a/views/index.ejs b/views/index.ejs index e5c0ff30..211ca16c 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -1,12 +1,14 @@

The Original 151 Pokemon

+ Home Favorites +

<% pokemon.forEach(function(pokemon) { %>
-

<%= pokemon.name %>

+

<%= pokemon.name %>

diff --git a/views/layout.ejs b/views/layout.ejs index cf20551c..e4abf26d 100644 --- a/views/layout.ejs +++ b/views/layout.ejs @@ -7,6 +7,7 @@ + <%- body %> diff --git a/views/pokemon/index.ejs b/views/pokemon/index.ejs new file mode 100644 index 00000000..33bce1bc --- /dev/null +++ b/views/pokemon/index.ejs @@ -0,0 +1,14 @@ +
+

The Original 151 Pokemon

+

+ Home + Favorites + +

+ + <% pokemon.forEach(function(pokemon) { %> +
+

<%= pokemon.name %>

+
+ <% }); %> +
diff --git a/views/pokemon/show.ejs b/views/pokemon/show.ejs new file mode 100644 index 00000000..9983b419 --- /dev/null +++ b/views/pokemon/show.ejs @@ -0,0 +1,41 @@ +
+

The Original 151 Pokemon

+

+ Home + Favorites + +

+ + +

<%= pokemon.name.charAt(0).toUpperCase() + pokemon.name.slice(1)%>

+ + +

Types:

+
    + <% pokemon.types.forEach((type) => { %> +
  • <%= type.type.name %>
  • + <% }) %> +
+ +

+

Abilities:

+
    + <% pokemon.abilities.forEach((ability) => { %> +
  • <%= ability.ability.name %>
  • + <% }) %> +
+

Base Stats:

+
    + <% pokemon.stats.forEach((stat) => { %> +
  • <%= stat.stat.name %>: <%= stat.base_stat %>
  • + <% }) %> +
+

Moves:

+
    + <% pokemon.moves.forEach((move) => { %> +
  • <%= move.move.name %>
  • + <% }) %> +
+ + +