Skip to content

Commit

Permalink
Added cart functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
lmarkus committed Dec 5, 2013
1 parent 479a974 commit 5aa586f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
73 changes: 73 additions & 0 deletions controllers/cart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';
var Product = require('../models/productModel');

module.exports = function (server) {

/**
* Display the shopping cart
*/
server.get('/cart', function (req, res) {

//Retrieve the shopping cart from memory
var cart = req.session.cart,
displayCart = {items: [], total: 0},
total = 0;

if (!cart) {
res.render('result', {result: 'Your cart is empty!'});
return;
}

//Ready the products for display
for (var item in cart) {
displayCart.items.push(cart[item]);
total += (cart[item].qty * cart[item].price);
}
req.session.total = displayCart.total = total.toFixed(2);

var model =
{
cart: displayCart
};

res.render('cart', model);
});

/**
* Add an item to the shopping cart
*/
server.post('/cart', function (req, res) {

//Load (or initialize) the cart
req.session.cart = req.session.cart || {};
var cart = req.session.cart;

//Read the incoming product data
var id = req.param('item_id');

//Locate the product to be added
Product.findById(id, function (err, prod) {
if (err) {
console.log('Error adding product to cart: ', err);
res.redirect('/cart');
return;
}

//Add or increase the product quantity in the shopping cart.
if (cart[id]) {
cart[id].qty++;
}
else {
cart[id] = {
name: prod.name,
price: prod.price,
prettyPrice: prod.prettyPrice(),
qty: 1
};
}

res.redirect('/cart');

});
});
};
1 change: 1 addition & 0 deletions locales/US/en/cart.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
index.greeting=Hello, {name}!
Empty file added models/cart.js
Empty file.
44 changes: 44 additions & 0 deletions public/templates/cart.dust
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{>"layouts/master" /}

{<body}
<main role="main">
<div class="products mb2">
<h2>Your cart </h2>
<ul class="nm-np inline">
{#cart.items}
<li>
<h3 class="nm-np">{.qty} x {.name}</h3>
<h4 class="nm-np">Price: {.prettyPrice} ea.</h4>
</li>
{/cart.items}
</ul>
</div>

<div class="ccForm inline">
<h3>Total: ${cart.total}</h3>
<fieldset>
<form method="post" action="/pay">
<input name="cc" placeholder="CC #" value="4532649989162709" maxlength="16"><br>
<input name="expMonth" placeholder="MM" value="12" maxlength="2" size="2">
<input name="expYear" placeholder="YYYY" value="2018" maxlength="4" size="4">
<input name="cvv" placeholder="cvv" value="111" maxlength="4" size="4"><br>
<input name="firstName" value="Ash" placeholder="First Name">
<input name="lastName" value="Williams" placeholder="Last Name"><br>
<input type="hidden" name="_csrf" value="{_csrf}">
<input type="submit" value="Complete Purchase">
</form>
</fieldset>
</div>
<div>
<p>
Note:<br>
The above values have been pre-filled for your convenience<br>
You can use any valid Amex, Visa, MC or Discover credit card number
(<a target="_blank" href="http://www.darkcoding.net/credit-card-numbers/">Random Generator</a>)<br>
along with any cvv and expiration date in the future.


</p>
</div>
</main>
{/body}

0 comments on commit 5aa586f

Please sign in to comment.