Skip to content

Commit

Permalink
Application Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
vineets740 committed Feb 10, 2019
1 parent 60c96e7 commit 3e62d65
Show file tree
Hide file tree
Showing 5 changed files with 2,822 additions and 0 deletions.
47 changes: 47 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const fs=require('fs');
const _ = require('lodash');
const yargs = require('yargs');

const notes = require('./notes.js');

var argv = yargs.argv;
var command = argv._[0];


// console.log('Process', process.argv);
// console.log('Yargs', argv);

if(command == 'add'){
var note = notes.addNote(argv.title, argv.body);
if(note){
console.log('Note Created');
notes.logNote(note);
}else{
console.log("Note title taken");
}
}else if(command === 'list'){
var allNotes = notes.getAll();
console.log(`Printing ${allNotes.length} note(s)`);
allNotes.forEach(note => notes.logNote(note));
}else if(command === 'read'){
var note = notes.getNote(argv.title);
if(note){
console.log('Note Found');
notes.logNote(note);
}else{
console.log("Note Not Found");
}
}else if(command === 'remove'){
var removed = notes.removeNote(argv.title);
if(removed){
console.log('Removed Note');
console.log('--');
console.log(`Title: ${argv.title}`);
console.log(`Body: ${argv.body}`);
}else{
console.log('Note Not Found');
}

}else{
console.log('Command not recognised');
}
1 change: 1 addition & 0 deletions notes-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"title":"Secret1","body":"Note 1"},{"title":"Secret2","body":"Note 2"},{"title":"Secret3","body":"Note 2"}]
70 changes: 70 additions & 0 deletions notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const fs = require('fs');

var fetchNotes = () => {
try{
var notesString = fs.readFileSync('notes-data.json');
return JSON.parse(notesString);
}catch(e){
return [];
}
}

var saveNote = (notes) => {
fs.writeFileSync('notes-data.json',JSON.stringify(notes));
}

var addNote = (title, body) => {
notes = [];
note = {
title,
body
}

notes = fetchNotes();

var duplicateNotes = notes.filter((note) => note.title === title);

if(duplicateNotes.length === 0){
notes.push(note);
saveNote(notes);
return note;
}
}

var getAll = () => {
return fetchNotes();
}

var getNote = (title) =>{
var notes = fetchNotes();

var filteredNotes = notes.filter((note) => note.title === title);

return filteredNotes[0];
}

var removeNote = (title) => {
var notes = fetchNotes();

var filteredNotes = notes.filter((note) => note.title !== title);

saveNote(filteredNotes);

return notes.length !== filteredNotes.length;

}

var logNote = (note) => {
debugger;
console.log('--');
console.log(`Title: ${note.title}`);
console.log(`Body: ${note.body}`);
}

module.exports = {
addNote,
getAll,
getNote,
removeNote,
logNote
}
Loading

0 comments on commit 3e62d65

Please sign in to comment.