Permalink
Browse files

comitting files to push to new remote repo.

  • Loading branch information...
laustinr90 committed Feb 13, 2020
0 parents commit 24fff60918c655bed8db89ff045f50bbe29d673b
Showing with 285 additions and 0 deletions.
  1. +72 −0 controllers/shop.js
  2. +51 −0 models/shop.js
  3. +18 −0 package.json
  4. +62 −0 readme.md
  5. +82 −0 server.js
@@ -0,0 +1,72 @@
/* 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.send(shopsApi.getShops());
})

shopRouter.get('/new', function(req, res) {
res.send(shopsApi.createShop());
})

shopRouter.get('/:index', function(req,res){
res.send(shopsApi.getShop(req.params.index));
})

shopRouter.post('/', function(req,res){
shopsApi.addShop(req.body);
res.status(200).end();
})

shopRouter.put('/:index', function(req,res){
shopsApi.updateShop(req.params.index, req.body);
res.status(200).end();
})

shopRouter.delete('/:index', function(req,res){
shopsApi.deleteShop(req.params.index)
res.status(200).end();
})


/* Step 6
*
* Export the router from the file.
*
*/
module.exports = {
shopRouter
}

@@ -0,0 +1,51 @@
/*
* 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) {
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,
};
@@ -0,0 +1,18 @@
{
"name": "coffee-shop",
"version": "1.0.0",
"description": "# Setup",
"main": "server.js",
"dependencies": {
"express": "^4.17.1",
"hbs": "^4.0.4",
"method-override": "^3.0.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC"
}
@@ -0,0 +1,62 @@
# express-intro-exercise

## Objective

- Create an HTTP web wrapper around a set of model code.

# Setup

Follow the instructions for setting up a new project from the
[mehn-template-project readme](../mehn-template-project/readme.md).

NOTE: be sure that `server.js` uses the `express.json()` middleware and _not_
the `express.urlencoded()` middleware.


# Create API

1. Copy the `models/template.js` to `models/shops.js`
2. In `models/shops.js` create a set of functions to perform the
following:

| function name | parameters | return |
| :------------ | :------------ | :-------------|
| getShops | n/a | list of shops |
| getShop | `index` | single shop |
| createShop | n/a | new shop |
| addShop | `newShop` | n/a |
| deleteShop | `index` | n/a |
| updateShop | `index, shop` | n/a |

Each coffee shop should have the following schema:

- name (string)
- employees (number)
- currentlyOpen (boolean)

# Create Web Server

1. Copy the `controllers/template.js` file to `controllers/shops.js`
2. In `controllers/shops.js` create as set of HTTP request handlers to
do the following:

| path | Method | model function to call |
| :-------------- | :----- | :--------------------- |
| `/shops` | GET | `getShops` |
| `/shops/:index` | GET | `getShop` |
| `/shops/new` | GET | `createShop` |
| `/shops` | POST | `addShop` |
| `/shops/:index` | DELETE | `deleteShop` |
| `/shops/:index` | PUT | `updateShop` |

# Strech Goals

1. Add some more methods to your model that modify the data inside of
the coffee shop (such as adding a menu item or changing the name)
and write an HTTP request handler around that.
2. create an other model called employees. Create a controller for
these employees
3. add a key to the employees model called `shopIndex`. Add a method
that will get all employees given the value of `shopIndex`.
4. Write an controller function that uses the function from the
previous step.
@@ -0,0 +1,82 @@
/*
* 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()
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())

/* Step 3.b
*
* add method-override middleware for "faking" DELETE and PUT/PATCH requests
*/
app.use(methodOverride('_method'))

/* Step 3.c
*
* use the `./public` directory to host static resources such as css and
* image files
*/
app.use(express.static(__dirname+"/public"))

/* 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(`App is listening on PORT ${PORT}`)
})

0 comments on commit 24fff60

Please sign in to comment.