Cleaning up code
parent
ecef3b8224
commit
3c39b5e5c5
|
@ -17,6 +17,8 @@ Keeping expressCart running after closing the terminal can be done in a few ways
|
||||||
4. Save the PM2 config: `pm2 save`
|
4. Save the PM2 config: `pm2 save`
|
||||||
5. To start/stop: `pm2 start expressCart` / `pm2 stop expressCart`
|
5. To start/stop: `pm2 start expressCart` / `pm2 stop expressCart`
|
||||||
|
|
||||||
|
> Note: Node.js version 7.x or greater is needed.
|
||||||
|
|
||||||
## Admin
|
## Admin
|
||||||
|
|
||||||
Visit: [http://127.0.0.1:1111/admin](http://127.0.0.1:1111/admin)
|
Visit: [http://127.0.0.1:1111/admin](http://127.0.0.1:1111/admin)
|
||||||
|
|
2
app.js
2
app.js
|
@ -184,7 +184,7 @@ handlebars = handlebars.create({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// var session store
|
// session store
|
||||||
let store = new MongoStore({
|
let store = new MongoStore({
|
||||||
uri: config.databaseConnectionString,
|
uri: config.databaseConnectionString,
|
||||||
collection: 'sessions'
|
collection: 'sessions'
|
||||||
|
|
|
@ -2,12 +2,17 @@ const _ = require('lodash');
|
||||||
const uglifycss = require('uglifycss');
|
const uglifycss = require('uglifycss');
|
||||||
const colors = require('colors');
|
const colors = require('colors');
|
||||||
const lunr = require('lunr');
|
const lunr = require('lunr');
|
||||||
|
const cheerio = require('cheerio');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const glob = require('glob');
|
||||||
|
const async = require('async');
|
||||||
|
const nodemailer = require('nodemailer');
|
||||||
const escape = require('html-entities').AllHtmlEntities;
|
const escape = require('html-entities').AllHtmlEntities;
|
||||||
let ObjectId = require('mongodb').ObjectID;
|
let ObjectId = require('mongodb').ObjectID;
|
||||||
|
|
||||||
// common functions
|
// common functions
|
||||||
exports.checkLogin = function(req, res, next){
|
exports.checkLogin = (req, res, next) => {
|
||||||
// if not protecting we check for public pages and don't checkLogin
|
// if not protecting we check for public pages and don't checkLogin
|
||||||
if(req.session.needsSetup === true){
|
if(req.session.needsSetup === true){
|
||||||
res.redirect('/setup');
|
res.redirect('/setup');
|
||||||
|
@ -21,7 +26,7 @@ exports.checkLogin = function(req, res, next){
|
||||||
res.redirect('/login');
|
res.redirect('/login');
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.showCartCloseBtn = function(page){
|
exports.showCartCloseBtn = (page) => {
|
||||||
let showCartCloseButton = true;
|
let showCartCloseButton = true;
|
||||||
if(page === 'checkout' || page === 'pay'){
|
if(page === 'checkout' || page === 'pay'){
|
||||||
showCartCloseButton = false;
|
showCartCloseButton = false;
|
||||||
|
@ -31,9 +36,9 @@ exports.showCartCloseBtn = function(page){
|
||||||
};
|
};
|
||||||
|
|
||||||
// adds products to sitemap.xml
|
// adds products to sitemap.xml
|
||||||
exports.addSitemapProducts = function(req, res, cb){
|
exports.addSitemapProducts = (req, res, cb) => {
|
||||||
let db = req.app.db;
|
let db = req.app.db;
|
||||||
let async = require('async');
|
|
||||||
let config = exports.getConfig();
|
let config = exports.getConfig();
|
||||||
let hostname = config.baseUrl;
|
let hostname = config.baseUrl;
|
||||||
|
|
||||||
|
@ -59,11 +64,11 @@ exports.addSitemapProducts = function(req, res, cb){
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.restrict = function(req, res, next){
|
exports.restrict = (req, res, next) => {
|
||||||
exports.checkLogin(req, res, next);
|
exports.checkLogin(req, res, next);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.clearSessionValue = function(session, sessionVar){
|
exports.clearSessionValue = (session, sessionVar) => {
|
||||||
let temp;
|
let temp;
|
||||||
if(session){
|
if(session){
|
||||||
temp = session[sessionVar];
|
temp = session[sessionVar];
|
||||||
|
@ -72,7 +77,7 @@ exports.clearSessionValue = function(session, sessionVar){
|
||||||
return temp;
|
return temp;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.updateTotalCartAmount = function(req, res){
|
exports.updateTotalCartAmount = (req, res) => {
|
||||||
let config = exports.getConfig();
|
let config = exports.getConfig();
|
||||||
|
|
||||||
req.session.totalCartAmount = 0;
|
req.session.totalCartAmount = 0;
|
||||||
|
@ -90,8 +95,7 @@ exports.updateTotalCartAmount = function(req, res){
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.checkDirectorySync = function (directory){
|
exports.checkDirectorySync = (directory) => {
|
||||||
let fs = require('fs');
|
|
||||||
try{
|
try{
|
||||||
fs.statSync(directory);
|
fs.statSync(directory);
|
||||||
}catch(e){
|
}catch(e){
|
||||||
|
@ -99,16 +103,12 @@ exports.checkDirectorySync = function (directory){
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getThemes = function (){
|
exports.getThemes = () => {
|
||||||
let fs = require('fs');
|
|
||||||
let path = require('path');
|
|
||||||
return fs.readdirSync(path.join('public', 'themes')).filter(file => fs.statSync(path.join(path.join('public', 'themes'), file)).isDirectory());
|
return fs.readdirSync(path.join('public', 'themes')).filter(file => fs.statSync(path.join(path.join('public', 'themes'), file)).isDirectory());
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getImages = function (dir, req, res, callback){
|
exports.getImages = (dir, req, res, callback) => {
|
||||||
let db = req.app.db;
|
let db = req.app.db;
|
||||||
let glob = require('glob');
|
|
||||||
let fs = require('fs');
|
|
||||||
|
|
||||||
db.products.findOne({_id: exports.getId(dir)}, (err, product) => {
|
db.products.findOne({_id: exports.getId(dir)}, (err, product) => {
|
||||||
if(err){
|
if(err){
|
||||||
|
@ -144,10 +144,7 @@ exports.getImages = function (dir, req, res, callback){
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getConfig = function(){
|
exports.getConfig = () => {
|
||||||
let fs = require('fs');
|
|
||||||
let path = require('path');
|
|
||||||
|
|
||||||
let config = JSON.parse(fs.readFileSync(path.join(__dirname, '../config', 'settings.json'), 'utf8'));
|
let config = JSON.parse(fs.readFileSync(path.join(__dirname, '../config', 'settings.json'), 'utf8'));
|
||||||
config.customCss = typeof config.customCss !== 'undefined' ? escape.decode(config.customCss) : null;
|
config.customCss = typeof config.customCss !== 'undefined' ? escape.decode(config.customCss) : null;
|
||||||
config.footerHtml = typeof config.footerHtml !== 'undefined' ? escape.decode(config.footerHtml) : null;
|
config.footerHtml = typeof config.footerHtml !== 'undefined' ? escape.decode(config.footerHtml) : null;
|
||||||
|
@ -171,9 +168,7 @@ exports.getConfig = function(){
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getPaymentConfig = function(){
|
exports.getPaymentConfig = () => {
|
||||||
let fs = require('fs');
|
|
||||||
let path = require('path');
|
|
||||||
let siteConfig = this.getConfig();
|
let siteConfig = this.getConfig();
|
||||||
|
|
||||||
let config = [];
|
let config = [];
|
||||||
|
@ -184,9 +179,7 @@ exports.getPaymentConfig = function(){
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.updateConfig = function(fields){
|
exports.updateConfig = (fields) => {
|
||||||
let fs = require('fs');
|
|
||||||
let path = require('path');
|
|
||||||
let settingsFile = JSON.parse(fs.readFileSync(path.join(__dirname, '../config/settings.json'), 'utf8'));
|
let settingsFile = JSON.parse(fs.readFileSync(path.join(__dirname, '../config/settings.json'), 'utf8'));
|
||||||
|
|
||||||
_.forEach(fields, (value, key) => {
|
_.forEach(fields, (value, key) => {
|
||||||
|
@ -230,12 +223,12 @@ exports.updateConfig = function(fields){
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getMenu = function(db){
|
exports.getMenu = (db) => {
|
||||||
return db.menu.findOne({});
|
return db.menu.findOne({});
|
||||||
};
|
};
|
||||||
|
|
||||||
// creates a new menu item
|
// creates a new menu item
|
||||||
exports.newMenu = function(req, res){
|
exports.newMenu = (req, res) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
return exports.getMenu(db)
|
return exports.getMenu(db)
|
||||||
.then((menu) => {
|
.then((menu) => {
|
||||||
|
@ -263,7 +256,7 @@ exports.newMenu = function(req, res){
|
||||||
};
|
};
|
||||||
|
|
||||||
// delete a menu item
|
// delete a menu item
|
||||||
exports.deleteMenu = function(req, res, menuIndex){
|
exports.deleteMenu = (req, res, menuIndex) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
return exports.getMenu(db)
|
return exports.getMenu(db)
|
||||||
.then((menu) => {
|
.then((menu) => {
|
||||||
|
@ -280,7 +273,7 @@ exports.deleteMenu = function(req, res, menuIndex){
|
||||||
};
|
};
|
||||||
|
|
||||||
// updates and existing menu item
|
// updates and existing menu item
|
||||||
exports.updateMenu = function(req, res){
|
exports.updateMenu = (req, res) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
return exports.getMenu(db)
|
return exports.getMenu(db)
|
||||||
.then((menu) => {
|
.then((menu) => {
|
||||||
|
@ -298,7 +291,7 @@ exports.updateMenu = function(req, res){
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.sortMenu = function(menu){
|
exports.sortMenu = (menu) => {
|
||||||
if(menu && menu.items){
|
if(menu && menu.items){
|
||||||
menu.items = _.sortBy(menu.items, 'order');
|
menu.items = _.sortBy(menu.items, 'order');
|
||||||
return menu;
|
return menu;
|
||||||
|
@ -307,7 +300,7 @@ exports.sortMenu = function(menu){
|
||||||
};
|
};
|
||||||
|
|
||||||
// orders the menu
|
// orders the menu
|
||||||
exports.orderMenu = function(req, res){
|
exports.orderMenu = (req, res) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
return exports.getMenu(db)
|
return exports.getMenu(db)
|
||||||
.then((menu) => {
|
.then((menu) => {
|
||||||
|
@ -325,11 +318,8 @@ exports.orderMenu = function(req, res){
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getEmailTemplate = function(result){
|
exports.getEmailTemplate = (result) => {
|
||||||
let cheerio = require('cheerio');
|
|
||||||
let config = this.getConfig();
|
let config = this.getConfig();
|
||||||
let fs = require('fs');
|
|
||||||
let path = require('path');
|
|
||||||
|
|
||||||
let template = fs.readFileSync(path.join(__dirname, '../public/email_template.html'), 'utf8');
|
let template = fs.readFileSync(path.join(__dirname, '../public/email_template.html'), 'utf8');
|
||||||
|
|
||||||
|
@ -347,9 +337,8 @@ exports.getEmailTemplate = function(result){
|
||||||
return $.html();
|
return $.html();
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.sendEmail = function(to, subject, body){
|
exports.sendEmail = (to, subject, body) => {
|
||||||
let config = this.getConfig();
|
let config = this.getConfig();
|
||||||
let nodemailer = require('nodemailer');
|
|
||||||
|
|
||||||
let emailSettings = {
|
let emailSettings = {
|
||||||
host: config.emailHost,
|
host: config.emailHost,
|
||||||
|
@ -384,7 +373,7 @@ exports.sendEmail = function(to, subject, body){
|
||||||
};
|
};
|
||||||
|
|
||||||
// gets the correct type of index ID
|
// gets the correct type of index ID
|
||||||
exports.getId = function(id){
|
exports.getId = (id) => {
|
||||||
if(id){
|
if(id){
|
||||||
if(id.length !== 24){
|
if(id.length !== 24){
|
||||||
return id;
|
return id;
|
||||||
|
@ -394,7 +383,7 @@ exports.getId = function(id){
|
||||||
};
|
};
|
||||||
|
|
||||||
// run the DB query
|
// run the DB query
|
||||||
exports.dbQuery = function(db, query, sort, limit, callback){
|
exports.dbQuery = (db, query, sort, limit, callback) => {
|
||||||
if(sort && limit){
|
if(sort && limit){
|
||||||
db.find(query).sort(sort).limit(parseInt(limit)).toArray((err, results) => {
|
db.find(query).sort(sort).limit(parseInt(limit)).toArray((err, results) => {
|
||||||
if(err){
|
if(err){
|
||||||
|
@ -412,6 +401,32 @@ exports.dbQuery = function(db, query, sort, limit, callback){
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.getData = async (req, page, query) => {
|
||||||
|
let db = req.app.db;
|
||||||
|
let config = exports.getConfig();
|
||||||
|
let numberProducts = config.productsPerPage ? config.productsPerPage : 6;
|
||||||
|
|
||||||
|
let skip = 0;
|
||||||
|
if(page > 1){
|
||||||
|
skip = (page - 1) * numberProducts;
|
||||||
|
}
|
||||||
|
|
||||||
|
query['productPublished'] = 'true';
|
||||||
|
|
||||||
|
// Run our queries
|
||||||
|
return Promise.all([
|
||||||
|
db.products.find(query).skip(skip).limit(parseInt(numberProducts)).toArray(),
|
||||||
|
db.products.count(query)
|
||||||
|
])
|
||||||
|
.then((result) => {
|
||||||
|
const returnData = {data: result[0], totalProducts: result[1]};
|
||||||
|
return returnData;
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
throw new Error('Error retrieving products');
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
exports.indexProducts = (app) => {
|
exports.indexProducts = (app) => {
|
||||||
// index all products in lunr on startup
|
// index all products in lunr on startup
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
112
routes/index.js
112
routes/index.js
|
@ -13,7 +13,7 @@ router.get('/payment/:orderId', async (req, res, next) => {
|
||||||
if(err){
|
if(err){
|
||||||
console.info(err.stack);
|
console.info(err.stack);
|
||||||
}
|
}
|
||||||
res.render(config.themeViews + 'payment_complete', {
|
res.render(`${config.themeViews}payment_complete`, {
|
||||||
title: 'Payment complete',
|
title: 'Payment complete',
|
||||||
config: common.getConfig(),
|
config: common.getConfig(),
|
||||||
session: req.session,
|
session: req.session,
|
||||||
|
@ -40,7 +40,7 @@ router.get('/checkout', async (req, res, next) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// render the checkout
|
// render the checkout
|
||||||
res.render(config.themeViews + 'checkout', {
|
res.render(`${config.themeViews}checkout`, {
|
||||||
title: 'Checkout',
|
title: 'Checkout',
|
||||||
config: common.getConfig(),
|
config: common.getConfig(),
|
||||||
session: req.session,
|
session: req.session,
|
||||||
|
@ -66,7 +66,7 @@ router.get('/pay', async (req, res, next) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// render the payment page
|
// render the payment page
|
||||||
res.render(config.themeViews + 'pay', {
|
res.render(`${config.themeViews}pay`, {
|
||||||
title: 'Pay',
|
title: 'Pay',
|
||||||
config: common.getConfig(),
|
config: common.getConfig(),
|
||||||
paymentConfig: common.getPaymentConfig(),
|
paymentConfig: common.getPaymentConfig(),
|
||||||
|
@ -112,7 +112,7 @@ router.get('/product/:id', (req, res) => {
|
||||||
|
|
||||||
// show the view
|
// show the view
|
||||||
common.getImages(result._id, req, res, async (images) => {
|
common.getImages(result._id, req, res, async (images) => {
|
||||||
res.render(config.themeViews + 'product', {
|
res.render(`${config.themeViews}product`, {
|
||||||
title: result.productTitle,
|
title: result.productTitle,
|
||||||
result: result,
|
result: result,
|
||||||
productOptions: productOptions,
|
productOptions: productOptions,
|
||||||
|
@ -254,13 +254,12 @@ router.get('/search/:searchTerm/:pageNum?', (req, res) => {
|
||||||
pageNum = req.params.pageNum;
|
pageNum = req.params.pageNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
// we search on the lunr indexes
|
Promise.all([
|
||||||
getData(req, pageNum, {_id: {$in: lunrIdArray}}, async (err, results) => {
|
common.getData(req, pageNum, {_id: {$in: lunrIdArray}}),
|
||||||
if(err){
|
common.getMenu(db)
|
||||||
console.error(colors.red('Error searching for products', err));
|
])
|
||||||
}
|
.then(([results, menu]) => {
|
||||||
|
res.render(`${config.themeViews}index`, {
|
||||||
res.render(config.themeViews + 'index', {
|
|
||||||
title: 'Results',
|
title: 'Results',
|
||||||
results: results.data,
|
results: results.data,
|
||||||
filtered: true,
|
filtered: true,
|
||||||
|
@ -275,10 +274,13 @@ router.get('/search/:searchTerm/:pageNum?', (req, res) => {
|
||||||
pageNum: pageNum,
|
pageNum: pageNum,
|
||||||
paginateUrl: 'search',
|
paginateUrl: 'search',
|
||||||
config: config,
|
config: config,
|
||||||
menu: common.sortMenu(await common.getMenu(db)),
|
menu: common.sortMenu(menu),
|
||||||
helpers: req.handlebars.helpers,
|
helpers: req.handlebars.helpers,
|
||||||
showFooter: 'showFooter'
|
showFooter: 'showFooter'
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(colors.red('Error searching for products', err));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -300,13 +302,14 @@ router.get('/category/:cat/:pageNum?', (req, res) => {
|
||||||
pageNum = req.params.pageNum;
|
pageNum = req.params.pageNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
// we search on the lunr indexes
|
Promise.all([
|
||||||
getData(req, pageNum, {_id: {$in: lunrIdArray}}, async (err, results) => {
|
common.getData(req, pageNum, {_id: {$in: lunrIdArray}}),
|
||||||
if(err){
|
common.getMenu(db)
|
||||||
console.error(colors.red('Error getting products for category', err));
|
])
|
||||||
}
|
.then(([results, menu]) => {
|
||||||
|
const sortedMenu = common.sortMenu(menu);
|
||||||
|
|
||||||
res.render(config.themeViews + 'index', {
|
res.render(`${config.themeViews}index`, {
|
||||||
title: 'Category',
|
title: 'Category',
|
||||||
results: results.data,
|
results: results.data,
|
||||||
filtered: true,
|
filtered: true,
|
||||||
|
@ -319,13 +322,16 @@ router.get('/category/:cat/:pageNum?', (req, res) => {
|
||||||
productsPerPage: numberProducts,
|
productsPerPage: numberProducts,
|
||||||
totalProductCount: results.totalProducts,
|
totalProductCount: results.totalProducts,
|
||||||
pageNum: pageNum,
|
pageNum: pageNum,
|
||||||
menuLink: _.find(common.sortMenu(await common.getMenu(db)).items, (obj) => { return obj.link === searchTerm; }),
|
menuLink: _.find(sortedMenu.items, (obj) => { return obj.link === searchTerm; }),
|
||||||
paginateUrl: 'category',
|
paginateUrl: 'category',
|
||||||
config: config,
|
config: config,
|
||||||
menu: common.sortMenu(await common.getMenu(db)),
|
menu: sortedMenu,
|
||||||
helpers: req.handlebars.helpers,
|
helpers: req.handlebars.helpers,
|
||||||
showFooter: 'showFooter'
|
showFooter: 'showFooter'
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(colors.red('Error getting products for category', err));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -367,12 +373,12 @@ router.get('/page/:pageNum', (req, res, next) => {
|
||||||
let config = common.getConfig();
|
let config = common.getConfig();
|
||||||
let numberProducts = config.productsPerPage ? config.productsPerPage : 6;
|
let numberProducts = config.productsPerPage ? config.productsPerPage : 6;
|
||||||
|
|
||||||
getData(req, req.params.pageNum, {}, async (err, results) => {
|
Promise.all([
|
||||||
if(err){
|
common.getData(req, req.params.pageNum),
|
||||||
console.error(colors.red('Error getting products for page', err));
|
common.getMenu(db)
|
||||||
}
|
])
|
||||||
|
.then(([results, menu]) => {
|
||||||
res.render(config.themeViews + 'index', {
|
res.render(`${config.themeViews}index`, {
|
||||||
title: 'Shop',
|
title: 'Shop',
|
||||||
results: results.data,
|
results: results.data,
|
||||||
session: req.session,
|
session: req.session,
|
||||||
|
@ -387,11 +393,15 @@ router.get('/page/:pageNum', (req, res, next) => {
|
||||||
paginateUrl: 'page',
|
paginateUrl: 'page',
|
||||||
helpers: req.handlebars.helpers,
|
helpers: req.handlebars.helpers,
|
||||||
showFooter: 'showFooter',
|
showFooter: 'showFooter',
|
||||||
menu: common.sortMenu(await common.getMenu(db))
|
menu: common.sortMenu(menu)
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(colors.red('Error getting products for page', err));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// The main entry point of the shop
|
||||||
router.get('/:page?', (req, res, next) => {
|
router.get('/:page?', (req, res, next) => {
|
||||||
let db = req.app.db;
|
let db = req.app.db;
|
||||||
let config = common.getConfig();
|
let config = common.getConfig();
|
||||||
|
@ -399,13 +409,13 @@ router.get('/:page?', (req, res, next) => {
|
||||||
|
|
||||||
// if no page is specified, just render page 1 of the cart
|
// if no page is specified, just render page 1 of the cart
|
||||||
if(!req.params.page){
|
if(!req.params.page){
|
||||||
getData(req, 1, {}, async (err, results) => {
|
Promise.all([
|
||||||
if(err){
|
common.getData(req, 1, {}),
|
||||||
console.error(colors.red('Error getting products for page', err));
|
common.getMenu(db)
|
||||||
}
|
])
|
||||||
|
.then(([results, menu]) => {
|
||||||
res.render(config.themeViews + 'index', {
|
res.render(`${config.themeViews}index`, {
|
||||||
title: 'Shop',
|
title: `${config.cartTitle} - Shop`,
|
||||||
theme: config.theme,
|
theme: config.theme,
|
||||||
results: results.data,
|
results: results.data,
|
||||||
session: req.session,
|
session: req.session,
|
||||||
|
@ -419,8 +429,11 @@ router.get('/:page?', (req, res, next) => {
|
||||||
paginateUrl: 'page',
|
paginateUrl: 'page',
|
||||||
helpers: req.handlebars.helpers,
|
helpers: req.handlebars.helpers,
|
||||||
showFooter: 'showFooter',
|
showFooter: 'showFooter',
|
||||||
menu: common.sortMenu(await common.getMenu(db))
|
menu: common.sortMenu(menu)
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(colors.red('Error getting products for page', err));
|
||||||
});
|
});
|
||||||
}else{
|
}else{
|
||||||
if(req.params.page === 'admin'){
|
if(req.params.page === 'admin'){
|
||||||
|
@ -434,7 +447,7 @@ router.get('/:page?', (req, res, next) => {
|
||||||
}
|
}
|
||||||
// if we have a page lets render it, else throw 404
|
// if we have a page lets render it, else throw 404
|
||||||
if(page){
|
if(page){
|
||||||
res.render(config.themeViews + 'page', {
|
res.render(`${config.themeViews}page`, {
|
||||||
title: page.pageName,
|
title: page.pageName,
|
||||||
page: page,
|
page: page,
|
||||||
session: req.session,
|
session: req.session,
|
||||||
|
@ -461,31 +474,4 @@ router.get('/:page?', (req, res, next) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const getData = function (req, page, query, cb){
|
|
||||||
let db = req.app.db;
|
|
||||||
let config = common.getConfig();
|
|
||||||
let numberProducts = config.productsPerPage ? config.productsPerPage : 6;
|
|
||||||
|
|
||||||
let skip = 0;
|
|
||||||
if(page > 1){
|
|
||||||
skip = (page - 1) * numberProducts;
|
|
||||||
}
|
|
||||||
|
|
||||||
query['productPublished'] = 'true';
|
|
||||||
|
|
||||||
db.products.count(query, (err, totalProducts) => {
|
|
||||||
if(err){
|
|
||||||
console.error(colors.red('Error getting total product count', err));
|
|
||||||
}
|
|
||||||
|
|
||||||
db.products.find(query).skip(skip).limit(parseInt(numberProducts)).toArray((err, results) => {
|
|
||||||
if(err){
|
|
||||||
cb(new Error('Error retrieving products'), null);
|
|
||||||
}else{
|
|
||||||
cb(null, {data: results, totalProducts: totalProducts});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
Loading…
Reference in New Issue