Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
48 changes: 48 additions & 0 deletions checklist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
create a cli app to store list of items to be done
user will indicate input function and provide parameters


CLI

[] show
[] retrieve all the list items from db
[] log the data in a format string
[] indicate its completion by marking x in the square brackets

[] add
[] insert a new item into the db

[] done
[] mark as complete
[] display the updated list

[] stats complete-time
[] log the average completion time of all the items

[] stats add-time
[] log the average amt of items added per day

[] stats best-worst
[] log two items
- item completed the fastest
- item completed the slowest

[] between date date
[] log the items added between these dates

[] between complete date date
[] log the items completed between these dates

[] stats between date date complete-time asc
[] log all the items between two dates and sort it by the time to complete in asc or desc order

DATABASE

[] structure the item
|id|completed|activity|created_at|updated_at|
serial boolean string timestamp timestamp


TESTING

[] have a script to build dummy data
141 changes: 124 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,143 @@
console.log("works!!", process.argv[2]);
#!/usr/bin/env node

const { program } = require('commander');
program.version('0.0.1');

const moment = require('moment');
// set up for psql
const pg = require('pg');

const configs = {
user: 'akira',
user: 'Hilman',
host: '127.0.0.1',
database: 'todo',
port: 5432,
};

const client = new pg.Client(configs);

let queryDoneCallback = (err, result) => {
if (err) {
console.log("query error", err.message);
} else {
console.log("result", result.rows );

// declare options here
program
.option('-s, --show', 'show all items')
.option('-a, --add <activity>', 'add new item')
.option('-d, --done <id>', 'mark item as done')
.option('--stats [option]', 'show stat option')
.option('--between [option]', 'show all items')
.option('--archive <id>', 'archive item')
.option('--debug', 'show all options')


// parse cli option and params
program.parse(process.argv);


// all handlers must end connection at the end of the execution

client.connect();

async function getData(query) {
try{
let result = await client.query(query);
return result.rows
} catch(err) {
client.end();
console.log(err);
}
client.end();
};
}

async function addData(query) {
try{
let result = await client.query(query);
} catch(err) {
client.end();
console.log(err);
}
}

async function updateById(query) {
try{
let result = await client.query(query);
} catch(err) {
client.end();
console.log(err);
}
}

let clientConnectionCallback = (err) => {
let handleShow =(array)=> {
array.forEach((item, index)=>{
let {id,completed,activity, created_at, updated_at} = item;
let mark = completed ? "x" : " ";
let date = moment(created_at).format("Do MMMM YYYY");
let doneDate = completed ? "Completed: " + moment(updated_at).format("Do MMMM YYYY") : "";
let text = `${id}. [${mark}] - ${activity} Created: ${date} ${doneDate}`;
console.log(text);
})
}

if( err ){
console.log( "error", err.message );
}
let showAll =()=>{
// always show in sequential order
query = "SELECT * FROM items WHERE archived = false ORDER BY id;";
getData(query).then(result=>handleShow(result)).then(()=>client.end()).catch(err=>console.log(err))
}

let text = "INSERT INTO todo (name) VALUES ($1) RETURNING id";
let handleCompleteTime =(array)=> {
let sum = array.reduce((acc, obj)=>{
let a = moment(obj.created_at);
let b = moment(obj.updated_at);
let diff = b.diff(a, 'minutes', true)
return acc + diff;
},0)
let average = sum / array.length;
console.log("Average time to complete in minutes: " + average);
}

const values = ["hello"];
// detecting user selection

client.query(text, values, queryDoneCallback);
let query = null;

if (program.show) {
showAll();
};

//insert
if (program.add) {
query = `INSERT INTO items (completed,archived,activity) VALUES (false, false,'${program.add}');`
addData(query);
showAll();
};

//update
if (program.done) {
query = `UPDATE items SET completed = true, updated_at = now() WHERE id = ${program.done}`
updateById(query).then(()=>showAll())
};

if (program.archive) {
query = `UPDATE items SET archived = true, updated_at = now() WHERE id = ${program.archive}`
updateById(query).then(()=>showAll())
};

client.connect(clientConnectionCallback);
if (program.stats === true) {

console.log("stats options are: complete-time, add-time, best-worst");

} else if (program.stats === 'complete-time') {

query = 'SELECT created_at, updated_at FROM items WHERE completed = true;'
getData(query)
.then((result)=>handleCompleteTime(result))
.then(()=>client.end())
.catch(err=>console.log(err))

} else if (program.stats === 'add-time') {
console.log('add-time');
} else if (program.stats === 'best-worst') {
console.log('best-worst');
}


if (program.between === true) console.log("options are: add, done");
else if (program.between === "add") console.log("between add"); //get data
else if (program.between === "done") console.log("between done"); //get data
if (program.debug) console.log(program.opts());
1 change: 1 addition & 0 deletions node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions node_modules/buffer-writer/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions node_modules/buffer-writer/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions node_modules/buffer-writer/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading