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

What To Do? #101

Open
wants to merge 3 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
99 changes: 78 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,93 @@
console.log("works!!", process.argv[2]);
console.log("Running Cli-Todo-SQL");
console.log("Query Selector: ", process.argv[2]);

const pg = require('pg');

const configs = {
user: 'akira',
host: '127.0.0.1',
database: 'todo',
port: 5432,
user: 'yusofgotboudine',
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 );
}
client.end();
};
let querySelector = process.argv[2];

client.connect((err) => {

let clientConnectionCallback = (err) => {
if (err) {
console.log("error", err.message);
}

if (querySelector == 'show') {
let queryText = 'SELECT * FROM items ORDER BY id ASC';

if( err ){
console.log( "error", err.message );
client.query(queryText, (err, res) => {
//console.log(res);
console.log(queryText);
if (err) {
console.log("query error", err.message);
} else {
// iterate through all of your results:
for (let i = 0; i < res.rows.length; i++) {
if (res.rows[i].completed == null) {
console.log((i + 1) + ". [ ] - " + res.rows[i].name);
} else if (res.rows[i].completed == 'yes') {
console.log((i + 1) + ". [x] - " + res.rows[i].name);
}
}
}
});
}

let text = "INSERT INTO todo (name) VALUES ($1) RETURNING id";
if (querySelector == 'add') {
let item = process.argv[3];
let queryText = "INSERT INTO items (name) VALUES ('" + item + "')";
console.log(queryText);

const values = ["hello"];
client.query(queryText, (err, res) => {
if (err) {
console.log("query error", err.message);
} else {
console.log("Item added on todo list: ", item);
}
});

client.query(text, values, queryDoneCallback);
};
}

if (querySelector == 'done') {
let item = process.argv[3];
if (item == '') {
console.log("No input detected!");
client.end();
}
let queryText = "SELECT * FROM items ORDER BY id ASC";
let markItem = item - 1;

client.query(queryText, (err, res) => {
// console.log(res.rows[markItem].name);
if (err) {
console.log("query error", err.message);
} else {
for (let i = 0; i < res.rows.length; i++) {
if (i != markItem && res.rows[i].completed == null) {
console.log((i + 1) + ". [ ] - " + res.rows[i].name);
} else if (i != markItem && res.rows[i].completed != null) {
console.log((i + 1) + ". [x] - " + res.rows[i].name);
} else if (i == markItem) {
console.log((i + 1) + ". [x] - " + res.rows[i].name);
queryText = "UPDATE items SET completed='yes' WHERE name = '" + res.rows[i].name + "'";
client.query(queryText, (err, res) => {
console.log(queryText);
if (err) {
console.log("query error", err.message);
}
});
}
}
}
});
}
})

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