ASE LAB 6 Team 4 Spring 2019 - herbertkip/Advaced-Software GitHub Wiki

TEAM 4 Class ID 3 : Chepsoy, Herbert Kiplagat Class ID 20: Oguntimehin, Josh Joshua

Should include both wiki link and a 2-minute video link about the work done

•Understand the source code –app.js, note.js •Apply what you have learned by creating a new CRUD API for Customers data with some fields like customer_id, customer_name, customer_email, etc. Create methods for adding a new customer, listing all the customers, updating a customer and deleting a customer. •No UI is needed, create functions only using the given source code & express framework

Customer Data CRUD API Data Captured

    -Customer ID
    -Customer Details
    -Customer Name
    -Customer Email
    -Customer Category

app_cutomerdata.js

const fs = require('fs'); const _ = require('lodash'); const yargs = require('yargs');

const custs = require('./custdata.js');

// ------------ Begin - command configuration -----------------

const custIdOptions = { describe: 'Customer ID', demand : true, alias : 'i' }

const custDetailsOptions = { describe: 'Customer Details', demand : true, alias : 'd' } const custNameOptions = { describe: 'Customer Name', demand : true, alias : 'n' } const custEmailOptions = { describe: 'Customer Email', demand : true, alias : 'e' } const custCategoryOptions = { describe: 'Customer Category', demand : true, alias : 'g' } const argv = yargs

.command('add','Add a new customer',{
    custId: custIdOptions,
    custDetails: custDetailsOptions,
    custName: custNameOptions,
    custEmail: custEmailOptions,
    custCategory: custCategoryOptions

})
.command('list','List all customers')
.command('read','Read a note',{
    custid: custIdOptions
})
.command('display','display a customer',{

    custid: custIdOptions
})
.command('remove','Remove a Customer',{

    custid: custIdOptions
})
.help()
.argv;

// ------------ End - command configuration -----------------

var command = argv._[0];

if (command === 'add'){ var cust = custs.addCust(argv.custId,argv.custDetails,argv.custName,argv.custEmail,argv.custCategory); if (cust){ custs.logCust(cust); //add a new customer } else{ console.log("Customer already exists"); } } else if (command === 'list') { var AllCusts = custs.getAll(); console.log(Printing ${AllCusts.length} customers(s).); AllCusts.forEach((cust)=>{ // all customers(s) custs.logCust(cust); }); } else if (command === 'display') { var cust = notes.getCust(argv.custDetails,argv.custName,argv.custEmail,argv.custCategory); if(cust){ custs.logCust(cust); //Displays a customer } else{ console.log("Customer not found"); } } else{ console.log('command note recognized'); }

custdata.js const fs = require('fs');

// ------------------Begin of Reusable functions ---------------------

var fetchCusts = () => { try { //if file won't exist var custsString = fs.readFileSync('customer-data.json') return JSON.parse(notesString); } catch(e){ return []; } };

var saveCusts = (custs) => { fs.writeFileSync('customer-data.json',JSON.stringify(cust)); };

// ------------------End of Reusable functions ---------------------

// to add a new customer

var addCust = (custId,custDetails) => { var custs = fetchCusts(); var cust = {custId,custDetails};

var duplicateCusts =  custs.filter((cust) => { // to check if the customer already exists
    return cust.custId === custId;
});

if (duplicateCusts.length === 0){
    custs.push(cust);

    saveCusts(custs);
    return cust
}

};

//to list all the customers

var getAll = () => { return fetchCusts();

};

// to display a customer

var getCust = (custId) => {

var custs = fetchCusts();

var getCusts =  custs.filter((cust) => {  // to check if a customer exists and return customer
    return cust.custId === custId;
});

return getCusts[0]

};

// to delete a note

var remove = (custId) => {

var custs = fetchCusts(); // reusable func

var filteredCusts =  custs.filter((cust) => { // will return all other notes other than "note to be removed"
    return cust.custId !== custId;
});

saveCusts(filteredCusts); //save new customer array

return custs.length !== filteredCusts.length

};

// function just to print out note to screen

var logCust = (cust) => { console.log('--'); console.log(custId: ${cust.custId}); console.log(custDetails: ${note.custDetails}); console.log(custName: ${note.custName}); console.log(custEmail: ${note.custEmail}); console.log(custCategory: ${note.custCategory}); };

// add new function names here to be accessible from other modules

module.exports = { addCust, getAll, remove, getCust,logCust };

JSON Output [{"custId":"c001","custDetails":"data1","custName":"johnDoe","custEmail":"[email protected]","custCategory":"new"}, {"custId":"c023","custDetails":"det2","custName":"janeSmith","custEmail":"[email protected]","custCategory":"existing"],