-
Notifications
You must be signed in to change notification settings - Fork 38
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
still in progress #19
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
function menu(){ | ||
|
||
const menuClosure = { | ||
1: { | ||
id:1, | ||
title: "Strawberry cheesecake", | ||
price: 6, | ||
image: 'strawberrycheesecake.jpeg', | ||
description: 'A medium sized Strawberry Cheesecake', | ||
category: 'dessert' | ||
}, | ||
2: { | ||
id:2, | ||
title: 'Blueberry cheesecake', | ||
price: 6.50, | ||
image: 'blueberry cheesecake.jpeg', | ||
description: 'A medium sized Blueberry Cheesecake', | ||
category: 'dessert' | ||
}, | ||
3:{ | ||
id:3, | ||
title:'Chocolate cheesecake', | ||
price: 6.50, | ||
image: 'chocolate cheesecake.jpeg', | ||
description: 'A medium sized Chocolate Cheesecake', | ||
category: 'dessert' | ||
}, | ||
4:{ | ||
id:4, | ||
title:'chesse', | ||
price: 6.50, | ||
image: 'chocolate cheesecake.jpeg', | ||
description: 'cheese', | ||
category: 'main' | ||
}, | ||
5:{ | ||
id:5, | ||
title:'Lamb Kebab', | ||
price: 11.50, | ||
image: 'chocolate cheesecake.jpeg', | ||
description: 'A nice kebab', | ||
category: 'main' | ||
}, | ||
6:{ | ||
id:6, | ||
title:'flatiron steak', | ||
price: 10.50, | ||
image: 'chocolate cheesecake.jpeg', | ||
description: 'A nice steak', | ||
category: 'main' | ||
} | ||
|
||
} | ||
return { | ||
getAllItems(){ | ||
return menuClosure; | ||
}, | ||
|
||
getItem(itemId){ | ||
return menuClosure[itemId] | ||
}, | ||
} | ||
} | ||
|
||
|
||
|
||
function orders(){ | ||
const orderClosure = {} // order will start as empty since orders need to be placed | ||
let orderId =''//think I will need an order id | ||
return { | ||
getAllOrders(){ | ||
return orderClosure; | ||
}, | ||
|
||
getOrder(orderId){ | ||
return orderClosure[orderId] | ||
}, | ||
|
||
createNewOrder(newOrder){ | ||
const orderKeys = Object.keys(orderClosure); | ||
const highestiD = Math.max(...orderKeys); | ||
const newId = highestiD + 1; | ||
orders = Object.assign({},orderClosure,{[newId]:newOrder}) | ||
return orderClosure | ||
}, | ||
|
||
deleteStudent(orderId){ | ||
delete orderClosure[orderId] | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
module.exports = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great to see closures in use :) |
||
menu, | ||
orders | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,63 @@ const express = require('express'); | |
const bodyParser = require('body-parser'); | ||
const app = express(); | ||
|
||
const{ | ||
menu, orders | ||
} = require('./apiFunctions.js') | ||
|
||
const {getAllItems, getItem} = menu() | ||
const {getAllOrders,getOrder,createNewOrder} = orders() | ||
|
||
app.use(bodyParser.json()); | ||
app.use('/static', express.static('static')); | ||
app.set('view engine', 'hbs'); | ||
|
||
const menu = { | ||
1: { | ||
id: 1, | ||
name: "Strawberry cheesecake", | ||
price: 6 | ||
} | ||
}; | ||
|
||
|
||
app.get('/', function(req, res){ | ||
res.render('index'); | ||
}); | ||
|
||
//Menu Routes | ||
app.get('/menu', function(req, res){ | ||
|
||
const allItems = getAllItems() | ||
res.json(allItems) | ||
}); | ||
|
||
app.get('/menu/:itemId', function(req, res){ | ||
const item = getItem(req.params.itemId) | ||
if(item){ | ||
return res.json(item) | ||
} else res.status(404).json({error: 'this item does not exist'}) | ||
}); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
//Order Routes | ||
app.get('/order', function(req,res){ | ||
const allOrders = getAllOrders() | ||
res.json(allOrders) | ||
}) | ||
|
||
app.get('/order:orderId', function(req,res){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should probably be a slash after |
||
const specificOrder = getOrder() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get order needs to receive an order id to work |
||
res.json(specificOrder) | ||
}) | ||
|
||
app.post('/order', function(req,res){ | ||
const newOrder = req.body; | ||
res.status(201).json(createNewOrder(newOrder)) | ||
}) | ||
|
||
|
||
|
||
|
||
//Listen Route | ||
app.listen(8080, function(){ | ||
console.log('Listening on port 8080'); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,89 @@ | ||
import React from 'react'; | ||
import Homepage from './Homepage' | ||
import Basket from './Basket' | ||
import MenuResults from './MenuResults' | ||
|
||
import '../styles/App.scss'; | ||
|
||
class App extends React.Component { | ||
constructor(){ | ||
super(); | ||
this.state = { | ||
menu: {}, | ||
order: {}, //to add empty history of order | ||
|
||
|
||
} | ||
this.getOrder = this.getOrder.bind(this); | ||
// this.fetchMenu = this.fetchMenu.bind(this) | ||
this.sendOrder = this.sendOrder.bind(this) | ||
|
||
} | ||
|
||
componentDidMount(){ | ||
this.fetchMenu() | ||
} | ||
|
||
fetchMenu(){ | ||
fetch('/menu') | ||
.then(response => response.json()) | ||
.then(body => { | ||
console.log(body) | ||
this.setState({ | ||
menu: body, | ||
}) | ||
}) | ||
} | ||
|
||
sendOrder(){ | ||
this.setState({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just saw that the order gets sent from Basket. It might better to do fetch and clear data in one go so that you only clear order data once you know that the API request has been successful |
||
order: {} | ||
}) | ||
} | ||
|
||
|
||
getOrder(order) { | ||
const matchingOrders = Array.from(this.state.order).find(newOrder => { | ||
return order.id === newOrder.id; | ||
}); | ||
|
||
if (matchingOrders) { | ||
matchingOrders.quantity += order.quantity; | ||
matchingOrders.price += order.price; | ||
|
||
const ordersWithoutCurrentOrder = this.state.order.filter( | ||
newOrder => { | ||
return newOrder.id !== order.id; | ||
} | ||
); | ||
|
||
this.setState({ | ||
order: ordersWithoutCurrentOrder.concat(matchingOrders) | ||
}); | ||
} else { | ||
this.setState({ | ||
order: [...this.state.order, order] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Order is initialised to be an object, but here an array gets assigned |
||
}); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
render(){ | ||
return ( | ||
<div> | ||
Delivereat app | ||
<div className='homepage-header'> | ||
<Homepage/> | ||
</div> | ||
<Basket sendOrder={this.sendOrder} order={this.state.order}/> | ||
<MenuResults getOrder={this.getOrder} menu={this.state.menu}/> | ||
</div> | ||
) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't call the object a closure, because a closure is a combination of an outer function and inner function which retains access to parent scope. Here we are just storing some data so something like
menuData
would be more appropriate