const keystone = require('keystone');

function normalizeProduct(product) {
    const {slug, name, description, price, externalImages, image0} = product;

    return {
        slug,
        name,
        description,
        price,
        image: externalImages.length !== 0 ? externalImages[0] : '/product/images/' + image0.filename
    };
}

function onInitView(view, next) {
    const {res} = view;
    const {locals} = res;

    keystone
        .list('Product')
        .model
        .find()
        // .limit(30)
        .select('slug name description price externalImages image0')
        .exec((err, products) => {
            if (err) {
                res.status(500);
                res.render('errors/500');
                return;
            }

            Object.assign(locals, {products: products.map(normalizeProduct)});
            next(err);
        });
}

exports = module.exports = (req, res) => {
    const view = new keystone.View(req, res);

    view.on('init', next => onInitView(view, next));

    view.render('index');
};

@@ -385,16 +385,21 @@
 nst %7Bres
+, req
 %7D = view
@@ -478,30 +478,882 @@
    .
-model%0A        .find()%0A
+paginate(%7B%0A            page: req.query.page %7C%7C 1,%0A            perPage: 10,%0A            maxPages: 10%0A        %7D)%0A        // .where('state', 'published')%0A        // .sort('-publishedDate')%0A        // .populate('author categories')%0A        .select('slug name description price externalImages image0')%0A        .exec((err, result) =%3E %7B%0A            // todo: parse result to get pages count, current pages end etc.%0A            // todo: test req.query.page %7C%7C 1%0A            debugger;%0A            if (err) %7B%0A                res.status(500);%0A                res.render('errors/500');%0A                return;%0A            %7D%0A%0A            // locals.data.posts = results;%0A            Object.assign(locals, %7Bproducts: result.results.map(normalizeProduct)%7D);%0A%0A            next(err);%0A        %7D);%0A%0A    /*%0A        keystone%0A            .list('Product')%0A            .model%0A            .find()%0A    
     
@@ -1370,16 +1370,20 @@
 mit(30)%0A
+    
         
@@ -1443,32 +1443,36 @@
 mage0')%0A        
+ 
+   
 .exec((err, prod
@@ -1478,24 +1478,28 @@
 ducts) =%3E %7B%0A
+    
             
@@ -1517,32 +1517,36 @@
                 
+    
 res.status(500);
@@ -1554,32 +1554,36 @@
                 
+   
+ 
 res.render('erro
@@ -1600,32 +1600,36 @@
                 
+    
 return;%0A        
@@ -1624,35 +1624,43 @@
 rn;%0A            
-%7D%0A%0A
+    %7D%0A%0A    
             Obje
@@ -1726,32 +1726,36 @@
 %7D);%0A            
+ 
+   
 next(err);%0A     
@@ -1757,19 +1757,30 @@
         
-%7D);
+    %7D);%0A    */
 %0A%7D%0A%0Aexpo

index page pagination - begin