diff --git a/.gitignore b/.gitignore index c5582434..05dcb6b5 100755 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .svn *~ .*.swp +node_modules \ No newline at end of file diff --git a/index.js b/index.js index 3907d3b5..ce569c25 100644 --- a/index.js +++ b/index.js @@ -1,36 +1,468 @@ -console.log("works!!", process.argv[2]); +//console.log("works!!", process.argv[2]); const pg = require('pg'); - +let startDate=""; const configs = { - user: 'akira', + user: 'kenneththesheep', host: '127.0.0.1', database: 'todo', port: 5432, }; - +let data={task:[]}; const client = new pg.Client(configs); +function formatDate(date) { + var d = new Date(date), + month = '' + (d.getMonth() + 1), + day = '' + d.getDate(), + year = d.getFullYear(); + + if (month.length < 2) + month = '0' + month; + if (day.length < 2) + day = '0' + day; + + return [day, month, year].join('/'); +} + let queryDoneCallback = (err, result) => { + if (err) { + console.log("query error", err.message); + } else { + //console.log("result", result.rows ); + //console.log(result.rows); + + } + client.end(); +}; + +let queryDoneCallback2 = (err, result) => { + if (err) { console.log("query error", err.message); } else { console.log("result", result.rows ); + //result.rows[0].completedtime="Something"; + console.log(result.rows); + + } + client.end(); +}; + +let queryDoneCallbackAverage = (err, result) => { + + if (err) { + console.log("query error", err.message); + } else { + //console.log("result", result.rows ); + let completedTime=[]; + let sum=0; + for(let count=0; count { + + if (err) { + console.log("query error", err.message); + } else { + //console.log("result", result.rows ); + let numberOfTask=result.rows.length; + let sum=0; + let createdTime=[] + for(let count=0; count { + + if (err) { + console.log("query error", err.message); + } else { + //console.log("result", result.rows ); + + + let completedTime=[]; + let sum=0; + let worstTask="" + let bestTask="" + for(let count=0; count { + if (err) { + console.log("query error", err.message); + } else { + console.log(`All result between ${process.argv[3]} and ${process.argv[4]}`, result.rows ); + let completedTask=[]; + for(let count=0; count timingB) { + comparison = 1; + } else if (timingA < timingB) { + comparison = -1; + } + return comparison; +} + +let queryDoneCallbackBetweenAsc = (err, result) => { + if (err) { + console.log("query error", err.message); + } else { + // console.log(`All result between ${process.argv[4]} and ${process.argv[5]}`, result.rows ); + let completedTask=[]; + for(let count=0; count{ + let date=new Date(); + let formattedDate=formatDate(date); + let timeSecond=new Date(); + n=timeSecond.getTime()/1000; + let text = "INSERT INTO items (completion, name, created_at, created_second) VALUES ($1, $2, $3, $4) RETURNING id, created_at"; + const values = ["[ ]",process.argv[3], formattedDate,n]; + //console.log(text); + + client.query(text, values, queryDoneCallback); +} + +///////// view Task +let viewTask=()=>{ + let text = `SELECT * FROM items WHERE id=${parseInt(process.argv[3])} `; + console.log("text is "+text); + client.query(text, queryDoneCallback); +} + +//////////////////Update Tasks +let updateTask=()=>{ + console.log(typeof Date()); + let date = Date(); + //let ReadText="SELECT * from items"; + //client.query(ReadText, queryDoneCallback); + let Updatetext = `SELECT * FROM items WHERE id=${parseInt(process.argv[3])}`; + client.query(Updatetext, queryDoneCallback2); + + +} + +//////////////////Delete Tasks +let archiveTask=()=>{ + + + let archivetext = `DELETE FROM items WHERE id=${parseInt(process.argv[3])}`; + client.query(archivetext, queryDoneCallback2); + } + + + + + +//////////////////Done Tasks +let doneTask=()=>{ + console.log(typeof Date()); + let date = new Date(); + let formattedDate=formatDate(date); + let calculateDate=new Date(); + let calculateSecond=calculateDate.getTime()/1000; + //let ReadText="SELECT * from items"; + //client.query(ReadText, queryDoneCallback); + let Updatetext = `UPDATE items SET completion = REPLACE(completion,completion,'[ X ]'), finished_at =($1), completedtime = ($2)- created_second WHERE id=${parseInt(process.argv[3])}`; + client.query(Updatetext,[formattedDate, calculateSecond], queryDoneCallback2); + + +} + +let computeAverage=()=>{ + let text="SELECT * FROM items"; + if(process.argv[3].toLowerCase()==="complete-time" || process.argv[3].toLowerCase()==="time") + { + client.query(text, queryDoneCallbackAverage); + } + if(process.argv[3].toLowerCase()==="add-time"|| process.argv[3].toLowerCase()==="task") + { + client.query(text, queryDoneCallbackTask); + } + if(process.argv[3].toLowerCase()==="best-worst"|| process.argv[3].toLowerCase()==="bw") + { + client.query(text, queryDoneCallbackBestWorst); + } +} + + + +let findBetween=()=>{ + let text="SELECT * FROM items WHERE created_at >= ($1) AND created_at <= ($2)" + client.query(text,[process.argv[3], process.argv[4]],queryDoneCallbackBetween); +} + +let findBetweenAsc=()=>{ + let text="SELECT * FROM items WHERE created_at >= ($1) AND created_at <= ($2)" + client.query(text,[process.argv[4], process.argv[5]],queryDoneCallbackBetweenAsc); +} + let clientConnectionCallback = (err) => { if( err ){ console.log( "error", err.message ); } - let text = "INSERT INTO todo (name) VALUES ($1) RETURNING id"; +if(process.argv[2].toLowerCase()==="add") +{ + addTask(); + return +} +if(process.argv[2].toLowerCase()==="done") +{ + doneTask(); + return +} +if(process.argv[2].toLowerCase()==="view") +{ + viewTask(); + return +} +if(process.argv[2].toLowerCase()==="update") +{ + updateTask(); + return +} - const values = ["hello"]; +if(process.argv[2].toLowerCase()==="archive") +{ - client.query(text, values, queryDoneCallback); + archiveTask(); + return +} + +if(process.argv[2].toLowerCase()==="stats"&& process.argv[3].toLowerCase()==="between" && process.argv[6]==="complete-time" && process.argv[7].toLowerCase()==="asc") +{ + + findBetweenAsc(); + return; +} +if(process.argv[2].toLowerCase()==="stats"&&process.argv[4]===undefined) +{ + + computeAverage(); + + + return; +} +if(process.argv[2].toLowerCase()==="between") +{ + findBetween(); + return +} + +console.log(` + ██▓ ███▄ █ ██▒ █▓ ▄▄▄ ██▓ ██▓▓█████▄ +▓██▒ ██ ▀█ █▓██░ █▒▒████▄ ▓██▒ ▓██▒▒██▀ ██▌ +▒██▒▓██ ▀█ ██▒▓██ █▒░▒██ ▀█▄ ▒██░ ▒██▒░██ █▌ +░██░▓██▒ ▐▌██▒ ▒██ █░░░██▄▄▄▄██ ▒██░ ░██░░▓█▄ ▌ +░██░▒██░ ▓██░ ▒▀█░ ▓█ ▓██▒░██████▒░██░░▒████▓ +░▓ ░ ▒░ ▒ ▒ ░ ▐░ ▒▒ ▓▒█░░ ▒░▓ ░░▓ ▒▒▓ ▒ + ▒ ░░ ░░ ░ ▒░ ░ ░░ ▒ ▒▒ ░░ ░ ▒ ░ ▒ ░ ░ ▒ ▒ + ▒ ░ ░ ░ ░ ░░ ░ ▒ ░ ░ ▒ ░ ░ ░ ░ + ░ ░ ░ ░ ░ ░ ░ ░ ░ + ░ ░ +`); +return; }; -client.connect(clientConnectionCallback); + +///////Commander input +function activateReadWrite(word, input) +{ + + + if(word === "add") + { + + addTask(); + return + + } + + + if(word === "done" ) + { + doneTask(); + return + + } + if( word === "archive") + { + archiveTask(); + return; + + } + + if( word === "compare") + { + computeAverage(); + return; + + } + +} + + + +const { program } = require('commander'); + + program + + .description("Select an option. '-a value' to add value. -s to show. '-d value' to update what is done. '-ar value' to archive task. '-c time' to find average time. '-c task' to find average task add rate. '-c bw' to find the best and worst time. '-s asc' to sort") + .option("-a, --value ") + .option("-c, --compare ") + .option("-ar, --archive "); + + program.parse(process.argv); + if( program.value ) activateReadWrite ( "add" , program.value ); + if (program.compare) activateReadWrite ("compare", program.compare); + if ( program.done ) activateReadWrite ( "done" , program.done ); + if ( program.archive ) activateReadWrite ( "archive" , program.del ); + + + + console.log(` + ▄▄▄▄▄▄▄▄▄▄▄ ▄▄ ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄ ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄ ▄ ▄▄▄▄▄▄▄▄▄▄▄ +▐░░░░░░░░░░░▌▐░░▌ ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌ ▐░▌▐░░░░░░░░░░░▌ + ▀▀▀▀█░█▀▀▀▀ ▐░▌░▌ ▐░▌▐░█▀▀▀▀▀▀▀▀▀ ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌ ▐░▌▐░█▀▀▀▀▀▀▀▀▀ ▀▀▀▀█░█▀▀▀▀ ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌░▌ ▐░▌▐░█▀▀▀▀▀▀▀▀▀ + ▐░▌ ▐░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌▐░▌ ▐░▌▐░▌ + ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░█▄▄▄▄▄▄▄█░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌▐░█▄▄▄▄▄▄▄▄▄ + ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░░░░░░░░░░░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌▐░░░░░░░░░░░▌ + ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▀▀▀▀▀▀▀▀▀█░▌ ▐░▌ ▐░█▀▀▀▀█░█▀▀ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▀▀▀▀▀▀▀▀▀█░▌ + ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌▐░▌ ▐░▌ + ▄▄▄▄█░█▄▄▄▄ ▐░▌ ▐░▐░▌ ▄▄▄▄▄▄▄▄▄█░▌ ▐░▌ ▐░▌ ▐░▌ ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▄▄▄▄█░█▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░▌ ▐░▐░▌ ▄▄▄▄▄▄▄▄▄█░▌ +▐░░░░░░░░░░░▌▐░▌ ▐░░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░▌ ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌ ▐░░▌▐░░░░░░░░░░░▌ + ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀▀ ▀▀▀▀▀▀▀▀▀▀▀ +`); +console.log(` +.▄▄ · ▄▄▄ .▄▄▌ ▄▄▄ . ▄▄· ▄▄▄▄▄ ▄▄▄· ▐ ▄ ▄▄▄·▄▄▄▄▄▪ ▐ ▄ +▐█ ▀. ▀▄.▀·██• ▀▄.▀·▐█ ▌▪•██ ▐█ ▀█ •█▌▐█ ▪ ▐█ ▄█•██ ██ ▪ •█▌▐█ +▄▀▀▀█▄▐▀▀▪▄██▪ ▐▀▀▪▄██ ▄▄ ▐█.▪ ▄█▀▀█ ▐█▐▐▌ ▄█▀▄ ██▀· ▐█.▪▐█· ▄█▀▄ ▐█▐▐▌ +▐█▄▪▐█▐█▄▄▌▐█▌▐▌▐█▄▄▌▐███▌ ▐█▌· ▐█ ▪▐▌██▐█▌ ▐█▌.▐▌▐█▪·• ▐█▌·▐█▌▐█▌.▐▌██▐█▌ + ▀▀▀▀ ▀▀▀ .▀▀▀ ▀▀▀ ·▀▀▀ ▀▀▀ ▀ ▀ ▀▀ █▪ ▀█▄▀▪.▀ ▀▀▀ ▀▀▀ ▀█▄▀▪▀▀ █▪ +`); +console.log(`'-a value' to add task.\n'-d value' to update what is done.\n'-ar value' to archive task.\n -c time to find average completion time \n -c task to find average add task per day\n -c bw to find best and worst time`); + + + +client.connect(clientConnectionCallback); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..29bcca10 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,124 @@ +{ + "name": "cli-todo-sql", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "buffer-writer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" + }, + "commander": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-5.0.0.tgz", + "integrity": "sha512-JrDGPAKjMGSP1G0DUoaceEJ3DZgAfr/q6X7FVk4+U5KxUSKviYGM2k6zWkfyyBHy5rAtzgYJFa1ro2O9PtoxwQ==" + }, + "packet-reader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" + }, + "pg": { + "version": "8.0.2", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.0.2.tgz", + "integrity": "sha512-ngOUEDk69kLdH/k/YLT2NRIBcUiPFRcY4l51dviqn79P5qIa5jBIGIFTIGXh4OlT/6gpiCAza5a9uy08izpFQQ==", + "requires": { + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "0.1.3", + "pg-pool": "^3.1.0", + "pg-protocol": "^1.2.1", + "pg-types": "^2.1.0", + "pgpass": "1.x", + "semver": "4.3.2" + } + }, + "pg-connection-string": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz", + "integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=" + }, + "pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" + }, + "pg-pool": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.1.0.tgz", + "integrity": "sha512-CvxGctDwjZZad6Q7vvhFA4BsYdk26UFIZaFH0XXqHId5uBOc26vco/GFh/laUVIQUpD9IKe/f9/mr/OQHyQ2ZA==" + }, + "pg-protocol": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.2.1.tgz", + "integrity": "sha512-IqZ+VUOqg3yydxSt5NgNKLVK9JgPBuzq4ZbA9GmrmIkQjQAszPT9DLqTtID0mKsLEZB68PU0gjLla561WZ2QkQ==" + }, + "pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "requires": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + } + }, + "pgpass": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.2.tgz", + "integrity": "sha1-Knu0G2BltnkH6R2hsHwYR8h3swY=", + "requires": { + "split": "^1.0.0" + } + }, + "postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==" + }, + "postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" + }, + "postgres-date": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.5.tgz", + "integrity": "sha512-pdau6GRPERdAYUQwkBnGKxEfPyhVZXG/JiS44iZWiNdSOWE09N2lUgN6yshuq6fVSon4Pm0VMXd1srUUkLe9iA==" + }, + "postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "requires": { + "xtend": "^4.0.0" + } + }, + "semver": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz", + "integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=" + }, + "split": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", + "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", + "requires": { + "through": "2" + } + }, + "through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 00000000..b5a644f0 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "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/kenneththesheep/cli-todo-sql.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/kenneththesheep/cli-todo-sql/issues" + }, + "homepage": "https://github.com/kenneththesheep/cli-todo-sql#readme", + "dependencies": { + "commander": "^5.0.0", + "pg": "^8.0.2" + } +}