Permalink
Browse files

changes with solutions

  • Loading branch information...
maij committed Feb 14, 2020
1 parent e487842 commit 14c56d8a23c7c225bc6399519b9f9e509d4b7160
Showing with 227 additions and 113 deletions.
  1. +66 −45 controllers/shops.js
  2. +7 −4 create.hbs
  3. +62 −0 models/shops.js
  4. +0 −27 models/user.js
  5. +75 −13 server.js
  6. 0 shop.hbs
  7. 0 shops.hbs
  8. +12 −12 views/shop.hbs
  9. +5 −12 views/shops.hbs
@@ -1,53 +1,74 @@
function appIndex(req, res) {
res.send('Hello from the application index page');
}
/* Step 1 import express
*
*/
const express = require('express')

module.exports = {
index: appIndex,
};

// in controllers/shops.js modify the .get('/shops' route to render the shops/shops
// template. You will need to do the following:
/* Step 2
*
* Import the api files from the models
*
* TODO: change the file path to the models file you'll need to use.
* TODO: rename this from `shopsApi` to something more sensible (e.g:
* `shopsAPI`)
*
* NOTE: You may need to import more than one API to create the
* controller you need.
*
*/
const shopsApi = require('../models/shop.js')

/* Step 3
*
* Create a new router.
*
* the router will "contain" all the request handlers that you define in this file.
* TODO: rename this from shopRouter to something that makes sense. (e.g:
* `shopRouter`)
*/
const shopRouter = express.Router()


// replace res.send with res.render
// set the context (2nd argument to res.render) to {shops: <results from calling
// getShops()>}
/* Step 4
*
*/

shopRouter.get('/', function(req,res) {
res.render('shops/shops', { shops: shopsApi.getShops() });
})

shopRouter.get('/new', function(req, res) {
res.render('shops/create', { shop: shopsApi.createShop() })
})

shopRouter.get('/', function(req, res){
res.render('shops/shops', {shops: shopsApi.getShops()});
shopRouter.get('/:index', function(req,res){
res.render('shops/shop', {
shop: shopsApi.getShop(req.params.index),
index: req.params.index,
})
})

const express = require('express');
const router = express.Router();

const userModel = require('../models/user.js');

router.get('/', function(req, res) {
const allShops = getShops.index();
res.render('user/index', {shops: allShops});
});

router.get('/new', function(req, res) {
res.render('shop/new');
});

router.post('/', function(req, res) {
const newShop = req.body;
shopModel.create(newShop);
res.send('ok');
});

router.get('/:index', function(req, res) {
res.render(
'shop/show',
{
shop: shopModel.show(req.params.index)
});
});

router.delete('/:index', function(req, res) {
shopModel.delete(req.params.index);
res.send('shop deleted');
shopRouter.post('/', function(req,res){
shopsApi.addShop(req.body);
res.redirect('/shops');
})

module.exports = router;
shopRouter.put('/:index', function(req,res){
shopsApi.updateShop(req.params.index, req.body);
res.redirect('/shops');
})

shopRouter.delete('/:index', function(req,res){
shopsApi.deleteShop(req.params.index)
res.redirect('/shops');
})


/* Step 6
*
* Export the router from the file.
*
*/
module.exports = {
shopRouter
}
@@ -1,5 +1,8 @@
<form> method="POST" action="/shops">
<input> type="text" value="{{<TODO: model.stringMember (e.g; account.name)}}" placeholder="name" name="<TODO>"</input>
<input> type="checkbo" name="<TODO: model.booleanMember (e.g; account.isActive)" value="true</input></TODO:>
<input type="submit" value="Create Shop"/>
<form action="/shops" method="POST">
Name: <input type="text" name="name" value="{{ shop.name }}">
Employees: <input type="number" name="employees" values="{{ shop.employees }}">
Currently open:
<input type="checkbox" name="currentlyOpen"
{{#if shop.currentlyOpen }} checked {{/if}}>
<input type="submit" value="Add Shop">
</form>
@@ -0,0 +1,62 @@
/*
* Place all functions, classes, and/or DB schemas here for a single
* model.
*/
global.shops = [
{
'name': 'Starbucks (c)',
'employees': 100000,
'currentlyOpen': true,
},
{
'name': 'Dancing Goats Cafe',
'employees': 100,
'currentlyOpen': false,
},
];

/*
* TODO: delete this it's just a sample
*
*/
function createShop() {
return {
name: "",
employees: 0,
currentlyOpen: false
};
}

function getShops() {
return global.shops;
}

function getShop(i) {
return global.shops[i];
}

function addShop(newShop) {
global.shops.push(newShop);
}

function updateShop(i, shop) {
global.shops[i] = shop;
}

function deleteShop(i) {
global.shops.splice(i, 1);
}

/* Step 1
*
* TODO: export all functions from this file by adding their names as keys to this
* object
*/
module.exports = {
addShop,
createShop,
deleteShop,
getShop,
getShops,
updateShop,
};

This file was deleted.

Oops, something went wrong.
@@ -1,21 +1,83 @@

/*
* This is the template for a server.js file. Follow the steps below and read
* the comments for creating your own (or you can just copy this file).
*/

/* Step 1
*
* Import needed packages
*
*/
const express = require('express')
const app = express();
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'hbs');
const app = express()
const methodOverride = require('method-override')

/* Step 2
*
* import routers from controllers/
*
*/
const { shopRouter } = require('./controllers/shop.js')


/* Step 3
*
* Register middleware...
*/

/* Step 3.a
* ...to parse the body of the HTTP requests from a URL encoded string
*/
app.use(express.urlencoded())

/* Step 3.b
*
* ...to parse the body of the HTTP requests from a JSON string
* replace the above line with this
*/
//app.use(express.json())

const shopController = require('./controllers/shops.js');
app.use('/shop', shopController);
/* Step 3.b
*
* add method-override middleware for "faking" DELETE and PUT/PATCH requests
*/
app.use(methodOverride('_method'))

app.get('/', (req, res) => {
res.render('index')
});
/* Step 3.c
*
* use the `./public` directory to host static resources such as css and
* image files
*/
app.use(express.static(__dirname+"/public"))

// process.env.PORT is necessary for deployment to Heroku
// If environment variable is not provided, default to 3000
const PORT = process.env.PORT || 3000
/* Step 3.b
*
* set the view engine of express to use the hbs (handlebars) package
*/
app.set('view engine', 'hbs')


/* Step 4
*
* add router for the application to use. The first argument is a prefix to all
* the paths defined in the router.
*/
app.use('/shops', shopRouter)


/* Step 5
*
* Set the port the server is to run on
*
* NOTE: keep these lines at the bottom of the file
*/
const PORT = process.env.PORT || 3000

/* Step 6
*
* Start the server
*/
app.listen(PORT, () => {
console.log(`Server is listening on PORT: ${PORT}`)
console.log(`App is listening on PORT ${PORT}`)
})

No changes.
No changes.
@@ -1,12 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Views</title>
<!-- Static assets can go in here! -->
<!-- <link rel="stylesheet" type="text/css" href="/css/styles.css"> -->
</head>
<body>
{{{ body }}}
<!-- The body tag is what our templates will be replacing. -->
</body>
</html>
<form action="/shops/{{ index }}?_method=PUT" method="POST">
Name: <input type="text" name="name" value="{{ shop.name }}">
Employees: <input type="number" name="employees" value="{{ shop.employees }}">
Currently open:
<input type="checkbox" name="currentlyOpen"
{{#if shop.currentlyOpen }} checked {{/if}}>
<input type="submit" value="Update Shop">
</form>

<form action="/shops/{{ index }}?_method=DELETE" method="POST">
<input type="submit" value="Delete Shop">
</form>
@@ -1,15 +1,8 @@

{{!-- views/shops/shops.hbs to list the names of all the shops using --}}
{{#each}}...{{/each}}

<a href="/shops/{{@index}}">...</a>

<ul>
{{#each shops}}
<li>{{ Target }}</li>
<li>{{ Publix }}</li>
<li>{{ WalMart }}</li>
<li>{{ Ralphs }}</li>
<li>{{ Macys }}</li>
<ul>
{{#each shops }}
<li>
<a href="/shops/{{ @index }}">{{ name }}</a>
</li>
{{/each}}
</ul>

0 comments on commit 14c56d8

Please sign in to comment.