'use strict';

//Database functions
const pg = require('pg');

let pool = new pg.Pool({
    host: 'localhost',
    database: 'booktown'
});

function runQuery(query, argsArray, callback) {
    pool.connect((err, client, done) => {
        if (err) {
            //likely a connection error that will print to console.
            done();
            throw err;
        }
        client.query(query, argsArray, (err, results) => {
           done();  //call done to release the client to the connection pool.
           callback(err, results); //make it the callers responsiblity for checking for query errors.
        });
    });
}

const yargs = require('yargs');

const args = yargs
    .alias('f', 'firstName')
    .describe('f', 'first name')
    .alias('l', 'lastName')
    .describe('l', 'last name')
    .alias('p', 'phone')
    .describe('p', 'phone number')
    .alias('e', 'email')
    .describe('e', 'email')
    .alias('a', 'action')
    .demand('a')
    .describe('a', 'action to take [update, create]')
    .argv;

if (args.action === 'create') {
   create(args);
} else if (args.action == 'update') {
   update(args);
} else {
    throw new Error('Action not supported');
}

function create(options) {
    let query = 'insert into customers(first_name, last_name, email, phone) values ($1, $2, $3, $4)';
    let args = [options.firstName, options.lastName, options.email, options.phone];
    runQuery(query, args, (err, results) => {
        if (err) {
            console.error(err);
        } else {
            console.log(results);
        }
        process.exit();
    })
}
@@ -8,16 +8,50 @@
 rict';%0A%0A
+const _ = require('underscore');%0A%0A
 //Databa
@@ -1044,16 +1044,86 @@
 eate%5D')%0A
+    .alias('i', 'id')%0A    .describe('i', 'customer id for an update')%0A
     .arg
@@ -1686,12 +1686,12 @@
 ();%0A
-
     %7D)%0A%7D
@@ -1690,8 +1690,1408 @@
     %7D)%0A%7D
+%0A%0Afunction update(options) %7B%0A    if (!options.id) %7B%0A        throw new Error('id is required for an update');%0A    %7D%0A    let fieldsToUpdate = %5B%5D;%0A    let valuesToUpdate = %5B%5D;%0A    if (options.firstName) %7B%0A        fieldsToUpdate.push('first_name');%0A        valuesToUpdate.push(options.firstName);%0A    %7D%0A    if (options.lastName) %7B%0A        fieldsToUpdate.push('last_name');%0A        valuesToUpdate.push(options.lastName);%0A    %7D%0A    if (options.email) %7B%0A        fieldsToUpdate.push('email');%0A        valuesToUpdate.push(options.email);%0A    %7D%0A    if (options.phone) %7B%0A        fieldsToUpdate.push('phone');%0A        valuesToUpdate.push(options.phone);%0A    %7D%0A    %0A    if (fieldsToUpdate.length === 0 %7C%7C valuesToUpdate.length === 0) %7B%0A        throw new Error('no fields to update');%0A    %7D%0A    let placeHolderParams = (_.map(valuesToUpdate, (v, i) =%3E %7B return %60$$%7Bi + 1%7D%60; %7D)).join(', ');%0A    let idWhereClause = %60where id = $$%7BvaluesToUpdate.length + 1%7D%60;%0A    valuesToUpdate.push(options.id);%0A    let query = %60update customers set ($%7BfieldsToUpdate.join(', ')%7D) = ($%7BplaceHolderParams%7D) $%7BidWhereClause%7D%60;%0A    runQuery(query, valuesToUpdate, (err, result) =%3E %7B%0A        if (err) %7B%0A            console.error(err);%0A        %7D else %7B%0A            console.log(result);%0A            if (result.rowCount === 0) %7B%0A                console.error('No record updated');%0A            %7D%0A        %7D%0A        process.exit();%0A    %7D);%0A%7D

update customer