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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 87 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,110 @@
console.log("works!!", process.argv[2]);

// Global Variables
const args = process.argv
let operation = args[2];
let todoItem = args[3];

// PG to handle postGres requests
const pg = require('pg');

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

const client = new pg.Client(configs);


// Results Handler
let queryDoneCallback = (err, result) => {
if (err) {
console.log("query error", err.message);
console.log("query error", err.message);
} else {
console.log("result", result.rows );
switch (operation) {
case "show":
result.rows.forEach((entry, index) => {
console.log(`${result.rows[index].id}. ${result.rows[index].status} - ${result.rows[index].name}`);
})
break;
case "add":
console.log("Item successfully added!")
break;
case "done":
console.log("Item marked as done.")
break;
case "archive":
console.log("Item successfully archived.")
break;
case "stats":
if (todoItem === "add-time") {
console.log(`A total of ${result.rows[0].count} tasks have been added today`);
} else if (todoItem === "complete-time") {
console.log(`The average time to complete all tasks is ${result.rows[0].date_part} seconds`);
}
break;
default:
console.log("Invalid selection.")
break;
}
}
client.end();
};
}

let clientConnectionCallback = (err) => {

if( err ){
console.log( "error", err.message );
}
// Query Handler
let clientConnectionCallback = (err) => {
if( err ){
console.log( "error", err.message );
}
if (operation === "add"){
let text = "INSERT INTO items (status, name, updated_at) VALUES ('[ ]', $1, now()) RETURNING id;"
const values = [todoItem];
client.query(text, values, queryDoneCallback);
} else if (operation === "show") {
let text = "SELECT * FROM items ORDER BY id;"
client.query(text, queryDoneCallback);
} else if (operation === "done") {
const values = [todoItem];
let text = "UPDATE items SET status = '[x]', updated_at = now() WHERE id = $1;"
client.query(text, values, queryDoneCallback);
} else if (operation === "archive") {
const values = [todoItem];
client
.query("INSERT INTO archive SELECT status, name FROM items WHERE id= $1", values)
.then(result => console.log(`Archiving...`))
.catch(e => console.log(e.stack))

let text = "INSERT INTO todo (name) VALUES ($1) RETURNING id";
client
.query("DELETE FROM items WHERE id= $1;", values)
.then(result => console.log(`Done!`))
.catch(e => console.log(e.stack))
.then(() => client.end())

const values = ["hello"];
// let text = "DELETE FROM items WHERE id= $1;"
// client.query(text, values, queryDoneCallback);
} else if (operation === "stats" && todoItem === "complete-time") {
let text = "SELECT EXTRACT(EPOCH FROM avg(updated_at - created_at)) FROM items;"
client.query(text, queryDoneCallback);
} else if (operation === "stats" && todoItem ==="add-time") {
let text = "SELECT count(1) FROM items WHERE created_at > now() - interval '1 day';"
client.query(text, queryDoneCallback);
} else if (operation === "stats" && todoItem ==="best-worst") {
client
.query("SELECT name, time_taken FROM items WHERE time_taken = (SELECT min(time_taken) FROM items);")
.then(result => console.log(`Fastest task: ${result.rows[0].name} at ${result.rows[0].time_taken}`))
.catch(e => {
console.log(e.stack)
});

client.query(text, values, queryDoneCallback);
client
.query("SELECT name, time_taken FROM items WHERE time_taken = (SELECT max(time_taken) FROM items);")
.then(result => console.log(`Slowest task: ${result.rows[0].name} at ${result.rows[0].time_taken}`))
.catch(e => console.log(e.stack))
.then(() => client.end())
}
};

client.connect(clientConnectionCallback);

client.connect(clientConnectionCallback);
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.

129 changes: 129 additions & 0 deletions node_modules/buffer-writer/index.js

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

57 changes: 57 additions & 0 deletions node_modules/buffer-writer/package.json

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

1 change: 1 addition & 0 deletions node_modules/buffer-writer/test/mocha.opts

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

Loading