From 40bd068c37235210a576a7a0ce3d799620247445 Mon Sep 17 00:00:00 2001 From: Subhrakanti Dasgupta Date: Mon, 27 Jun 2022 17:47:44 +0530 Subject: [PATCH] Add files via upload --- controllers/shop.js | 50 +++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/controllers/shop.js b/controllers/shop.js index 7a6e3f8..50f0616 100644 --- a/controllers/shop.js +++ b/controllers/shop.js @@ -14,29 +14,35 @@ exports.listProducts = (req, res, next) => { let total; - Product.countDocuments() - .then((count) => { - total = count; - return Product.find() - .skip((page - 1) * ITEM_COUNT) - .limit(ITEM_COUNT) - }) - .then((prods) => { - let products = []; - - if(req.user) { - products = prods.filter((prod) => prod.createdBy.toString() !== req.user._id.toString()); - } else { - products = prods; - } - - res.render('shop/list', { pageTitle: 'Shopper', path: '/', products: products, currentPage: page, hasNext: ITEM_COUNT * page < total, nextPage: page + 1, hasPrevious: page > 1, previousPage: page - 1, lastPage: Math.ceil(total / ITEM_COUNT) }); - }) - .catch((err) => { - const error = new Error(err); - error.httpStatusCode = 500; - next(error); + if(req.user) { + Product.countDocuments({ createdBy: { $ne: req.user._id } }) + .then((count) => { + total = count; + return Product.find({ createdBy: { $ne: req.user._id } }) + .skip((page - 1) * ITEM_COUNT) + .limit(ITEM_COUNT) + }) + .then((products) => res.render('shop/list', { pageTitle: 'Shopper', path: '/', products: products, currentPage: page, hasNext: ITEM_COUNT * page < total, nextPage: page + 1, hasPrevious: page > 1, previousPage: page - 1, lastPage: Math.ceil(total / ITEM_COUNT) })) + .catch((err) => { + const error = new Error(err); + error.httpStatusCode = 500; + next(error); }); + } else { + Product.countDocuments() + .then((count) => { + total = count; + return Product.find() + .skip((page - 1) * ITEM_COUNT) + .limit(ITEM_COUNT) + }) + .then((products) => res.render('shop/list', { pageTitle: 'Shopper', path: '/', products: products, currentPage: page, hasNext: ITEM_COUNT * page < total, nextPage: page + 1, hasPrevious: page > 1, previousPage: page - 1, lastPage: Math.ceil(total / ITEM_COUNT) })) + .catch((err) => { + const error = new Error(err); + error.httpStatusCode = 500; + next(error); + }); + } }; exports.findItem = (req, res, next) => {