Ravi Srinivasan
2019-10-11 0284ea37d466e5a4d907ee83b81442b4cb7ba125
Added a POST route for contacts app to seed data
2 files modified
56 ■■■■ changed files
contacts/routes/index.js 46 ●●●● patch | view | raw | blame | history
contacts/views/index.pug 10 ●●●● patch | view | raw | blame | history
contacts/routes/index.js
@@ -2,19 +2,51 @@
const router = express.Router();
const { pgconn } = require('../db/config')
/* GET home page. */
/* Show home page. */
router.get('/', function(req, res) {
  pgconn.query('SELECT * FROM contacts', function(err,results) {
  // we first check if the 'contacts' table exists
  pgconn.query("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'contacts')", function(err,results) {
    if (err) {
      console.log(err);
      res.render('index', { error: 'Database connection failure! '+err.stack, contacts: null, title: 'Contact List' });
    }
    else {
      let contacts = results.rows;
      console.log(contacts);
      res.render('index', { error: null, contacts: contacts, title: 'Contact List' });
    // 'contacts' table does not exist. Show an empty table.
    else if(results.rows[0].exists == false) {
      res.render('index', { error: null, contacts: null, title: 'Contact List' });
    }
  })
    // 'contacts' table exists. Show the records.
    else {
      pgconn.query('SELECT * FROM contacts', function(err,results) {
        if (err) {
          console.log(err);
          res.render('index', { error: 'Database connection failure! '+err.stack, contacts: null, title: 'Contact List' });
        }
        else {
          let contacts = results.rows;
          console.log(contacts);
          res.render('index', { error: null, contacts: contacts, title: 'Contact List' });
        }
      })
    }
  });
});
/* Seed test data */
router.post('/seed', function(req,res) {
  // drop 'contacts' table if already exists, and seed some test data
  pgconn.query("drop table if exists contacts; create table contacts(id serial primary key,firstname varchar(30) not null,lastname varchar(30) not null, email varchar(30) not null); insert into contacts(firstname, lastname, email) values ('Bilbo','Baggins','bilbo@theshire.com'),('Frodo','Baggins','frodo@theshire.com'),('Samwise','Gamgee','sam@theshire.com'),('Peregrin','Took','pippin@theshire.com'),('Meriadoc','Brandybuck','merry@theshire.com')",function(err,results) {
    if (err) {
      console.log(err);
      res.render('index', { error: 'Seeding database failure! '+err.stack, contacts: null, title: 'Contact List' });
    }
    // redirect to the index page
    else {
      res.redirect('/');
    }
  });
});
module.exports = router;
contacts/views/index.pug
@@ -25,6 +25,10 @@
                  td= contact.lastname
                  td= contact.email
          else
              tr
                td
                  | No contacts found
            form(action="/seed", method="POST")
                button(type="submit").btn.btn-primary Seed Data
            br
            br
            tr
              td
                | No contacts found