Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Travis Submission #110

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
72 changes: 67 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
console.log("works!!", process.argv[2]);

const pg = require('pg');

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

const client = new pg.Client(configs);

let queryDoneCallback = (err, result) => {
/*let queryDoneCallback = (err, result) => {
if (err) {
console.log("query error", err.message);
} else {
Expand All @@ -33,4 +31,68 @@ let clientConnectionCallback = (err) => {
client.query(text, values, queryDoneCallback);
};

client.connect(clientConnectionCallback);
client.connect(clientConnectionCallback);*/

client.connect((err) => {
if(err){
console.log("Error connecting: ", err.message);
}else {
if(process.argv[2] === "add") {
let task = "";
let date = Date();
for(let i=3; i<process.argv.length; i++){
task = task + " " + process.argv[i];
}
let values = [task, '[ ]', date.toString(), '-'];
let queryText = `INSERT INTO items (task, isdone, created_at, updated_at) VALUES ($1, $2, $3, $4) RETURNING *`;

client.query(queryText, values, (err, res) =>{
if(err){
console.log("Error in query: ", err.message);
}else {
console.log(`ID ${res.rows[0].id} has been inserted, result: `, res.rows);
}
});
}else if(process.argv[2] === "show"){
let queryText = `SELECT * FROM items`;
client.query(queryText, (err, res) => {
if(err) {
console.log("Error in query: ", err.message);
}else {
for(let i=0; i<res.rows.length; i++){
let line = `${res.rows[i].id}. ${res.rows[i].isdone} - ${res.rows[i].task}, Created at: ${res.rows[i].created_at}, Updated at: ${res.rows[i].updated_at}`;
console.log(line);
}
}
});
}else if(process.argv[2] === "done"){
let date = Date().toString();
let queryText = `UPDATE items SET isdone='[x]' WHERE id = ${process.argv[3]}`;
let queryText2 = `UPDATE items SET updated_at='${date}' WHERE id = ${process.argv[3]} RETURNING *`;

client.query(queryText, (err, res) => {
if(err){
console.log("Error in query: ", err.message);
}else {
client.query(queryText2, (err, res) => {
if(err){
console.log("Error in query: ", err.message);
}else {
console.log(`ID ${res.rows[0].id} has been updated! result: `, res.rows[0]);
}
});
}
});
}else if(process.argv[2] === "delete"){
let queryText = `DELETE from items WHERE id = ${process.argv[3]}`;

client.query(queryText, (err, res) => {
if(err){
console.log("Error in query: ", err.message);
}else {
console.log("Delete action completed");
}
});
}
}
});
119 changes: 119 additions & 0 deletions package-lock.json

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

22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "cli-todo-sql",
"version": "1.0.0",
"description": "![https://i.giphy.com/media/26ufnwz3wDUli7GU0/giphy.webp](https://i.giphy.com/media/26ufnwz3wDUli7GU0/giphy.webp)",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Travisenquiry/cli-todo-sql.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Travisenquiry/cli-todo-sql/issues"
},
"homepage": "https://github.com/Travisenquiry/cli-todo-sql#readme",
"dependencies": {
"pg": "^8.0.2"
}
}