var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  let query = "SELECT product_id, productname, prodimage, description, saleprice, stock FROM product WHERE homepage = 1";
  db.all(query, (err, result) => {
    if (err) {
      console.log(err);
      res.render('error', {error: err});
    } else {
      let query2 = "SELECT promotion_id, promotitle, promoimage FROM promotion WHERE startdate <= date('now') AND enddate >= date('now')";
      db.all(query2, (err2, result2) => {
        if (err2) {
          console.log(err2);
          res.render('error', {error: err2});
        } else {
          res.render('index', {allrecs: result, promos: result2});
        }
      });
    }
  });
});
  
module.exports = router;
