Skip to content
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

Finished hw #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
52 changes: 52 additions & 0 deletions controllers/pokemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const express = require('express');
const router = express.Router();

const Pokemon = require('../models/pokemon');

//index route
router.get('/', (req,res) => {
res.render('index.ejs', {
pokemon: Pokemon
});
})

//new route
router.get('/new', (req,res) => {
res.render('new.ejs');
})

//post route
router.post('/',(req,res) => {
Pokemon.push(req.body);
res.redirect('/pokemon');
})

//show route
router.get('/:id', (req,res) => {
res.render('show.ejs', {
pokemon: Pokemon[req.params.id],
id: req.params.id
})
})

//delete route
router.delete('/:id', (req,res) => {
Pokemon.splice(req.params.id,1);
res.redirect('/pokemon');
})

//edit route
router.get('/:id/edit', (req,res) => {
res.render('edit.ejs', {
pokemon: Pokemon[req.params.id],
index: req.params.id
})
})

//update route
router.put('/:id', (req,res) => {
Pokemon[req.params.id] = req.body;
res.redirect('/pokemon');
})

module.exports = router;
32 changes: 32 additions & 0 deletions models/pokemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const pokemon = [
{
name: "Bulbasaur",
img: "http://img.pokemondb.net/artwork/bulbasaur.jpg"
},
{
name: "Ivysaur",
img: "http://img.pokemondb.net/artwork/ivysaur.jpg"
},
{
name: "Venusaur",
img: "http://img.pokemondb.net/artwork/venusaur.jpg"
},
{
name: "Charmander",
img: "http://img.pokemondb.net/artwork/charmander.jpg"
},
{
name: "Charizard",
img: "http://img.pokemondb.net/artwork/charizard.jpg"
},
{
name: "Squirtle",
img: "http://img.pokemondb.net/artwork/squirtle.jpg"
},
{
name: "Wartortle",
img: "http://img.pokemondb.net/artwork/wartortle.jpg"
}
];

module.exports = pokemon;
94 changes: 85 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"cheerio": "^1.0.0-rc.2",
"ejs": "^2.6.1",
"express": "^4.16.3",
"method-override": "^3.0.0",
"mocha": "^5.2.0",
"supertest": "^3.3.0"
}
Expand Down
3 changes: 3 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: cornsilk;
}
20 changes: 20 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const pokemonController = require('./controllers/pokemon');

//middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride('_method'));
app.use('/pokemon', pokemonController);
app.use(express.static('public'));

app.get('/',(req, res) => {
res.send('This is the server responding to a get request')
})

app.listen(3000, () => {
console.log('server is listening on port 3000');
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd recommend organizing this file a little more carefully. have a separate section for controllers and put all your controller stuff there (even though they are implemented the same way middleware is). example:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const methodOverride = require('method-override');

//middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride('_method'));
app.use(express.static('public'));

// controllers
app.use('/pokemon', pokemonController);
const pokemonController = require('./controllers/pokemon');

app.get('/',(req, res) => {
	res.send('This is the server responding to a get request')
})

app.listen(3000, () => {
	console.log('server is listening on port 3000');
})

1 change: 1 addition & 0 deletions test/pokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const supertest = require('supertest');
const app = require('../server');
const should = chai.should()
const cheerio = require('cheerio');
const firstHref = $('.pokemon li:first-child a').attr('href');

describe('pokemon routes index and show', function(){
it('displays all pokemon at the index route', function(done){
Expand Down
16 changes: 16 additions & 0 deletions views/edit.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Pokemon Editor</h1>
<form method="POST" action="/pokemon/<%=index%>?_method=PUT">
Name: <input type="text" name="name" value="<%=pokemon.name%>"><br/>
Image: <input type="text" name="img" value="<%=pokemon.img%>"><br/>
<input type="submit" name="" value="Submit Edits"/>
</form>


</body>
</html>
25 changes: 25 additions & 0 deletions views/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Pokemon Index</title>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/style.css">
<style>
li {font-family: roboto;}
</style>
</head>
<body>
<h1>Pokemon Index</h1>
<ul class="pokemon">
<% for(let i = 0; i < pokemon.length; i++) {%>
<li>
<a href='/pokemon/<%=i%>'><%= pokemon[i].name %></a>
<form action='/pokemon/<%=i%>?_method=DELETE' method="POST">
<input type="submit" value="delete"/>
</form>
<a href='/pokemon/<%=i%>/edit'>Edit</a>
</li>
<% }%>
</ul>
</body>
</html>
16 changes: 16 additions & 0 deletions views/new.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>New Pokemon Page</h1>

<form action="/pokemon" method="POST">
Name: <input type="text" name="name"><br/>
Image: <input type="text" name="img"><br/>
<input type="submit">
</form>

</body>
</html>
15 changes: 15 additions & 0 deletions views/show.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Pokemon Show Page</h1>
<a href='/pokemon'>Index</a>
<p>Name: <%= pokemon.name%></p>
<img src="<%=pokemon.img%>"/>
<form action='/pokemon/<%=id%>?_method=DELETE' method="POST">
<input type="submit" value="Delete"/>
</form>
</body>
</html>