Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/products.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{
"id": 3,
"title": "Garlic",
"description": "Garlic are <span class=\"label label-info\">15 CHF each</span>",
"description": "Garlic is <span class=\"label label-info\">15 CHF each</span>",
"price": 15
},
{
Expand Down
16 changes: 7 additions & 9 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,43 @@ var fs = require('fs');
var Cart = require('../models/cart');
var products = JSON.parse(fs.readFileSync('./data/products.json', 'utf8'));

router.get('/', function (req, res, next) {
var productId = products && products[0].id;

router.get('/', function (_req, res, _next) {
res.render('index',
{
title: 'NodeJS Shopping Cart',
title: 'Shopping Cart',
products: products
}
);
});

router.get('/add/:id', function(req, res, next) {
router.get('/add/:id', function(req, res, _next) {

var productId = req.params.id;
var cart = new Cart(req.session.cart ? req.session.cart : {});
var product = products.filter(function(item) {
return item.id == productId;
return item.id === productId;
});
cart.add(product[0], productId);
req.session.cart = cart;
res.redirect('/');
inline();
});

router.get('/cart', function(req, res, next) {
router.get('/cart', function(req, res, _next) {
if (!req.session.cart) {
return res.render('cart', {
products: null
});
}
var cart = new Cart(req.session.cart);
res.render('cart', {
title: 'NodeJS Shopping Cart',
title: 'Shopping Cart',
products: cart.getItems(),
totalPrice: cart.totalPrice
});
});

router.get('/remove/:id', function(req, res, next) {
router.get('/remove/:id', function(req, res, _next) {
var productId = req.params.id;
var cart = new Cart(req.session.cart ? req.session.cart : {});

Expand Down