Permalink
Browse files

homework

  • Loading branch information...
Claylee3095 committed Feb 14, 2020
0 parents commit 50c1aa569ac161ea0b928b8291e5f8ee3592eff8
Showing with 779 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +73 −0 controllers/shop.js
  3. +53 −0 models/shop.js
  4. +473 −0 package-lock.json
  5. +18 −0 package.json
  6. +62 −0 readme.md
  7. +82 −0 server.js
  8. +5 −0 views/shops.hbs
  9. +12 −0 views/shops/create.hbs
  10. 0 views/shops/shop.hbs
@@ -0,0 +1 @@
node_modules
@@ -0,0 +1,73 @@
/* Step 1 import express
*
*/
const express = require('express')


/* 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()


/* 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('/:index', function(req,res){
const index = req.params.index
res.render('shops/shop', { shop: shopsApi.getShop(index), index });
})

shopRouter.post('/', function(req,res){
shopsApi.addShop(req.body);
res.redirect('/shops');
})

shopRouter.put('/:index', function(req,res){
const index = req.params.index;
shopsApi.updateShop(index, req.body);
res.render('shops/shop', { shop: shopsApi.getShops(index), index, saved: true})
})

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
}

@@ -0,0 +1,53 @@
/*
* Place all functions, classes, and/or DB schemas here for a single
* model.
*/
global.shops = [];

/*
* 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) {
newShop.currentlyOpen = newShop.currentlyOpen || false;
newShop.employees = newShop.employees || 0;
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,
};
Oops, something went wrong.

0 comments on commit 50c1aa5

Please sign in to comment.