|
| 1 | +const Product = require('../models/product'); |
| 2 | +const Cart = require('../models/cart'); |
| 3 | + |
| 4 | +exports.getProducts = (req, res, next) => { |
| 5 | + Product.fetchAll(products => { |
| 6 | + res.render('shop/product-list', { |
| 7 | + prods: products, |
| 8 | + pageTitle: 'All Products', |
| 9 | + path: '/products' |
| 10 | + }); |
| 11 | + }); |
| 12 | +}; |
| 13 | + |
| 14 | +exports.getProduct = (req, res, next) => { |
| 15 | + const prodId = req.params.productId; |
| 16 | + Product.findById(prodId, product => { |
| 17 | + res.render('shop/product-detail', { |
| 18 | + product: product, |
| 19 | + pageTitle: product.title, |
| 20 | + path: '/products' |
| 21 | + }); |
| 22 | + }); |
| 23 | +}; |
| 24 | + |
| 25 | +exports.getIndex = (req, res, next) => { |
| 26 | + Product.fetchAll(products => { |
| 27 | + res.render('shop/index', { |
| 28 | + prods: products, |
| 29 | + pageTitle: 'Shop', |
| 30 | + path: '/' |
| 31 | + }); |
| 32 | + }); |
| 33 | +}; |
| 34 | + |
| 35 | +exports.getCart = (req, res, next) => { |
| 36 | + Cart.getCart(cart => { |
| 37 | + Product.fetchAll(products => { |
| 38 | + const cartProducts = []; |
| 39 | + for (product of products) { |
| 40 | + const cartProductData = cart.products.find(prod => prod.id === product.id); |
| 41 | + if (cartProductData) { |
| 42 | + cartProducts.push({ productData: product, qty: cartProductData.qty }); |
| 43 | + } |
| 44 | + } |
| 45 | + res.render('shop/cart', { |
| 46 | + path: '/cart', |
| 47 | + pageTitle: 'Your Cart', |
| 48 | + products: cartProducts |
| 49 | + }); |
| 50 | + }); |
| 51 | + }); |
| 52 | +}; |
| 53 | + |
| 54 | +exports.postCart = (req, res, next) => { |
| 55 | + const prodId = req.body.productId; |
| 56 | + Product.findById(prodId, product => { |
| 57 | + Cart.addProduct(prodId, product.price); |
| 58 | + }); |
| 59 | + res.redirect('/cart'); |
| 60 | +}; |
| 61 | + |
| 62 | +exports.postCartDeleteProduct = (req, res, next) => { |
| 63 | + const prodId = req.body.productId; |
| 64 | + Product.findById(prodId, product => { |
| 65 | + Cart.deleteProduct(prodId, product.price); |
| 66 | + res.redirect('/cart'); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +exports.getOrders = (req, res, next) => { |
| 71 | + res.render('shop/orders', { |
| 72 | + path: '/orders', |
| 73 | + pageTitle: 'Your Orders' |
| 74 | + }); |
| 75 | +}; |
| 76 | + |
| 77 | +exports.getCheckout = (req, res, next) => { |
| 78 | + res.render('shop/checkout', { |
| 79 | + path: '/checkout', |
| 80 | + pageTitle: 'Checkout' |
| 81 | + }); |
| 82 | +}; |
0 commit comments