Skip to content

Instantly share code, notes, and snippets.

@erockdotdev
Created May 2, 2017 02:41
Show Gist options
  • Save erockdotdev/c2cbd2c5d76e85157efa232cde0781e1 to your computer and use it in GitHub Desktop.
Save erockdotdev/c2cbd2c5d76e85157efa232cde0781e1 to your computer and use it in GitHub Desktop.
Full Day Build

queries.js

var promise = require('bluebird'); var options = { promiseLib: promise };

var pgp = require('pg-promise')(options)

var connectionString = 'postgres://localhost:5432/contacts_db'; var db = pgp(connectionString);

function createContact(req, res, next) { console.log(req.body) req.body.age = parseInt(req.body.age) db.none('insert into contacts(first, last, age, sex)' + 'values(${first}, ${last}, ${age}, ${sex})', req.body) .then(res.redirect('/')) }

function getAllContacts(req, res, next){ db.any('select * from contacts') .then(function(data){ console.log(data) res.render( 'index', { title:"All Contacts", data:data }) }) }

function removeContact(req, res, next) { let contactID = parseInt(req.params.id) db.result('delete from contacts where id = $1', contactID) }

function updateContact(req,res, next) { let contactID = parseInt(req.params.id)

db.none('UPDATE contacts WHERE id = $1 SET first=$1', [req.body.name, parseInt(req.params.id)] ).then(res.redirect('/'))

}

module.exports = { createContact: createContact, getAllContacts: getAllContacts, removeContact: removeContact, updateContact: updateContact }

index.js

var express = require('express'); var router = express.Router(); var db = require('../queries')

/* GET home page. */ router.get('/', db.getAllContacts); router.post('/', db.createContact); router.delete('/:id', db.removeContact) router.put('/:id', db.updateContact)

module.exports = router;

form.js

console.log('form.js is working')

$('.delete').on('click', function() { let id = $(this).parent().attr('data-id') axios.delete("http://localhost:3000/"+id) $(this).parent().remove(); });

$('.edit').on('click', function() { // let id = $(this).parent().attr('data-id')

$(this).siblings('input').attr("readonly", false);

// axios.delete("http://localhost:3000/"+id) // $(this).parent().remove(); });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment