Permalink
Browse files

finished everything except order submission

  • Loading branch information...
troygoode committed Apr 17, 2012
1 parent 4835911 commit e9f37723892b22c901513210213174608a13b5ba
Showing with 115 additions and 46 deletions.
  1. +42 −0 public/express-lane-client.css
  2. +12 −6 repositories/cart.js
  3. +10 −16 repositories/orders.js
  4. +7 −2 routes/cart.js
  5. +8 −11 routes/checkout.js
  6. +34 −4 views/cart.jade
  7. +0 −6 views/checkout.jade
  8. +1 −0 views/layout.jade
  9. +1 −1 views/product.jade
@@ -0,0 +1,42 @@
+body{
+ padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
+ cursor: default;
+}
+
+table#cart{
+ margin-top: 1em;
+}
+table#cart thead{
+ background-color: #666;
+ color: #fff;
+}
+table#cart th{
+ padding: .5em 1em;
+ text-align: left;
+}
+table#cart td{
+ padding: .5em 1em;
+ text-align: right;
+}
+table#cart td:first-child{
+ padding: .5em 1em;
+ text-align: left;
+}
+table#cart form{
+ margin: 0;
+ padding: 0;
+}
+table#cart tfoot{
+ border-top: 1px solid #666;
+}
+
+.btn{
+ margin-right: 5px;
+}
+.btn:last-child{
+ margin-right: 0px;
+}
+
+.buttons form{
+ display: inline-block;
+}
View
@@ -5,24 +5,30 @@ module.exports = function(){
var cart = session.cart || {};
var itemsInCart = [];
for(var key in cart)
- itemsInCart.push({ id: key, quantity: cart[key] });
+ itemsInCart.push(cart[key]);
cb(null, itemsInCart);
},
increment: function(session, product, cb){
var cart = session.cart || {};
- var quantity = (cart[product._id] || 0) + 1;
- cart[product._id] = quantity;
+ var productInCart = cart[product._id] || {product: product, quantity: 0};
+ productInCart.quantity++;
+ cart[product._id] = productInCart;
session.cart = cart;
- cb(null, quantity);
+ cb(null, productInCart.quantity);
},
remove: function(session, product, cb){
var cart = session.cart || {};
- var quantity = cart[product._id];
+ var productInCart = cart[product._id];
delete cart[product._id];
session.cart = cart;
- cb(null, quantity);
+ cb(null, productInCart.quantity);
+ },
+
+ empty: function(session, cb){
+ session.cart = null;
+ cb(null);
}
};
View
@@ -1,34 +1,28 @@
var request = require('request');
-var makeRequest = function(options, cb) {
- request(options, function(err, res, body) {
- if (err !== null) {
- cb(err);
- } else if (res.statusCode !== 200 && res.statusCode !== 201) {
- cb(res.statusCode + ": " + body);
- } else {
- cb(null, body);
- }
- });
-};
-
module.exports = function(config) {
return {
- find: function(cb){
+ find: function(emailAddress, cb){
var url = config.serverUrl + '/orders';
var options = { json: true, method: 'get', url: url };
- makeRequest(options, cb);
+ request(options, function(err, res, body) {
+ if (err !== null) cb(err);
+ else cb(null, body);
+ });
},
- add: function(order, cb){
+ add: function(emailAddress, productsInCart, cb){
cb(null, null);
},
mark_complete: function(id, cb) {
var url = config.serverUrl + '/order/' + id + '/complete';
var options = { json: true, method: 'post', url: url };
- makeRequest(options, cb);
+ request(options, function(err, res, body) {
+ if (err !== null) cb(err);
+ else cb(null, body);
+ });
}
};
View
@@ -1,8 +1,13 @@
module.exports = function(app, cartRepo, productsRepo){
app.get('/cart', function(req, res){
- cartRepo.find(req.session, function(err, products){
- res.render('cart', {products: products});
+ cartRepo.find(req.session, function(err, productsInCart){
+ res.render('cart', {
+ cart: productsInCart,
+ total: productsInCart.reduce(function(previous, current){
+ return previous + (current.quantity * current.product.price);
+ }, 0)
+ });
});
});
View
@@ -1,21 +1,18 @@
module.exports = function(app, cartRepo, ordersRepo){
- app.get('/checkout', function(req, res){
- res.render('checkout');
- });
-
- app.post('/checkout', function(req, res){
- cartRepo.find(req.session.emailAddress, function(products){
- var order = {}; //TODO
- ordersRepo.add(order, function(){
- res.redirect('/orders');
+ app.post('/orders', function(req, res){
+ cartRepo.find(req.session, function(err, productsInCart){
+ ordersRepo.add(req.session.emailAddress, productsInCart, function(err, order){
+ cartRepo.empty(req.session, function(err){
+ res.redirect('/orders');
+ });
});
});
});
app.get('/orders', function(req, res){
- ordersRepo.find(req.session.emailAddress, function(orders){
- res.render('orders');
+ ordersRepo.find(req.session.emailAddress, function(err, orders){
+ res.render('orders', {orders: orders});
});
});
View
@@ -3,9 +3,39 @@ extends layout
block content
.container
h1 Shopping Cart
- if products.length
- ul
- each product in products
- li #{product.id} x#{product.quantity}
+
+ if cart.length
+ table#cart
+ thead
+ tr
+ th Product
+ th Quantity
+ th Unit Price
+ th Sub-Total
+ th
+ tbody
+ each productInCart in cart
+ tr
+ td: a(href='/products/#{productInCart.product._id}')= productInCart.product.name
+ td x#{productInCart.quantity}
+ td $#{(productInCart.product.price * 1).toFixed(2)}
+ td $#{(productInCart.product.price * productInCart.quantity).toFixed(2)}
+ td: form(action='/cart/#{productInCart.product._id}', method='POST')
+ input(type='hidden', name='_method', value='DELETE')
+ input.btn.btn-danger(type='submit', value='Remove')
+ tfoot
+ tr
+ td(colspan=3)
+ td $#{total.toFixed(2)}
+ td
else
p: em Your shopping cart is empty.
+
+ if cart.length
+ form(action='/orders', method='POST')
+ div.buttons
+ a.btn(href='/') Continue Shopping
+ input.btn.btn-success(type='submit', value='Checkout')
+ else
+ div.buttons
+ a.btn(href='/') Continue Shopping
View
@@ -1,6 +0,0 @@
-extends layout
-
-block content
- .container
- h1 Checkout
- p Lorem ipsum.
View
@@ -11,6 +11,7 @@ html(lang='en')
style
body{ padding-top: 60px; } /* 60px to make the container go all the way to the bottom of the topbar */
link(rel='stylesheet', href='/bootstrap/css/bootstrap-responsive.min.css')
+ link(rel='stylesheet', href='/express-lane-client.css')
body
.navbar.navbar-fixed-top
View
@@ -15,4 +15,4 @@ block content
dd= product.description
form(action='/cart/#{product._id}', method='POST')
- input(type='submit', value='Add To Cart')
+ input.btn.btn-success(type='submit', value='Add To Cart')

0 comments on commit e9f3772

Please sign in to comment.