Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
dasgupta002 authored Jun 27, 2022
1 parent f843afa commit 40bd068
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions controllers/shop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 40bd068

Please sign in to comment.