const express = require('express'); const router = express.Router(); const { getUser } = require('../util.js'); router.get('/', function(req, res, next) { if (req.session.productList && req.session.productList.length != 0 && req.query.remove) { req.session.productList = req.session.productList.filter(p => p.id != req.query.remove); } if (req.session.productList && req.session.productList.length != 0 && req.query.id && req.query.inc) { req.session.productList = req.session.productList.map(p => { if (p.id == req.query.id) { const newQuantity = p.quantity + Number(req.query.inc); return newQuantity === 0 ? null : {...p, quantity: p.quantity + Number(req.query.inc)}; } return p; }).filter(p => p !== null); } let content; if (req.session.productList && req.session.productList.length != 0) { req.session.productList = req.session.productList.filter(p => p); const productList = req.session.productList; content = ` ${productList.map(product => ` `).join('\n')}
Product ID Product Name Price Quantity
${product.id} ${product.name} $${Number(product.price).toFixed(2)} -  ${product.quantity}  + Delete

Checkout

`; } else{ content = `

Your shopping cart is empty!

`; } res.render('layouts/main', { title: 'Your Shopping Cart', loggedIn: true, user: getUser(req), spacer: true, content: `

Your Shopping Cart

${content}

Continue Shopping

`, }); }); module.exports = router;